整数变量没有在python中更新

时间:2014-12-24 10:53:51

标签: python pygame

在我的代码开头我创建了两个变量

WINDOW_WIDTH = 400
WINDOW_HEIGHT = 300

稍后,在def main()中,如果事件以下列方式发生,我会更改它们

while True:

    for event in pygame.event.get():
        if event.type == VIDEORESIZE:
            DISPLAY = pygame.display.set_mode(event.dict['size'], HWSURFACE | DOUBLEBUF | RESIZABLE)
            # Here, the integers are supposed to be being changed
            WINDOW_WIDTH, WINDOW_HEIGHT = DISPLAY.get_size()
            print("%d, %d" % (WINDOW_WIDTH, WINDOW_HEIGHT))

 drawArena()

更改下方的打印声明表示已进行更改。然后在drawArena()中执行以下操作:

print("Drawing (%d, %d)" % (WINDOW_WIDTH, WINDOW_HEIGHT))

但窗口高度和窗口宽度不变,并且具有与首次初始化时相同的值。

1 个答案:

答案 0 :(得分:5)

您需要声明这些变量是全局的

global WINDOW_HEIGHT,WINDOW_WIDTH
while True:
    for event in pygame.event.get():
        if event.type == VIDEORESIZE:
            DISPLAY = pygame.display.set_mode(event.dict['size'], HWSURFACE | DOUBLEBUF | RESIZABLE) # Here, the integers are supposed to be being changed
            WINDOW_WIDTH, WINDOW_HEIGHT = DISPLAY.get_size()
            print("%d, %d" % (WINDOW_WIDTH, WINDOW_HEIGHT))

drawArena()