I can't seem to find out why my keyboard work. I have tried changing the speed, changing the class, and swapped it out for using the mouse but I want more keyboard! No errors appear when I run it but the main player just stays in the top left and doesn't move. I've also duplicated this code with less bonus characters and it worked but once I add bonus and more levels, it fails. Any help would be greatly appreciated.
The link is for all the images and code. https://drive.google.com/drive/folders/162vsO20kRoNBy6IJa3WgSq4D5ZB5PsI2?usp=sharing
import pygame
import random
BLACK = (0, 0, 0)
pygame.init()
class Block(pygame.sprite.Sprite):
# Constructor. Pass in the color of the block,
# and its x and y position
def __init__(self, color, width, height):
# Call the parent class (Sprite) constructor
super().__init__()
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface([width, height])
self.image.fill(color)
# Fetch the rectangle object that has the dimensions of the image
# image.
# Update the position of this object by setting the values
# of rect.x and rect.y
self.rect = self.image.get_rect()
class Player(pygame.sprite.Sprite):
# Constructor. Pass in the color of the block,
# and its x and y position
def __init__(self, x, y):
# Call the parent class (Sprite) constructor
super().__init__()
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.image.load("aboriginal.png")
# Fetch the rectangle object that has the dimensions of the image
# image.
# Update the position of this object by setting the values
# of rect.x and rect.y
self.rect = self.image.get_rect()
# Make our top-left corner the passed-in location.
self.rect.x = x
self.rect.y = y
# -- Attributes
# Set speed vector
self.change_x = 0
self.change_y = 0
def changeSpeed(self, x, y):
""" Change the speed of the player"""
self.change_x += x
self.change_y += y
def update(self):
""" Find a new position for the player"""
self.rect.x += self.change_x
self.rect.y += self.change_y
class Bonus(pygame.sprite.Sprite):
# Constructor. Pass in the color of the block,
# and its x and y position
def __init__(self, pic):
# Call the parent class (Sprite) constructor
super().__init__()
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.image.load(pic)
# Fetch the rectangle object that has the dimensions of the image
# image.
# Update the position of this object by setting the values
# of rect.x and rect.y
self.rect = self.image.get_rect()
SCREEN_WIDTH = 700
SCREEN_HEIGHT = 400
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
pygame.display.set_caption(('Outback Steakhouse'))
background = pygame.image.load('winter.png')
background2 = pygame.image.load('timthumb.jpg')
background3 = pygame.image.load('winter.png')
background4 = pygame.image.load('timthumb.jpg')
background5 = pygame.image.load('rugbyStadium.png')
# sprites
block_list = pygame.sprite.Group()
bonus_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()
for i in range(10):
# This represents a block
block = Block(BLACK, 20, 15)
# Set a random location for the block
block.rect.x = random.randrange(SCREEN_WIDTH)
block.rect.y = random.randrange(SCREEN_HEIGHT)
# Add the block to the list of objects
block_list.add(block)
all_sprites_list.add(block)
# Create a player instance
player = Player(0,0)
all_sprites_list.add(player)
# Bonus instance
kiwi = Bonus('kiwi.png')
kiwi.rect.x = random.randrange(SCREEN_WIDTH - 64)
kiwi.rect.y = random.randrange(SCREEN_HEIGHT - 64)
bonus_list.add(kiwi)
all_sprites_list.add(kiwi)
done = False
clock = pygame.time.Clock()
font = pygame.font.Font(None, 36)
score = 0
level = 1
# -------- Main Program Loop -----------
while not done:
# ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
# Set the speed based on the key pressed
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.changeSpeed(-3, 0)
elif event.key == pygame.K_RIGHT:
player.changeSpeed(3, 0)
elif event.key == pygame.K_UP:
player.changeSpeed(0, -3)
elif event.key == pygame.K_DOWN:
player.changeSpeed(0, 3)
# Reset speed when key goes up
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.changeSpeed(3, 0)
elif event.key == pygame.K_RIGHT:
player.changeSpeed(-3, 0)
elif event.key == pygame.K_UP:
player.changeSpeed(0, 3)
elif event.key == pygame.K_DOWN:
player.changeSpeed(0, -3)
# See if the player block has collided with anything including bonus
blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)
bonus_hit_list = pygame.sprite.spritecollide(player, bonus_list, True)
# Check the list of collisions.
for block in blocks_hit_list:
score += 1
print(score)
for bonus in bonus_hit_list:
score += 10
print(score)
# Check to see if all the blocks are gone.
# If they are, level up.
if len(block_list) == 0 and len(bonus_list) == 0:
# Add one to the level
level += 1
# Add more blocks. How many depends on the level.
# Also, an 'if' statement could be used to change what
# happens customized to levels 2, 3, 4, etc.
for i in range(level * 10):
# This represents a block
block = Block(BLACK, 20, 15)
# Set a random location for the block
block.rect.x = random.randrange(SCREEN_WIDTH)
block.rect.y = random.randrange(SCREEN_HEIGHT)
# Add the block to the list of objects
block_list.add(block)
all_sprites_list.add(block)
for i in range(level):
if level == 1:
kiwi = Bonus('kiwi.png')
kiwi.rect.x = random.randrange(SCREEN_WIDTH - 64)
kiwi.rect.y = random.randrange(SCREEN_HEIGHT - 64)
bonus_list.add(kiwi)
all_sprites_list.add(kiwi)
if level == 2:
koala = Bonus('koala.png')
koala.rect.x = random.randrange(SCREEN_WIDTH - 64)
koala.rect.y = random.randrange(SCREEN_HEIGHT - 64)
bonus_list.add(koala)
all_sprites_list.add(koala)
if level == 3:
kangaroo = Bonus('kangaroo.png')
kangaroo.rect.x = random.randrange(SCREEN_WIDTH - 64)
kangaroo.rect.y = random.randrange(SCREEN_HEIGHT - 64)
bonus_list.add(kangaroo)
all_sprites_list.add(kangaroo)
if level == 4:
boomerang = Bonus('boomerang.png')
boomerang.rect.x = random.randrange(SCREEN_WIDTH - 64)
boomerang.rect.y = random.randrange(SCREEN_HEIGHT - 64)
bonus_list.add(boomerang)
all_sprites_list.add(boomerang)
if level == 5:
rugby = Bonus('rugby.png')
rugby.rect.x = random.randrange(SCREEN_WIDTH - 64)
rugby.rect.y = random.randrange(SCREEN_HEIGHT - 64)
bonus_list.add(rugby)
all_sprites_list.add(rugby)
# ALL GAME LOGIC SHOULD GO ABOVE THIS COMMENT
# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
# Clear the screen and add new images
if level == 1:
screen.fill(WHITE)
screen.blit(background, (0, 0))
message = font.render("Easy Peasy" , True, RED)
screen.blit(message, [300, 10])
elif level == 2:
screen.fill(WHITE)
screen.blit(background2, (0, 0))
elif level == 3:
screen.fill(WHITE)
screen.blit(background3, (0, 0))
elif level == 4:
screen.fill(WHITE)
screen.blit(background4, (0, 0))
elif level == 5:
screen.fill(WHITE)
screen.blit(background5, (0,0))
else:
pygame.quit()
# Draw all the spites
all_sprites_list.draw(screen)
text = font.render("Score: " + str(score), True, BLACK)
screen.blit(text, [10, 10])
text = font.render("Level: " + str(level), True, BLACK)
screen.blit(text, [10, 40])
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Limit to 60 frames per second
clock.tick(60)
pygame.quit()