Pygame:变量不更新每次迭代

时间:2013-12-14 19:54:17

标签: python pygame

我是Python的新手,所以这可能是一个简单的问题。我希望代码显示“rotation:”后跟变量player_rotation的值。它这样做,但显示的值不是每次迭代1(正如我所期望的那样)。

import pygame
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((480, 480))
myfont = pygame.font.SysFont("monospace", 15)
player_rotation = 0
rotation_label = myfont.render("rotation: " + str(player_rotation), 1, (255,255,0))

while 1:

    screen.blit(rotation_label, (100,100))
    player_rotation += 1
    pygame.display.flip()

for event in pygame.event.get():
    if event.type==pygame.QUIT:
        pygame.quit()
        exit(0)

3 个答案:

答案 0 :(得分:1)

您只设置一次标签,这发生在循环之前。尝试将rotation_label移动到循环中。

while 1:
    rotation_label = myfont.render("rotation: " + str(player_rotation), 1, (255,255,0))
    screen.blit(rotation_label, (100,100))
    player_rotation += 1
    pygame.display.flip()

另外,for循环中的代码永远不会执行,因为它出现在while 1

之后

答案 1 :(得分:0)

您忘记在while(1)循环中添加制表符空格。那就是:

while 1:
    screen.blit(rotation_label, (100,100))
    player_rotation += 1
    pygame.display.flip()

答案 2 :(得分:0)

您只需更新变量player_rotation,但永远不会让您重新标记它。

相关问题