SlideShare a Scribd company logo
1 of 4
Download to read offline
Please fix pygame code so that the first "Enemy" bounces off the walls of the screen. Thank
You.
import pygame
import sys
import math
import random
def pixel_collision(mask1, rect1, mask2, rect2):
offset_x = rect2[0] - rect1[0]
offset_y = rect2[1] - rect1[1]
overlap = mask1.overlap(mask2, (offset_x, offset_y))
return overlap
class Sprite:
def __init__(self, image, x, y):
self.image = image
self.rectangle = image.get_rect()
self.rectangle.center = (x, y)
self.mask = pygame.mask.from_surface(image)
def set_position(self, new_position):
self.rectangle.center = new_position
def draw(self, screen):
screen.blit(self.image, self.rectangle)
def is_colliding(self, other_sprite):
return pixel_collision(self.mask, self.rectangle, other_sprite.mask, other_sprite.rectangle)
class Enemy(Sprite):
def __init__(self, image, width, height):
super().__init__(image, width, height)
self.rectangle.centerx = random.randint(0, width)
self.rectangle.centery = random.randint(0, height)
vx = random.uniform(-1, 1)
vy = random.uniform(-1, 1)
self.speed = (vx, vy)
def move(self):
self.rectangle.move_ip(self.speed)
def bounce(self, width, height):
if self.rectangle.left < 0 or self.rectangle.right > width:
self.speed = (-self.speed[0], self.speed[1])
if self.rectangle.top < 0 or self.rectangle.bottom > height:
self.speed = (self.speed[0], -self.speed[1])
class Enemy2(Sprite):
def __init__(self, image, width, height):
super().__init__(image, width, height)
self.speed = (0, 0)
self.acceleration = 0.1
def move(self, target_position):
dx = target_position[0] - self.rectangle.centerx
dy = target_position[1] - self.rectangle.centery
dist = math.sqrt(dx ** 2 + dy ** 2)
if dist != 0:
dx_norm = dx / dist
dy_norm = dy / dist
self.speed = (self.speed[0] + dx_norm * self.acceleration, self.speed[1] + dy_norm *
self.acceleration)
self.rectangle.move_ip(self.speed[0], self.speed[1])
class PowerUp(Sprite):
def __init__(self, image, width, height):
super().__init__(image, width, height)
def main():
pygame.init()
myfont = pygame.font.SysFont('monospace', 24)
width, height = 600, 400
screen = pygame.display.set_mode((width, height))
enemy1 = pygame.image.load("devil.png").convert_alpha()
enemy2 = pygame.image.load("ghost.png").convert_alpha()
enemy1_image = pygame.transform.smoothscale(enemy1, (50, 50))
enemy2_image = pygame.transform.smoothscale(enemy2, (50, 50))
enemy_sprites = []
enemy2_sprites = []
for i in range(5):
enemy_sprites.append(Enemy(enemy1_image, width, height))
for i in range(2):
enemy2_sprites.append(Enemy2(enemy2_image, width, height))
player_image = pygame.image.load("prayerhands.jpeg").convert_alpha()
player_image = pygame.transform.scale(player_image, (50, 50))
x, y = 0, 0
player_sprite = Sprite(player_image, x, y)
life = 3
powerup_image = pygame.image.load("life_powerup.jpeg").convert_alpha()
powerups = []
is_playing = True
clock = pygame.time.Clock()
while is_playing:
clock.tick(60)
if life <= 0:
is_playing = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_playing = False
pos = pygame.mouse.get_pos()
player_sprite.set_position(pos)
for enemy_sprite in enemy_sprites:
if player_sprite.is_colliding(enemy_sprite):
life -= 0.2
for powerup_sprite in powerups:
if player_sprite.is_colliding(powerup_sprite):
life += 1
powerups = [p for p in powerups if not player_sprite.is_colliding(p)]
for enemy_sprite in enemy_sprites:
enemy_sprite.move()
enemy_sprite.bounce(width, height)
for enemy2_sprite in enemy2_sprites:
enemy2_sprite.move(player_sprite.rectangle.center)
if random.random() < 0.01:
powerups.append(PowerUp(powerup_image, random.randint(0, width),
random.randint(0, height)))
screen.fill((255, 255, 255))
for enemy_sprite in enemy_sprites:
enemy_sprite.draw(screen)
for enemy2_sprite in enemy2_sprites:
enemy2_sprite.draw(screen)
for powerup_sprite in powerups:
powerup_sprite.draw(screen)
player_sprite.draw(screen)
text = "Life: " + str('%.1f' % life)
label = myfont.render(text, True, (0, 0, 0))
screen.blit(label, (20, 20))
pygame.display.update()
pygame.time.wait(2000)
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()

