Pygame在尝试打印tex时不更新显示

2024-06-28 11:42:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图通过遵循这个tutorial来学习PyGame。 当我崩溃时,“You've crash”文本只显示一次,几乎立即消失。当我在我朋友的电脑上运行代码时,它运行得非常好。你知道吗

它发生的视频:Streamable link

实际情况:玩游戏->;你崩溃->;出现文本(“你死了”)->;等待2秒->;游戏重置

我的代码是:

import pygame
import time

pygame.init()

display_width = 800
display_height = 600
crash = False

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('lil spacey')
clock = pygame.time.Clock()

characterImage = pygame.transform.scale(pygame.image.load('Longcat.jpg'), (100,100))

spriteWidth = pygame.Surface.get_width(characterImage)
spriteHeight = pygame.Surface.get_height(characterImage)

def spriteCharacter(x, y):
    gameDisplay.blit(characterImage, (x, y))

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def messageDisplay(text):
    largeText = pygame.font.Font('freesansbold.ttf', 115)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2), (display_height/2))
    gameDisplay.blit(TextSurf, TextRect)
    pygame.display.update()
    time.sleep(2)
    gameLoop()

def crash():
    messageDisplay('You dead')

def gameLoop():
    x = (display_width * 0.45)
    y = (display_height * 0.8)

    x_change = 0
    y_change = 0

    gameExit = False

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                elif event.key == pygame.K_RIGHT:
                    x_change = 5
                elif event.key == pygame.K_UP:
                    y_change = -5
                elif event.key == pygame.K_DOWN:
                    y_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0
                elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    y_change = 0

        x += x_change
        y += y_change

        gameDisplay.fill(white)
        spriteCharacter(x,y)

        if x > display_width - spriteWidth or x < 0:
            crash()
        elif y > display_height - spriteHeight or y < 0:
            crash()

        pygame.display.update()
        clock.tick(60)

gameLoop()
pygame.quit()
quit()

我试过的:

  • 重新安装的Python
  • 重新安装的Pygame

版本:

  • Python:3.7.2版
  • Pygame:1.9.4版
  • 操作系统:macOS Mojave

希望你能帮忙:)


Tags: keytextgteventifdefdisplaycrash
1条回答
网友
1楼 · 发布于 2024-06-28 11:42:16

当您不让pygame处理事件时,调用pygame.display.update()后窗口可能无法重新绘制。你知道吗

您可以在pygame.display.update()time.sleep(2)之间调用pygame.event.get(),但您应该尝试遵循以下规则:

  • 只有一个主回路
  • 每帧调用pygame.display.update一次
  • 永远不要调用time.sleep或任何其他长时间运行的阻塞函数
  • 不要从主循环函数调用主循环函数

以防止此类问题。你知道吗

当崩溃发生时,你应该改变游戏的状态来反映这个事实,然后计算时间直到2秒过去,然后再次改变状态。你知道吗

相关问题 更多 >