Update game2

main
Anna Marija Redžepova 2024-03-03 17:38:50 +00:00
parent b3b5c6e7dc
commit 5dcdc5faf1
1 changed files with 165 additions and 143 deletions

308
game2
View File

@ -112,6 +112,14 @@ def handle_character_selection():
elif event.key == pygame.K_RETURN: elif event.key == pygame.K_RETURN:
return CHARACTER_OPTIONS[selected_character_index]["image"] return CHARACTER_OPTIONS[selected_character_index]["image"]
# Function to create a button
def draw_button():
play_again_text = font.render("Play Again", True, WHITE)
play_again_rect = play_again_text.get_rect(center=(WIDTH // 2, HEIGHT // 2 + 50))
pygame.draw.rect(screen, GREEN, play_again_rect, border_radius=5)
screen.blit(play_again_text, play_again_rect)
return play_again_rect
# Main loop for character selection screen # Main loop for character selection screen
while True: while True:
screen.fill(BLACK) screen.fill(BLACK)
@ -201,168 +209,182 @@ class FallingObject:
surface.blit(self.image, (self.x, self.y)) surface.blit(self.image, (self.x, self.y))
# Main game loop # Main game loop
while not game_over and not winning: while True:
for event in pygame.event.get(): while not game_over and not winning:
if event.type == pygame.QUIT: for event in pygame.event.get():
pygame.quit() if event.type == pygame.QUIT:
sys.exit() pygame.quit()
elif event.type == pygame.KEYDOWN: sys.exit()
if event.key == pygame.K_LEFT: elif event.type == pygame.KEYDOWN:
move_left = True if event.key == pygame.K_LEFT:
elif event.key == pygame.K_RIGHT: move_left = True
move_right = True elif event.key == pygame.K_RIGHT:
elif event.type == pygame.KEYUP: move_right = True
if event.key == pygame.K_LEFT: elif event.type == pygame.KEYUP:
move_left = False if event.key == pygame.K_LEFT:
elif event.key == pygame.K_RIGHT: move_left = False
move_right = False elif event.key == pygame.K_RIGHT:
elif event.type == SPAWN_OBJECTS: move_right = False
# Randomly spawn an object (cupcake, football, or trollface) elif event.type == SPAWN_OBJECTS:
spawn_type = random.choice(["cupcake", "football", "trollface"]) # Randomly spawn an object (cupcake, football, or trollface)
if spawn_type == "cupcake": spawn_type = random.choice(["cupcake", "football", "trollface"])
falling_cupcakes.append(FallingObject(cupcake_image, CUPCAKE_WIDTH, CUPCAKE_HEIGHT, 5)) if spawn_type == "cupcake":
elif spawn_type == "football": falling_cupcakes.append(FallingObject(cupcake_image, CUPCAKE_WIDTH, CUPCAKE_HEIGHT, 5))
falling_footballs.append(FallingObject(football_image, FOOTBALL_WIDTH, FOOTBALL_HEIGHT, 5)) elif spawn_type == "football":
else: falling_footballs.append(FallingObject(football_image, FOOTBALL_WIDTH, FOOTBALL_HEIGHT, 5))
falling_trollfaces.append(FallingObject(trollface_image, TROLLFACE_WIDTH, TROLLFACE_HEIGHT, 5)) else:
falling_trollfaces.append(FallingObject(trollface_image, TROLLFACE_WIDTH, TROLLFACE_HEIGHT, 5))
# Move the paddle based on flags # Move the paddle based on flags
if move_left and paddle.left > 0: if move_left and paddle.left > 0:
paddle.x -= PADDLE_SPEED paddle.x -= PADDLE_SPEED
if move_right and paddle.right < WIDTH: if move_right and paddle.right < WIDTH:
paddle.x += PADDLE_SPEED paddle.x += PADDLE_SPEED
# Update cat position based on speed # Update cat position based on speed
cat_rect.x += cat_speed[0] cat_rect.x += cat_speed[0]
cat_rect.y += cat_speed[1] cat_rect.y += cat_speed[1]
# Check for collision with walls # Check for collision with walls
if cat_rect.left <= 0 or cat_rect.right >= WIDTH: if cat_rect.left <= 0 or cat_rect.right >= WIDTH:
cat_speed[0] = -cat_speed[0] cat_speed[0] = -cat_speed[0]
if boing_sound: if boing_sound:
boing_sound.play() boing_sound.play()
if cat_rect.top <= 0 or cat_rect.colliderect(paddle): if cat_rect.top <= 0 or cat_rect.colliderect(paddle):
cat_speed[1] = -cat_speed[1] cat_speed[1] = -cat_speed[1]
if boing_sound: if boing_sound:
boing_sound.play() boing_sound.play()
# Check for collision with bricks # Check for collision with bricks
for brick in bricks[:]: for brick in bricks[:]:
if cat_rect.colliderect(brick): if cat_rect.colliderect(brick):
bricks.remove(brick) bricks.remove(brick)
if block_sound: if block_sound:
block_sound.play() block_sound.play()
score += 1 score += 1
# Check for collision with cupcakes # Check for collision with cupcakes
for cupcake in falling_cupcakes[:]: for cupcake in falling_cupcakes[:]:
if paddle.colliderect(cupcake.x, cupcake.y, cupcake.width, cupcake.height): if paddle.colliderect(cupcake.x, cupcake.y, cupcake.width, cupcake.height):
falling_cupcakes.remove(cupcake) falling_cupcakes.remove(cupcake)
if chew_sound: if chew_sound:
chew_sound.play() chew_sound.play()
# Update cat size and speed # Update cat size and speed
cat_rect.width += 10 cat_rect.width += 10
cat_rect.height += 10 cat_rect.height += 10
cat_speed[0] += 1 cat_speed[0] += 1
cat_speed[1] += 1 cat_speed[1] += 1
# Check for collision with footballs # Check for collision with footballs
for football in falling_footballs[:]: for football in falling_footballs[:]:
if paddle.colliderect(football.x, football.y, football.width, football.height): if paddle.colliderect(football.x, football.y, football.width, football.height):
falling_footballs.remove(football) falling_footballs.remove(football)
if meow_sound: if meow_sound:
meow_sound.play() meow_sound.play()
# Update cat size and speed # Update cat size and speed
cat_rect.width -= 10 cat_rect.width -= 10
cat_rect.height -= 10 cat_rect.height -= 10
cat_speed[0] -= 1 cat_speed[0] -= 1
cat_speed[1] -= 1 cat_speed[1] -= 1
# Check for collision with trollfaces # Check for collision with trollfaces
for trollface in falling_trollfaces[:]: for trollface in falling_trollfaces[:]:
if paddle.colliderect(trollface.x, trollface.y, trollface.width, trollface.height): if paddle.colliderect(trollface.x, trollface.y, trollface.width, trollface.height):
falling_trollfaces.remove(trollface) falling_trollfaces.remove(trollface)
if rimshot_sound: if rimshot_sound:
rimshot_sound.play() rimshot_sound.play()
# Grow a random brick # Grow a random brick
brick = random.choice(bricks) brick = random.choice(bricks)
brick.width += 10 brick.width += 10
brick.height += 5 brick.height += 5
# Check for loss of life # Check for loss of life
if cat_rect.bottom >= HEIGHT: if cat_rect.bottom >= HEIGHT:
lives -= 1 lives -= 1
if fall_sound: if fall_sound:
fall_sound.play() fall_sound.play()
cat_rect.center = (WIDTH // 2, HEIGHT // 2) # Reset cat position to the center cat_rect.center = (WIDTH // 2, HEIGHT // 2) # Reset cat position to the center
if lives <= 0: if lives <= 0:
game_over = True game_over = True
# Check for winning condition # Check for winning condition
if len(bricks) == 0: if len(bricks) == 0:
winning = True winning = True
# Clear the screen # Clear the screen
screen.fill(BLACK) screen.fill(BLACK)
# Draw background image # Draw background image
screen.blit(background_image, (0, 0)) screen.blit(background_image, (0, 0))
# Draw paddle # Draw paddle
pygame.draw.rect(screen, GREEN, paddle) pygame.draw.rect(screen, GREEN, paddle)
# Draw bricks # Draw bricks
for brick in bricks: for brick in bricks:
pygame.draw.rect(screen, WHITE, brick) pygame.draw.rect(screen, WHITE, brick)
# Draw cat # Draw cat
screen.blit(cat_image, cat_rect) screen.blit(cat_image, cat_rect)
# Draw falling cupcakes # Draw falling cupcakes
for cupcake in falling_cupcakes: for cupcake in falling_cupcakes:
cupcake.update() cupcake.update()
cupcake.draw(screen) cupcake.draw(screen)
# Draw falling footballs # Draw falling footballs
for football in falling_footballs: for football in falling_footballs:
football.update() football.update()
football.draw(screen) football.draw(screen)
# Draw falling trollfaces # Draw falling trollfaces
for trollface in falling_trollfaces: for trollface in falling_trollfaces:
trollface.update() trollface.update()
trollface.draw(screen) trollface.draw(screen)
# Draw heart bar representing lives # Draw heart bar representing lives
for i in range(lives): for i in range(lives):
draw_heart(screen, RED, (10 + i * 30, 10)) # Adjust the heart position and color as needed draw_heart(screen, RED, (10 + i * 30, 10)) # Adjust the heart position and color as needed
# Draw score # Draw score
score_text = font.render(f"Score: {score}", True, PURPLE) score_text = font.render(f"Score: {score}", True, PURPLE)
screen.blit(score_text, (10, 50)) screen.blit(score_text, (10, 50))
# Update the display # Update the display
pygame.display.flip()
# Cap the frame rate
pygame.time.Clock().tick(60)
# Play appropriate sound and display text
if game_over:
if death_sound:
death_sound.play()
screen.blit(game_over_text, game_over_rect)
elif winning:
if win_sound:
win_sound.play()
screen.blit(winning_text, winning_rect)
# Draw play again button
play_again_rect = draw_button()
pygame.display.flip() pygame.display.flip()
# Cap the frame rate # Check for button click
pygame.time.Clock().tick(60) for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
# Play appropriate sound and display text if play_again_rect.collidepoint(event.pos):
if game_over: # Reset the game state
if death_sound: game_over = False
death_sound.play() winning = False
screen.blit(game_over_text, game_over_rect) score = 0
elif winning: lives = 9
if win_sound: cat_rect.center = (WIDTH // 2, HEIGHT // 2)
win_sound.play() bricks.clear()
screen.blit(winning_text, winning_rect) for row in range(BRICK_ROWS):
for col in range(BRICK_COLS):
# Update the display brick = pygame.Rect(col * (BRICK_WIDTH + 5), row * (BRICK_HEIGHT + 5), BRICK_WIDTH, BRICK_HEIGHT)
pygame.display.flip() bricks.append(brick)
falling_cupcakes.clear()
# Wait for a few seconds before quitting falling_footballs.clear()
pygame.time.wait(5000) falling_trollfaces.clear()
# Quit Pygame
pygame.quit()
sys.exit()