1

I use vscode (If that helps) I just simply want a game over text to pop up after the player goes out of the screen, but when the player does go out of bounds nothing happens! I get no error code. This little project is just a little green square moving around but with boundaries, and upon colliding with those boundaries, the player shall lose.

When reading the code I'd suggest looking for the end and the loss function.

import pygame
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode([1000, 1000])
pygame.display.set_caption("TicTac")

x, y = 490, 500
width, height = 25, 25
vol = 25
run = True
left = False
right = False
up = False
down = False

font = pygame.font.Font('freesansbold.ttf', 32) 

def loss(msg, color):
    screen_text = font.render(msg, False, color)
    screen.blit(screen_text, [500, 500])

def redraw():
    # clear dispalydisplay_surface = pygame.display.set_mode((X, Y )) 
    screen.fill((0, 0, 0))  
    # draw the scene
    pygame.draw.rect(screen, (0, 255, 0), (x, y, width, height))

    # update display
    pygame.display.flip() 
start_ticks=pygame.time.get_ticks()

while run:
    pygame.time.delay(100)
    seconds=(pygame.time.get_ticks()-start_ticks)/1000
    # handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        events = pygame.event.get()
       if event.type == pygame.KEYDOWN:

            if event.key == pygame.K_LEFT:
                x -= vol
                left = True
                right = False
                up = False
                down = False
                width += 25

            elif event.key == pygame.K_RIGHT:
                x += vol
                left = False
                right = True
                up = False
                down = False
                width += 25

            elif event.key == pygame.K_UP:
                y -= vol
                left = False
                right = False
                up = True
                down = False
                height += 25

            elif event.key == pygame.K_DOWN:
                y += vol
                height += 25                
                left = False
                right = False
                up = False
                down = True 


    #Loss Zone
    if x >= 1000 or y >= 1000 or x <= 0 or y <= 0:
        loss("GAME OVER", (250, 0, 0))
        pygame.time.delay(5000)
        run = False 
    redraw()
    vol = 25
    width, height = 25, 25
    print(seconds)
pygame.quit()
Abuzz
  • 73
  • 6

2 Answers2

1

You are drawing the text on the screen, then clearing the screen. Simply move the redraw() above where you call loss()

redraw()
if x >= 1000 or y >= 1000 or x <= 0 or y <= 0:
        loss("GAME OVER", (250, 0, 0))
        pygame.time.delay(5000)
        run = False 
vol = 25
width, height = 25, 25
print(seconds)
The Big Kahuna
  • 2,097
  • 1
  • 6
  • 19
1

Don't use pygame.time.delay() in the game loop. Add a state game_over. Set the state when the game is over:

game_over = x >= 1000 or y >= 1000 or x <= 0 or y <= 0

And do different things in the main application loop dependent on the state:

if not game_over:
    # draw game scene
else:
    # draw game over screen

See the example:

game_over = False
while run:
    pygame.time.delay(100)
    seconds=(pygame.time.get_ticks()-start_ticks)/1000
    # handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if not game_over:
                if event.key == pygame.K_LEFT:
                    x -= vol; width += 25
                    left, right, up, down = True, False, False, False
                elif event.key == pygame.K_RIGHT:
                    x += vol; width += 25
                    left, right, up, down = False, True, False, False
                elif event.key == pygame.K_UP:
                    y -= vol; height += 25
                    left, right, up, down = False, False, True, False
                elif event.key == pygame.K_DOWN:
                    y += vol; height += 25                
                    left, right, up, down = False, False, False, True

    if not game_over:
        game_over = x >= 1000 or y >= 1000 or x <= 0 or y <= 0
        if game_over:
            game_over_time = pygame.time.get_ticks()
        redraw()
        vol, width, height = 25, 25, 25

    else: 
        if pygame.time.get_ticks() > game_over_time + 5000:
            run = False       
        screen.fill((0, 0, 0)) 
        loss("GAME OVER", (250, 0, 0))
        pygame.display.flip() 
Rabbid76
  • 202,892
  • 27
  • 131
  • 174