Pygame 渲染文本,不显示任何内容

Pygame rendering text, doesn't show anything

我使用 vscode(如果有帮助的话)我只是想在玩家离开屏幕后弹出一个游戏结束文本,但是当玩家离开屏幕时什么也没有发生!我没有收到错误代码。这个小项目只是一个四处移动但有边界的绿色小方块,一旦与这些边界发生碰撞,玩家就会输。

阅读代码时,我建议寻找结尾和损失函数。

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

您正在屏幕上绘制文本,然后清除屏幕。只需将 redraw() 移动到您调用 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)

不要在游戏循环中使用pygame.time.delay()。添加状态 game_over。设置游戏结束时的状态:

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

并根据状态在主应用程序循环中做不同的事情:

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

看例子:

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