1

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.

3
  • 6
    Maybe you forgot to declare the variables as global in the code that changes them? Commented Dec 24, 2014 at 10:55
  • @FrédéricHamidi This fixed it. Thank you. I did not know about this feature of python, as I am new to the language. Commented Dec 24, 2014 at 10:58
  • It's considered good style in Python (and most other languages) to avoid using globals when practical, as they interfere with modularity. But using globals for things like window size is tolerable, I guess. Commented Dec 24, 2014 at 11:00

1 Answer 1

5

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.