More Related Content

Similar to Please fix pygame code so that the first Enemy bounces off the wal.pdf

Using the code below- I need help with the following 3 things- 1) Writ.pdf
Using the code below- I need help with the following 3 things- 1) Writ.pdfUsing the code below- I need help with the following 3 things- 1) Writ.pdf
Using the code below- I need help with the following 3 things- 1) Writ.pdfacteleshoppe
Β 
You are task to add a yawning detection to the programme below;i.pdf
You are task to add a yawning detection to the programme below;i.pdfYou are task to add a yawning detection to the programme below;i.pdf
You are task to add a yawning detection to the programme below;i.pdfsales223546
Β 
Need an detailed analysis of what this code-model is doing- Thanks #St.pdf
Need an detailed analysis of what this code-model is doing- Thanks #St.pdfNeed an detailed analysis of what this code-model is doing- Thanks #St.pdf
Need an detailed analysis of what this code-model is doing- Thanks #St.pdfactexerode
Β 
Modify the following source code so that when the mouse is clicked w.pdf
Modify the following source code so that when the mouse is clicked w.pdfModify the following source code so that when the mouse is clicked w.pdf
Modify the following source code so that when the mouse is clicked w.pdfarorastores
Β 
building_games_with_ruby_rubyconf
building_games_with_ruby_rubyconfbuilding_games_with_ruby_rubyconf
building_games_with_ruby_rubyconftutorialsruby
Β 
building_games_with_ruby_rubyconf
building_games_with_ruby_rubyconfbuilding_games_with_ruby_rubyconf
building_games_with_ruby_rubyconftutorialsruby
Β 
Denis Sergienko "Pip install driven deep learning"
Denis Sergienko "Pip install driven deep learning"Denis Sergienko "Pip install driven deep learning"
Denis Sergienko "Pip install driven deep learning"Fwdays
Β 
Making Games in JavaScript
Making Games in JavaScriptMaking Games in JavaScript
Making Games in JavaScriptSam Cartwright
Β 
Object Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonObject Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonTendayi Mawushe
Β 
Game age ppt
Game age pptGame age ppt
Game age pptAhmed Yousef
Β 
The Ring programming language version 1.10 book - Part 70 of 212
The Ring programming language version 1.10 book - Part 70 of 212The Ring programming language version 1.10 book - Part 70 of 212
The Ring programming language version 1.10 book - Part 70 of 212Mahmoud Samir Fayed
Β 
Particle Filter Tracking in Python
Particle Filter Tracking in PythonParticle Filter Tracking in Python
Particle Filter Tracking in PythonKohta Ishikawa
Β 
Player x 0 y ga.docx
Player x 0 y ga.docxPlayer x 0 y ga.docx
Player x 0 y ga.docxmattjtoni51554
Β 

Similar to Please fix pygame code so that the first Enemy bounces off the wal.pdf (20)

