Pygame not responding to variable updates
<p>I'm trying to build a program that will change each individual pixel within a Pygame surface to a random colour. </p>

<p>For a fixed and constant surface size (eg. 1300 x 700) this works and the whole surface is filled with random colours, however I'm trying to resize the surface with the pygame.RESIZABLE feature of Pygame on the fly (by updating the computed X and Y values that the program works from), every time the surface is resized, however, the Pygame surface only outputs randomly coloured pixels for pixels within the programs initial surface width and height (in this case 1300 x 700) and the rest of the surface is left black (the defult background colour I set), even though when I print the variables that respond to the screen height and width (and are being used to iterate through all the pixel) to the program log, they update as expected.</p>

<p>I don't understand how the surface is not responding to thees updated variables and I don't know how to fix this issue.</p>

<p>Any help is much appreciated and any questions about my code are welcome, thanks!</p>

<pre><code>import pygame
import random
pygame.init() #initiates pygame

Comp_X = 1300
Comp_Y = 700

Window = pygame.display.set_mode((Comp_X, Comp_Y), pygame.RESIZABLE) #defines the window size (the varible doesnt have to be called window)
pygame.display.set_caption("Random Colours") #sets the title of the window
clock = pygame.time.Clock() #sets the refresh rate of the program



def GameLoop():    
    global Comp_X #computed pixles for the X axis
    global Comp_Y #computed pixles for the Y axis
    Comp_Tot = Comp_X * Comp_Y #total pixles on the screen
    print("initial computed total pixles = " + str(Comp_Tot))

    #setting x and y position
    x = 0
    y = 0


    GameExit = False #sets the defult value of this varible to true

    while not GameExit: #this is effectivly a "while true" loop, unless the varible "crashed" is set to false

        for event in pygame.event.get(): #this for loop will listen for events
            print(event.type)
            print("X = " + str(Comp_X))
            print("Y = " + str(Comp_Y))

            if event.type == pygame.QUIT: # this if statement checks to see if the X in the top right of the window was pressed
                GameExit = True # this will break the while loop if the condition is met
                print("X = " + str(Comp_X))
                print("Y = " + str(Comp_Y))

            if event.type == 16: #event type 16 is the window resizing event
                Comp_X = event.dict['size'][0]
                Comp_Y = event.dict['size'][1]
                Comp_Tot = Comp_X * Comp_Y
                print("current computed total pixles = " + str(Comp_Tot))

            #Creating the colours
            if event.type == pygame.KEYDOWN:

                if event.key == pygame.K_RETURN:

                    for pixle in range (0, Comp_Tot):
                        if x == Comp_X:
                            print("Computed X = " + str(Comp_X))
                            print("Computed Y = " + str(Comp_Y))
                            x = 0
                            y += 1
                        pygame.draw.rect(Window, (random.randint(0,255), random.randint(0,255), random.randint(0,255)), [x, y, 2, 2])# draws a red square
                        x += 1


        #Drawing the frame
        pygame.display.update() #This updates the frame
        clock.tick(60) # this defines the FPS



GameLoop()
pygame.quit() # this will close the window if the "while GameLoop" loop stops running
quit()
</code></pre>

<p><a href="https://i.stack.imgur.com/gJ7lr.jpg" rel="nofollow">Using defult height and width of 1300 x 700</a></p>

<p><a href="https://i.stack.imgur.com/8pATR.jpg" rel="nofollow">Resizing surface to 1457 x 992 - not all of the surface is filled!</a></p>

<p>When the window resize event fires, you need to call <code>pygame.display.set_mode((w, h))</code> again to allocate a new surface of that size. Otherwise you're still writing to the original 1300x700 Surface instance.</p>

python python-3.x pygame