Connorelsea
Connorelsea

Reputation: 2438

Integer variables not updating in python

At the start of my code I make two variables

WINDOW_WIDTH = 400
WINDOW_HEIGHT = 300

Later, in def main(), I change them if an event occurs in the following manner

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()

The print statement below the change indicates that a change has been made. Then in drawArena() I do the following:

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

But the window height and window width are unchanged, and have the same values as when first initialized.

Upvotes: 1

Views: 131

Answers (1)

milad nasr
milad nasr

Reputation: 86

You need to state that these variables are global

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()

Upvotes: 5

Related Questions