Using the code below- I need help with the following 3 things- 1) Writ.pdf
Using the code below- I need help with the following 3 things- 1) Writ.pdfUsing the code below- I need help with the following 3 things- 1) Writ.pdf
Using the code below- I need help with the following 3 things- 1) Writ.pdf
Β 
You are task to add a yawning detection to the programme below;i.pdf
You are task to add a yawning detection to the programme below;i.pdfYou are task to add a yawning detection to the programme below;i.pdf
You are task to add a yawning detection to the programme below;i.pdf
Β 
Need an detailed analysis of what this code-model is doing- Thanks #St.pdf
Need an detailed analysis of what this code-model is doing- Thanks #St.pdfNeed an detailed analysis of what this code-model is doing- Thanks #St.pdf
Need an detailed analysis of what this code-model is doing- Thanks #St.pdf
Β 
Scmad Chapter07
Scmad Chapter07Scmad Chapter07
Scmad Chapter07
Β 
Modify the following source code so that when the mouse is clicked w.pdf
Modify the following source code so that when the mouse is clicked w.pdfModify the following source code so that when the mouse is clicked w.pdf
Modify the following source code so that when the mouse is clicked w.pdf
Β 
building_games_with_ruby_rubyconf
building_games_with_ruby_rubyconfbuilding_games_with_ruby_rubyconf
building_games_with_ruby_rubyconf
Β 
building_games_with_ruby_rubyconf
building_games_with_ruby_rubyconfbuilding_games_with_ruby_rubyconf
building_games_with_ruby_rubyconf
Β 
Of class1
Of class1Of class1
Of class1
Β 
Game dev 101 part 3
Game dev 101 part 3Game dev 101 part 3
Game dev 101 part 3
Β 
Denis Sergienko "Pip install driven deep learning"
Denis Sergienko "Pip install driven deep learning"Denis Sergienko "Pip install driven deep learning"
Denis Sergienko "Pip install driven deep learning"
Β 
Making Games in JavaScript
Making Games in JavaScriptMaking Games in JavaScript
Making Games in JavaScript
Β 
Factory in python
Factory in pythonFactory in python
Factory in python
Β 
Object Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in PythonObject Orientation vs Functional Programming in Python
Object Orientation vs Functional Programming in Python
Β 
Game age ppt
Game age pptGame age ppt
Game age ppt
Β 
The Ring programming language version 1.10 book - Part 70 of 212
The Ring programming language version 1.10 book - Part 70 of 212The Ring programming language version 1.10 book - Part 70 of 212
The Ring programming language version 1.10 book - Part 70 of 212
Β 
Particle Filter Tracking in Python
Particle Filter Tracking in PythonParticle Filter Tracking in Python
Particle Filter Tracking in Python
Β 
Real life XNA
Real life XNAReal life XNA
Real life XNA
Β 
Player x 0 y ga.docx
Player x 0 y ga.docxPlayer x 0 y ga.docx
Player x 0 y ga.docx
Β 
Pygame presentation
Pygame presentationPygame presentation
Pygame presentation
Β 
14709302.ppt
14709302.ppt14709302.ppt
14709302.ppt
Β 

More from amarnathmahajansport

