Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please edit my pygame to do whats in the image. import pygame, random #Initialize pygame pygame.init ( ) #Set display surface WINDOW _ WIDTH =

please edit my pygame to do whats in the image. import pygame, random
#Initialize pygame
pygame.init()
#Set display surface
WINDOW_WIDTH =1200
WINDOW_HEIGHT =700
display_surface = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Space Invaders")
#Set FPS and clock
FPS =60
clock = pygame.time.Clock()
#Define Classes
class Game():
"""A class to help control and update gameplay"""
def __init__(self, player, alien_group, player_bullet_group, alien_bullet_group):
"""Initialze the game"""
#Set game values
self.round_number =1
self.score =0
self.player = player
self.alien_group = alien_group
self.player_bullet_group = player_bullet_group
self.alien_bullet_group = alien_bullet_group
#Set sounds and music
self.new_round_sound = pygame.mixer.Sound("new_round.wav")
self.breach_sound = pygame.mixer.Sound("breach.wav")
self.alien_hit_sound = pygame.mixer.Sound("alien_hit.wav")
self.player_hit_sound = pygame.mixer.Sound("player_hit.wav")
#Set font
self.font = pygame.font.Font("Facon.ttf",32)
def update(self):
"""Update the game"""
self.shift_aliens()
self.check_collisions()
self.check_round_completion()
def draw(self):
"""Draw the HUD and other information to display"""
#Set colors
WHITE =(255,255,255)
#Set text
score_text = self.font.render("Score: "+ str(self.score), True, WHITE)
score_rect = score_text.get_rect()
score_rect.centerx = WINDOW_WIDTH//2
score_rect.top =10
round_text = self.font.render("Round: "+ str(self.round_number), True, WHITE)
round_rect = round_text.get_rect()
round_rect.topleft =(20,10)
lives_text = self.font.render("Lives: "+ str(self.player.lives), True, WHITE)
lives_rect = lives_text.get_rect()
lives_rect.topright =(WINDOW_WIDTH -20,10)
#Blit the HUD to the display
display_surface.blit(score_text, score_rect)
display_surface.blit(round_text, round_rect)
display_surface.blit(lives_text, lives_rect)
pygame.draw.line(display_surface, WHITE, (0,50),(WINDOW_WIDTH, 50),4)
pygame.draw.line(display_surface, WHITE, (0, WINDOW_HEIGHT -100),(WINDOW_WIDTH, WINDOW_HEIGHT -100),4)
def shift_aliens(self):
"""Shift a wave of aliens down the screen and reverse direction"""
#Determine if alien group has hit an edge
shift = False
for alien in (self.alien_group.sprites()):
if alien.rect.left =0 or alien.rect.right >= WINDOW_WIDTH:
shift = True
#Shift every alien down, change direction, and check for a breach
if shift:
breach = False
for alien in (self.alien_group.sprites()):
#Shift down
alien.rect.y +=10*self.round_number
#Reverse the direction and move the alien off the edge so 'shift' doesn't trigger
alien.direction =-1*alien.direction
alien.rect.x += alien.direction*alien.velocity
#Check if an alien reached the ship
if alien.rect.bottom >= WINDOW_HEIGHT -100:
breach = True
#Aliens breached the line
if breach:
self.breach_sound.play()
self.player.lives -=1
self.check_game_status("Aliens breached the line!", "Press 'Enter' to continue")
def check_collisions(self):
"""Check for collisions"""
#See if any bullet in the player bullet group hit an alien in the alien group
if pygame.sprite.groupcollide(self.player_bullet_group, self.alien_group, True, True):
self.alien_hit_sound.play()
self.score +=100
#See if the player has collided with any bullet in the alien bullet group
if pygame.sprite.spritecollide(self.player, self.alien_bullet_group, True):
self.player_hit_sound.play()
self.player.lives -=1
self.check_game_status("You've been hit!", "Press 'Enter' to continue")
def check_round_completion(self):
"""Check to see if a player has completed a single round"""
#If the alien group is empty, you've completed the round
if not (self.alien_group):
self.score +=1000*self.round_number
self.round_number +=1
self.start_new_round()
def start_new_round(self):
"""Start a new round"""
#Create a grid of Aliens 11 columns and 5 rows.
for i in range(11):
for j in range(5):
alien = Alien(64+ i*64,64+ j*64, self.round_number, self.alien_bullet_group)
self.alien_group.add(alien)
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Privacy In Statistical Databases International Conference Psd 2022 Paris France September 21 23 2022 Proceedings Lncs 13463

Authors: Josep Domingo-Ferrer ,Maryline Laurent

1st Edition

3031139445, 978-3031139444

More Books

Students also viewed these Databases questions

Question

Challenges Facing Todays Organizations?

Answered: 1 week ago