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