Please ask experts to solve this in the correct way. 1. (20 point.pdf
Please ask experts to solve this in the correct way.  1. (20 point.pdfPlease ask experts to solve this in the correct way.  1. (20 point.pdf
Please ask experts to solve this in the correct way. 1. (20 point.pdfamarnathmahajansport
Β 
please answer this multiple choice question2.What major Department.pdf
please answer this multiple choice question2.What major Department.pdfplease answer this multiple choice question2.What major Department.pdf
please answer this multiple choice question2.What major Department.pdfamarnathmahajansport
Β 
please answer this multiple choice question4.This Technical Review.pdf
please answer this multiple choice question4.This Technical Review.pdfplease answer this multiple choice question4.This Technical Review.pdf
please answer this multiple choice question4.This Technical Review.pdfamarnathmahajansport
Β 
Please answer question in full 2.1 Security management function.pdf
Please answer question in full  2.1 Security management function.pdfPlease answer question in full  2.1 Security management function.pdf
Please answer question in full 2.1 Security management function.pdfamarnathmahajansport
Β 
Please answer these questions 150 words only 1) List four lines o.pdf
Please answer these questions 150 words only  1) List four lines o.pdfPlease answer these questions 150 words only  1) List four lines o.pdf
Please answer these questions 150 words only 1) List four lines o.pdfamarnathmahajansport
Β 
Please answer the 4 questions at the end please do all of them at th.pdf
Please answer the 4 questions at the end please do all of them at th.pdfPlease answer the 4 questions at the end please do all of them at th.pdf
Please answer the 4 questions at the end please do all of them at th.pdfamarnathmahajansport
Β 
Please answer the questions in orderExplain why a virus is co.pdf
Please answer the questions in orderExplain why a virus is co.pdfPlease answer the questions in orderExplain why a virus is co.pdf
Please answer the questions in orderExplain why a virus is co.pdfamarnathmahajansport
Β 
Please answer the questions in orderCompare and contrast Virus a.pdf
Please answer the questions in orderCompare and contrast Virus a.pdfPlease answer the questions in orderCompare and contrast Virus a.pdf
Please answer the questions in orderCompare and contrast Virus a.pdfamarnathmahajansport
Β 
Please answer the questions in ordarExplain the advantage for sur.pdf
Please answer the questions in ordarExplain the advantage for sur.pdfPlease answer the questions in ordarExplain the advantage for sur.pdf
Please answer the questions in ordarExplain the advantage for sur.pdfamarnathmahajansport
Β 
Please give a detailed summary of the article where I can write at l.pdf
Please give a detailed summary of the article where I can write at l.pdfPlease give a detailed summary of the article where I can write at l.pdf
Please give a detailed summary of the article where I can write at l.pdfamarnathmahajansport
Β 
Please answer the 4 questions at the end Causes and Outcomes of Co.pdf
Please answer the 4 questions at the end Causes and Outcomes of Co.pdfPlease answer the 4 questions at the end Causes and Outcomes of Co.pdf
Please answer the 4 questions at the end Causes and Outcomes of Co.pdfamarnathmahajansport
Β 
please help In multiple regression analysis, residuals (YY^) shou.pdf
please help  In multiple regression analysis, residuals (YY^) shou.pdfplease help  In multiple regression analysis, residuals (YY^) shou.pdf
please help In multiple regression analysis, residuals (YY^) shou.pdfamarnathmahajansport
Β 
please fill in the chart A company that uses job order c.pdf
please fill in the chart A company that uses job order c.pdfplease fill in the chart A company that uses job order c.pdf
please fill in the chart A company that uses job order c.pdfamarnathmahajansport
Β 
Please fill in the code to run the program based on the following in.pdf
Please fill in the code to run the program based on the following in.pdfPlease fill in the code to run the program based on the following in.pdf
Please fill in the code to run the program based on the following in.pdfamarnathmahajansport
Β 
Please explain fully each step and what inputs and the number of row.pdf
Please explain fully each step and what inputs and the number of row.pdfPlease explain fully each step and what inputs and the number of row.pdf
Please explain fully each step and what inputs and the number of row.pdfamarnathmahajansport
Β 
please explain how attaker crack 2DES and show your work. 3DES = Run.pdf
please explain how attaker crack 2DES and show your work. 3DES = Run.pdfplease explain how attaker crack 2DES and show your work. 3DES = Run.pdf
please explain how attaker crack 2DES and show your work. 3DES = Run.pdfamarnathmahajansport
Β 
Please explain answer choice 26. Small functionally disruptive hardw.pdf
Please explain answer choice 26. Small functionally disruptive hardw.pdfPlease explain answer choice 26. Small functionally disruptive hardw.pdf
Please explain answer choice 26. Small functionally disruptive hardw.pdfamarnathmahajansport
Β 
Please explain answer choice 15. Match each of the following counter.pdf
Please explain answer choice 15. Match each of the following counter.pdfPlease explain answer choice 15. Match each of the following counter.pdf
Please explain answer choice 15. Match each of the following counter.pdfamarnathmahajansport
Β 
Please draw an activity diagram using Enterprise. or draw using symb.pdf
Please draw an activity diagram using Enterprise. or draw using symb.pdfPlease draw an activity diagram using Enterprise. or draw using symb.pdf
Please draw an activity diagram using Enterprise. or draw using symb.pdfamarnathmahajansport
Β 

