0

So I wrote a mini game program where a penguin should catch the fishes falling from above. When the "Missed" score reaches 5, I want the "Game Over" page to show up. The page has the "close" button for obvious reasons. Can you please help to correct the program? I think the issue is with updating the game but I'm not sure. Can't understand why it shows black screen then closes.

import pygame
import random


pygame.init()

# game window
screen_width = 600
screen_height = 800
game_window = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Penguin Fish Catcher")

# Set up the colors
white = (255, 255, 255)
black = (0, 0, 0)

# penguin
penguin_img = pygame.image.load("penguin.png")
penguin_width = 35
penguin_height = 45
penguin_img = pygame.transform.scale(penguin_img, (penguin_width, penguin_height))
penguin_x = screen_width // 2 - penguin_width // 2
penguin_y = screen_height - penguin_height

# fish
fish_img = pygame.image.load("fish.png")
fish_width = 25
fish_height = 25
fish_img = pygame.transform.scale(fish_img, (fish_width, fish_height))
fish_x = random.randint(0, screen_width - fish_width)
fish_y = -fish_height
fish_speed = 0.4

# Set up the font for the score
font = pygame.font.Font(None, 36)
caught_score = 0
missed_score = 0

# text and close button
game_over_text = font.render("Game Over", True, black)
game_over_rect = game_over_text.get_rect(center=(screen_width // 2, screen_height // 2 - 20))
close_button_text = font.render("Close", True, black)
close_button_rect = close_button_text.get_rect(center=(screen_width // 2, screen_height // 2 + 20))

# game loop
game_running = True
while game_running:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # Check if the "Close" button was clicked
            if close_button_rect.collidepoint(event.pos):
                game_running = False

    # Move the penguin based on arrow key input
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        penguin_x -= 1
    elif keys[pygame.K_RIGHT]:
        penguin_x += 1

    # Move the fish down the screen
    fish_y += fish_speed

    # Check if the fish has gone off the bottom of the screen
    if fish_y > screen_height:
        fish_x = random.randint(0, screen_width - fish_width)
        fish_y = -fish_height
        missed_score += 1

    # Check if the penguin has caught the fish
    if penguin_x < fish_x + fish_width and penguin_x + penguin_width > fish_x \
            and penguin_y < fish_y + fish_height and penguin_y + penguin_height > fish_y:
        fish_x = random.randint(0, screen_width - fish_width)
        fish_y = -fish_height
        caught_score += 1

    # End the game if the missed score reaches 5
    if missed_score >= 5:
        game_running = False
        game_window.fill(white)
        game_window.blit(game_over_text, game_over_rect)
        game_window.blit(close_button_text, close_button_rect)
        pygame.display.update()
        # pygame.time.wait(2000)

    # Draw the game elements
    else:
        game_window.fill(white)
        game_window.blit(penguin_img, (penguin_x, penguin_y))
        game_window.blit(fish_img, (fish_x, fish_y))
        caught_score_text = font.render(f"Caught: {caught_score}", True, black)
        game_window.blit(caught_score_text, (10, 10))
        missed_score_text = font.render(f"Missed: {missed_score}", True, black)
        game_window.blit(missed_score_text, (screen_width - 120, 10))
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Jose
  • 21
  • 4
  • you could put what you have inside the game loop now into a separate function `gameloopifplaying()`, and write some other function `gameloopifover()`. And then have `while game_running: if gamenotover: gameloopifplaying() else: gameloopifover()` – Jord van Eldik Apr 20 '23 at 22:04
  • also, i would divide your game loop into functions. e.g. `handleevents()`, `movepenguin()`, etc. this makes your code more structured, making it more readeable and easier to debug – Jord van Eldik Apr 20 '23 at 22:08

0 Answers0