Pygame没有响应变量更新

时间:2016-10-14 18:07:15

标签: python python-3.x pygame

我正在尝试构建一个程序,将Pygame表面中的每个像素更改为随机颜色。

对于固定和恒定的表面尺寸(例如1300 x 700),这是有效的,并且整个表面都填充了随机颜色,但是我正在尝试使用Pygame的pygame.RESIZABLE功能来快速调整表面大小(通过更新程序工作的计算X和Y值,每次调整表面大小时,Pygame表面只输出程序初始表面宽度和高度(在本例中为1300 x 700)内像素的随机彩色像素并且表面的其余部分保留黑色(我设置的defult背景颜色),即使我打印响应屏幕高度和宽度的变量(并且用于迭代所有像素)到程序日志,他们按预期更新。

我不明白表面是如何响应更新变量的,我不知道如何解决这个问题。

非常感谢任何帮助,欢迎任何有关我的代码的问题,谢谢!

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

Using defult height and width of 1300 x 700

Resizing surface to 1457 x 992 - not all of the surface is filled!

2 个答案:

答案 0 :(得分:1)

导致错误的代码实际上存在两个问题。第一个没有

Window = pygame.display.set_mode((Comp_X, Comp_Y), pygame.RESIZABLE)

如上所述,

if event.type == 16:内部。

另一个问题非常小但是如果在填充屏幕之后调整大小后会导致它无法正常工作,那就是在填满屏幕后你永远不会将x或y的值重置为0。

固定的代码部分:

            if event.type == pygame.VIDEORESIZE: 
                Comp_X = event.w
                Comp_Y = event.h
                Comp_Tot = Comp_X * Comp_Y
                print("current computed total pixles = " + str(Comp_Tot))
                Window = pygame.display.set_mode((Comp_X, Comp_Y), pygame.RESIZABLE)

            #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
                    x = 0
                    y = 0

我还将event.type == 16更改为event.type == pygame.VIDEORESIZE,因为它更具可读性。

答案 1 :(得分:0)

当窗口调整大小事件触发时,您需要再次调用... else { while (copy->next != NULL) { copy = copy->next; } temp->prev = copy; temp->next = NULL; copy->next = temp; } ... 以分配该大小的新曲面。否则,您仍然在写入原始的1300x700 Surface实例。