More from amarnathmahajansport (20)

Please ask experts to solve this in the correct way. 1. (20 point.pdf
Please ask experts to solve this in the correct way.  1. (20 point.pdfPlease ask experts to solve this in the correct way.  1. (20 point.pdf
Please ask experts to solve this in the correct way. 1. (20 point.pdf
Β 
please answer this multiple choice question2.What major Department.pdf
please answer this multiple choice question2.What major Department.pdfplease answer this multiple choice question2.What major Department.pdf
please answer this multiple choice question2.What major Department.pdf
Β 
please answer this multiple choice question4.This Technical Review.pdf
please answer this multiple choice question4.This Technical Review.pdfplease answer this multiple choice question4.This Technical Review.pdf
please answer this multiple choice question4.This Technical Review.pdf
Β 
Please answer question in full 2.1 Security management function.pdf
Please answer question in full  2.1 Security management function.pdfPlease answer question in full  2.1 Security management function.pdf
Please answer question in full 2.1 Security management function.pdf
Β 
Please answer these questions 150 words only 1) List four lines o.pdf
Please answer these questions 150 words only  1) List four lines o.pdfPlease answer these questions 150 words only  1) List four lines o.pdf
Please answer these questions 150 words only 1) List four lines o.pdf
Β 
Please answer the 4 questions at the end please do all of them at th.pdf
Please answer the 4 questions at the end please do all of them at th.pdfPlease answer the 4 questions at the end please do all of them at th.pdf
Please answer the 4 questions at the end please do all of them at th.pdf
Β 
Please answer the questions in orderExplain why a virus is co.pdf
Please answer the questions in orderExplain why a virus is co.pdfPlease answer the questions in orderExplain why a virus is co.pdf
Please answer the questions in orderExplain why a virus is co.pdf
Β 
Please answer the questions in orderCompare and contrast Virus a.pdf
Please answer the questions in orderCompare and contrast Virus a.pdfPlease answer the questions in orderCompare and contrast Virus a.pdf
Please answer the questions in orderCompare and contrast Virus a.pdf
Β 
Please answer the questions in ordarExplain the advantage for sur.pdf
Please answer the questions in ordarExplain the advantage for sur.pdfPlease answer the questions in ordarExplain the advantage for sur.pdf
Please answer the questions in ordarExplain the advantage for sur.pdf
Β 
Please give a detailed summary of the article where I can write at l.pdf
Please give a detailed summary of the article where I can write at l.pdfPlease give a detailed summary of the article where I can write at l.pdf
Please give a detailed summary of the article where I can write at l.pdf
Β 
Please answer the 4 questions at the end Causes and Outcomes of Co.pdf
Please answer the 4 questions at the end Causes and Outcomes of Co.pdfPlease answer the 4 questions at the end Causes and Outcomes of Co.pdf
Please answer the 4 questions at the end Causes and Outcomes of Co.pdf
Β 
please help In multiple regression analysis, residuals (YY^) shou.pdf
please help  In multiple regression analysis, residuals (YY^) shou.pdfplease help  In multiple regression analysis, residuals (YY^) shou.pdf
please help In multiple regression analysis, residuals (YY^) shou.pdf
Β 
please fill in the chart A company that uses job order c.pdf
please fill in the chart A company that uses job order c.pdfplease fill in the chart A company that uses job order c.pdf
please fill in the chart A company that uses job order c.pdf
Β 
Please fill in the code to run the program based on the following in.pdf
Please fill in the code to run the program based on the following in.pdfPlease fill in the code to run the program based on the following in.pdf
Please fill in the code to run the program based on the following in.pdf
Β 
Please explain fully each step and what inputs and the number of row.pdf
Please explain fully each step and what inputs and the number of row.pdfPlease explain fully each step and what inputs and the number of row.pdf
Please explain fully each step and what inputs and the number of row.pdf
Β 
please explain how attaker crack 2DES and show your work. 3DES = Run.pdf
please explain how attaker crack 2DES and show your work. 3DES = Run.pdfplease explain how attaker crack 2DES and show your work. 3DES = Run.pdf
please explain how attaker crack 2DES and show your work. 3DES = Run.pdf
Β 
Please explain answer choice 26. Small functionally disruptive hardw.pdf
Please explain answer choice 26. Small functionally disruptive hardw.pdfPlease explain answer choice 26. Small functionally disruptive hardw.pdf
Please explain answer choice 26. Small functionally disruptive hardw.pdf
Β 
Please explain answer choice 15. Match each of the following counter.pdf
Please explain answer choice 15. Match each of the following counter.pdfPlease explain answer choice 15. Match each of the following counter.pdf
Please explain answer choice 15. Match each of the following counter.pdf
Β 
Please draw an activity diagram using Enterprise. or draw using symb.pdf
Please draw an activity diagram using Enterprise. or draw using symb.pdfPlease draw an activity diagram using Enterprise. or draw using symb.pdf
Please draw an activity diagram using Enterprise. or draw using symb.pdf
Β 
please do the extr.pdf
please do the extr.pdfplease do the extr.pdf
please do the extr.pdf
Β 

