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()