【发布时间】:2020-04-04 05:26:12
【问题描述】:
我使用 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()
【问题讨论】:
-
你可能需要在等待之前在你的损失函数中调用
pygame.display.flip()