Recently uploaded

Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
Β 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
Β 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
Β 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
Β 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
Β 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
Β 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
Β 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
Β 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
Β 
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈcall girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ9953056974 Low Rate Call Girls In Saket, Delhi NCR
Β 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
Β 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
Β 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
Β 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
Β 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
Β 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
Β 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
Β 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
Β 

Recently uploaded (20)

Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
Β 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
Β 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
Β 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
Β 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
Β 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
Β 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
Β 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
Β 
Model Call Girl in Tilak Nagar Delhi reach out to us at πŸ”9953056974πŸ”
Model Call Girl in Tilak Nagar Delhi reach out to us at πŸ”9953056974πŸ”Model Call Girl in Tilak Nagar Delhi reach out to us at πŸ”9953056974πŸ”
Model Call Girl in Tilak Nagar Delhi reach out to us at πŸ”9953056974πŸ”
Β 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
Β 
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈcall girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ
call girls in Kamla Market (DELHI) πŸ” >ΰΌ’9953330565πŸ” genuine Escort Service πŸ”βœ”οΈβœ”οΈ
Β 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
Β 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
Β 
Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"Rapple "Scholarly Communications and the Sustainable Development Goals"
Rapple "Scholarly Communications and the Sustainable Development Goals"
Β 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
Β 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
Β 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
Β 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
Β 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
Β 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
Β 

