我正在尝试通过遵循此tutorial学习PyGame.
当我当机时,“您已当机”文本仅出现一格,并且几乎立即消失.当我在朋友PC上运行代码时,它运行得很好.
发生的视频:Streamable link
真正应该发生的事情:玩游戏->您当机->文字出现(“您死了”)->等待2秒->游戏重置
我的代码是:
import pygame
import time
pygame.init()
display_width = 800
display_height = 600
crash = False
black = (0,0)
white = (255,255,255)
red = (255,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
希望您能提供帮助:)
最佳答案
原文链接:https://www.f2er.com/python/533142.html