Question
why i can't play this game. that's the errors i got Traceback (most recent call last): File /Users/alexlarose/Documents/sky_dodge.py, line 184, in player = Player() File
why i can't play this game. that's the errors i got
Traceback (most recent call last): File "/Users/alexlarose/Documents/sky_dodge.py", line 184, in
# Import pygame module
import pygame
import sys
# Import random import random
# access to key coordinnates
from pygame.locals import ( RLEACCEL,
K_UP,
K_DOWN,
K_LEFT,
K_RIGHT,
K_ESCAPE,
KEYDOWN,
QUIT,
)
#Define the constants width and height
SCREEN_WIDTH = 900
SCREEN_HEIGHT = 700
# create a create a player object by extending pygame.sprite
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.surf = pygame.image.load("images/jet.png").convert()
self.surf.set_colorkey((255, 255, 255), RLEACCEL)
self.rect = self.surf.get_rect()
# Move the sprite based user keypresses
def update(self, pressed_keys):
if pressed_keys[K_UP]:
self.rect.move_ip(0, -5)
if pressed_keys[K_DOWN]:
self.rect.move_ip(0, 5)
if pressed_keys[K_LEFT]:
self.rect.move_ip(-5, 0)
if pressed_keys[K_RIGHT]:
self.rect.move_ip(5, 0)
# Keep player on the screen
if self.rect.left < 0: self.rect.left = 0 if self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH
if self.rect.top <= 0:
self.rect.top = 0
if self.rect.bottom >= SCREEN_HEIGHT:
self.rect.bottom = SCREEN_HEIGHT
# keep player on the screen if self.rect.left < 0: self.rect.left = 0 if self.rect.right > SCREEN_WIDTH: self.rect.right = SCREEN_WIDTH if self.rect.top <= 0: self.rect.top = 0 if self.rect.bottom >= SCREEN_HEIGHT: self.rect.bottom = SCREEN_HEIGHT
# create a class to define the enemy class Enemy(pygame.sprite.Sprite): def __init__(self): super(Enemy, self).__init__() self.surf = pygame.image.load("images/missile.png").convert() self.surf.set_colorkey((225, 255, 255), RLEACCEL) self.rect = self.surf.get_rect( center=( random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100), random.randint(0, SCREEN_HEIGHT),
)
) self.speed = random.randint(5, 20)
# Move the sprite def update(self): self.rect.move_ip(-self.speed, 0) if self.rect.rigth < 0: self.kill()
# Create a class name cloud to display the cloud class Cloud(pygame.sprite.Sprite): def __init__(self): super(Cloud, self).__init__() self.surf = pygame.image.load("cloud.png").convert() self.surf.set_colorkey((0, 0, 0), RLEACCEL)
# Starting position self.rect = self.surf.get_rect( center =( random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100), random.randint(0, SCREEN_HEIGHT), ) )
# Remove the clouds when it passes def update(self): self.rect.move_ip(-5, 0) if self.rect.right < 0: self.kill()
# Initialize the pygame
pygame.init()
# create the size of the constants
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# create event to add a new enemy ADDENEMY = pygame.USEREVENT + 1 pygame.time.set_timer(ADDENEMY, 250) ADDCLOUD = pygame.USEREVENT + 2 pygame.time.set_timer(ADDCLOUD, 1000)
# Instantiate player
player = Player()
# create group to hold the enemy enemies = pygame.sprite.Group() clouds = pygame.sprite.Group() all_sprites = pygame.sprite.Group() all_sprites.add(player)
# create variable to keep the main loop
running = True
# Main loop
while running:
for event in pygame.event.get():
#Did the user hit a a key
if event.type == KEYDOWN:
print(f"{event.key}")
# if the key escape, press stop if event.key == K_ESCAPE: running = false
# click the window to close buttom elif event.type == QUIT: running = False
elif event.type == ADDENEMY: # create new enemy new_enemy = Enemy() enemies.add(new_enemy) all_sprites.add(new_enemy)
# add cloud elif event.type == ADDCLOUD: # create new cloud new_cloud = Cloud() clouds.add(new_cloud) all_sprites.add(new_cloud)
# Get the set of keys pr
pressed_keys = pygame.key.get_pressed() player.update(pressed_keys)
#update enemy position and clouds enemies.update() clouds.update()
# Fill the screen with black
screen.fill((135, 206, 250))
# Draw sprite for entity in all_sprites: screen.blit(entity.surf, entity.rect)
#check for colission if pygame.sprite.spritecollideany(player, enemies): # if so remove the player player.kill() running = False
# Update the display
pygame.display.flip()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started