Please fix pygame code so that the first Enemy bounces off the wal.pdf

  • 1. Please fix pygame code so that the first "Enemy" bounces off the walls of the screen. Thank You. import pygame import sys import math import random def pixel_collision(mask1, rect1, mask2, rect2): offset_x = rect2[0] - rect1[0] offset_y = rect2[1] - rect1[1] overlap = mask1.overlap(mask2, (offset_x, offset_y)) return overlap class Sprite: def __init__(self, image, x, y): self.image = image self.rectangle = image.get_rect() self.rectangle.center = (x, y) self.mask = pygame.mask.from_surface(image) def set_position(self, new_position): self.rectangle.center = new_position def draw(self, screen): screen.blit(self.image, self.rectangle) def is_colliding(self, other_sprite): return pixel_collision(self.mask, self.rectangle, other_sprite.mask, other_sprite.rectangle) class Enemy(Sprite): def __init__(self, image, width, height): super().__init__(image, width, height) self.rectangle.centerx = random.randint(0, width) self.rectangle.centery = random.randint(0, height) vx = random.uniform(-1, 1) vy = random.uniform(-1, 1) self.speed = (vx, vy) def move(self): self.rectangle.move_ip(self.speed) def bounce(self, width, height):
  • 2. if self.rectangle.left < 0 or self.rectangle.right > width: self.speed = (-self.speed[0], self.speed[1]) if self.rectangle.top < 0 or self.rectangle.bottom > height: self.speed = (self.speed[0], -self.speed[1]) class Enemy2(Sprite): def __init__(self, image, width, height): super().__init__(image, width, height) self.speed = (0, 0) self.acceleration = 0.1 def move(self, target_position): dx = target_position[0] - self.rectangle.centerx dy = target_position[1] - self.rectangle.centery dist = math.sqrt(dx ** 2 + dy ** 2) if dist != 0: dx_norm = dx / dist dy_norm = dy / dist self.speed = (self.speed[0] + dx_norm * self.acceleration, self.speed[1] + dy_norm * self.acceleration) self.rectangle.move_ip(self.speed[0], self.speed[1]) class PowerUp(Sprite): def __init__(self, image, width, height): super().__init__(image, width, height) def main(): pygame.init() myfont = pygame.font.SysFont('monospace', 24) width, height = 600, 400 screen = pygame.display.set_mode((width, height)) enemy1 = pygame.image.load("devil.png").convert_alpha() enemy2 = pygame.image.load("ghost.png").convert_alpha() enemy1_image = pygame.transform.smoothscale(enemy1, (50, 50)) enemy2_image = pygame.transform.smoothscale(enemy2, (50, 50)) enemy_sprites = [] enemy2_sprites = [] for i in range(5): enemy_sprites.append(Enemy(enemy1_image, width, height)) for i in range(2):
  • 3. enemy2_sprites.append(Enemy2(enemy2_image, width, height)) player_image = pygame.image.load("prayerhands.jpeg").convert_alpha() player_image = pygame.transform.scale(player_image, (50, 50)) x, y = 0, 0 player_sprite = Sprite(player_image, x, y) life = 3 powerup_image = pygame.image.load("life_powerup.jpeg").convert_alpha() powerups = [] is_playing = True clock = pygame.time.Clock() while is_playing: clock.tick(60) if life <= 0: is_playing = False for event in pygame.event.get(): if event.type == pygame.QUIT: is_playing = False pos = pygame.mouse.get_pos() player_sprite.set_position(pos) for enemy_sprite in enemy_sprites: if player_sprite.is_colliding(enemy_sprite): life -= 0.2 for powerup_sprite in powerups: if player_sprite.is_colliding(powerup_sprite): life += 1 powerups = [p for p in powerups if not player_sprite.is_colliding(p)] for enemy_sprite in enemy_sprites: enemy_sprite.move() enemy_sprite.bounce(width, height) for enemy2_sprite in enemy2_sprites: enemy2_sprite.move(player_sprite.rectangle.center) if random.random() < 0.01: powerups.append(PowerUp(powerup_image, random.randint(0, width), random.randint(0, height))) screen.fill((255, 255, 255)) for enemy_sprite in enemy_sprites:
  • 4. enemy_sprite.draw(screen) for enemy2_sprite in enemy2_sprites: enemy2_sprite.draw(screen) for powerup_sprite in powerups: powerup_sprite.draw(screen) player_sprite.draw(screen) text = "Life: " + str('%.1f' % life) label = myfont.render(text, True, (0, 0, 0)) screen.blit(label, (20, 20)) pygame.display.update() pygame.time.wait(2000) pygame.quit() sys.exit() if __name__ == "__main__": main()