1

I'm trying to make it so when the game over screen shows the user can press space to get back into the game. Currently, when a game over happens, it displays the game over screen but accepts no input or at least doesn't do anything with the input. For some context, the game is basically about moving left and right to avoid obstacles. Currently, I only have one obstacle, but I just have not gotten to that yet. Thanks!

import pygame
import random
import math

pygame.init()

screenWidth = 700
screenHeight = 800

x = screenWidth / 2
y = (screenHeight / 4) * 3
width = 50
height = 50

win = pygame.display.set_mode((screenWidth, screenHeight))
pygame.display.set_caption("Test Game")

bg = pygame.image.load("background.png").convert()
gameover = pygame.image.load("gameover.png").convert()
bgx = (screenWidth / 6) * 2
bgy = 0

clock = pygame.time.Clock()

class enemy():
    def __init__(self,c,y,width,height):
        self.c = c
        self.y = y
        self.width = width
        self.height = height
        self.vel = 5

    def draw(self, win):
        if self.c == 1:
            self.x = 250
            #250
        elif self.c == 2:
            self.x = 350
            #350
        else:
            self.x = 450
            #450
        self.y += self.vel
        pygame.draw.rect(win, (0,0,255), (self.x,self.y,self.width,self.height))

evil = enemy(random.randint(1,3),0,50,50)

#def redrawGameWindow():
   # evil.draw(win)

   # pygame.display.update()

running = True
gameOver = False

while running:
    clock.tick(60)
    while gameOver:
        win.blit(gameover, (0,0))
        pygame.display.update()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
                pygame.quit()
            if pygame.event.type == pygame.KEYUP:
                if event.key == pygame.K_SPACE:
                    gameOver = True




    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            pygame.quit()

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                x+=100
            if event.key == pygame.K_LEFT:
                x-=100

    win.fill((0,0,0))
    win.blit(bg, (bgx, bgy))
    evil.draw(win)
    dist = math.hypot(evil.x - x, evil.y - y)
    if dist <= 50:
            print("Game Over!")
            running = False
            gameOver = True
    pygame.draw.rect(win, (255,0,0), (x,y,width,height))
    pygame.display.update()
    #redrawGameWindow()

while gameOver:
    win.blit(gameover, (0,0))
    pygame.display.update()

    keys = pygame.key.get_pressed()

    if keys[pygame.K_SPACE]:
        gameOver = False

pygame.quit()

1 Answers1

0

The main problem is that your game over scene is the loop behind the main while running: loop and there's no way to go back to the first loop when you reach it. When you touch the evil object, you set running = False, leave the main and enter the while gameOver: loop.

In this loop you also need to call pygame.event.pump(), otherwise the pygame.key.get_pressed() function doesn't work correctly and the window freezes after a while because the events are not handled. If you want to restart the game, you should rather use only the nested while gameOver loop. Actually, I would recommend that you restructure the scenes even more and use a finite state machine instead (here's an answer in which I use functions as scenes but check out the link in the comment below as well).

Here's a working version of your code. I've changed a few things and added comments to explain the changes.

while running:
    # -----The game over scene.-----
    while gameOver:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                # pygame.quit only uninitializes the pygame modules and
                # doesn't quit the program.
                pygame.quit()
                # This will quit the whole program. You need to import sys.
                sys.exit()
            elif event.type == pygame.KEYUP:  # event.type not pygame.event.type
                if event.key == pygame.K_SPACE:
                    # Change it to False to break out of the loop.
                    gameOver = False
                    # Reset the game. You could reset the position of the
                    # `evil` object or instantiate a new one.
                    evil.x = 350
                    evil.y = 0

        win.blit(gameover, (0,0))
        pygame.display.update()
        clock.tick(60)  # You need to call tick in this loop as well.
    # ------------------------------
    # -----The main scene.-----
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                x += 100
            elif event.key == pygame.K_LEFT:
                x -= 100

    win.fill((0,0,0))
    win.blit(bg, (bgx, bgy))
    evil.draw(win)
    dist = math.hypot(evil.x - x, evil.y - y)
    if dist <= 50:
        print("Game Over!")
        # running = False  # This would stop the main loop.
        gameOver = True
    pygame.draw.rect(win, (255,0,0), (x,y,width,height))
    pygame.display.update()
    clock.tick(60)


# The other while loop was removed.
pygame.quit()
skrx
  • 19,980
  • 5
  • 34
  • 48