Home > Net >  Pygame gameloop won't restart
Pygame gameloop won't restart

Time:03-04

I have a question regarding the restart and quit option. Whenever I press q after hitting the "wall" it quits the program as intended, however the c button doesn't seem to restart the gameloop() but I can't find the reason why.

I did try adding game_over and game_close = False in that same line, however that did not seem to fix the problem. I also tried to add Else gameloop() which did not solve it either.

import pygame 
import time

pygame.init()

dis_width = 800
dis_height  = 600

dis=pygame.display.set_mode((dis_width,dis_height))
pygame.display.set_caption('Snake delux')

#define colors
blue=(0,0,255)
red=(255,0,0)
white = (255, 255, 255)
black = (0, 0, 0)

#starting values
x1_change = 0       
y1_change = 0
x1 = dis_width/2
y1 = dis_height/2
#game parameters
snake_speed = 144
snake_block = 10
font_style = pygame.font.SysFont(None, 30)
game_over = False
game_close = False
clock = pygame.time.Clock()

def message(msg, color):
    mesg = font_style.render(msg, True, color)
    dis.blit(mesg, [dis_width/3, dis_height/3])

def gameLoop():
    global game_over 
    global game_close 
    global x1_change
    global y1_change
    global x1
    global y1
        

    while not game_over:

        while game_close == True:
            dis.fill(black)
            message("Press Q-Quit or C-Play Again", red)
            pygame.display.update()

            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        game_over = True
                        game_close = False
                    if event.key == pygame.K_c:
                        gameLoop()


        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x1_change = -snake_block
                    y1_change = 0
                elif event.key == pygame.K_RIGHT:
                    x1_change = snake_block
                    y1_change = 0
                elif event.key == pygame.K_UP:
                    y1_change = -snake_block
                    x1_change = 0
                elif event.key == pygame.K_DOWN:
                    y1_change = snake_block
                    x1_change = 0

        if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
            game_close = True
             
        x1  = x1_change
        y1  = y1_change

        pygame.draw.rect(dis, blue, [x1, y1, snake_block, snake_block])
        
        pygame.display.update()
        clock.tick(snake_speed)
     
    pygame.quit()
    quit()

gameLoop()

CodePudding user response:

If you reach the line where event.key == pygame.K_c is evaluated, then game_close must be True. You recursively call into the gameLoop function if c is being held down, but you don't change the game_over or game_close values so nothing will change.

If you imagine stepping through the code while pressing 'c':

                if event.key == pygame.K_c:

This evaluates to True, so execution goes into the gameLoop function, game_close is still True, so you go right back to where you were.

I think, rather than recursively calling gameLoop() if the c key is pressed, what you need to do is change your game_close variable similar to what you do for quitting the game.

CodePudding user response:

Do not call the gameLoop recursively, but run the game in a loop. Calling the main function recursively will mess up your variables and actually result in a game running within a game.

e.g.:

def gameLoop():
    global game_end 
    # [...]
        

    while not game_over:

        while game_close == True:
            [...]

            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        game_over = True
                        game_close = False
                        game_end = True
                    if event.key == pygame.K_c:
                        game_over = True
                        game_close = False
                        game_end = False

        # [...]
game_end False
while not game_end
    x1_change = 0       
    y1_change = 0
    x1 = dis_width/2
    y1 = dis_height/2
    snake_speed = 144
    snake_block = 10
    game_over = False
    game_close = False

    gameLoop()
  • Related