【问题标题】:Pygame not responding to variable updatesPygame 没有响应变量更新
【发布时间】:2016-10-14 18:07:15
【问题描述】:

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

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

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

非常感谢任何帮助,欢迎对我的代码提出任何问题,谢谢!

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!

【问题讨论】:

  • 与您的原始问题无关,而是作为以前做过此操作的人的路过评论(对于静态电视屏幕效果),如果您创建一个-时间全局 Comp_Tot 颜色列表,然后在列表中调用 random.shuffle 一次。我进一步提高了性能,仅将此技术应用于临时 200x200 表面,然后将该图像平铺到我的大屏幕上。
  • 很酷,谢谢!这个程序确实需要一段时间才能输出效果,所以我会试一试:)

标签: python python-3.x pygame


【解决方案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,因为它更具可读性。

【讨论】:

    【解决方案2】:

    当窗口调整大小事件触发时,您需要再次调用pygame.display.set_mode((w, h)) 以分配该大小的新表面。否则,您仍在写入原始 1300x700 Surface 实例。

    【讨论】:

    • 这适用于游戏的第一次运行,但是当我按回车键再次运行它时,它需要相同的时间来计算,但是它只是显示一个黑色的表面。如果我在计算颜色后调整表面大小,也会发生这种情况。有什么想法吗?
    • if event.type == 16: #event type 16是窗口大小调整事件 Comp_X = event.dict['size'][0] Comp_Y = event.dict['size'][1 ] pygame.display.set_mode((Comp_X, Comp_Y), pygame.RESIZABLE) Comp_Tot = Comp_X * Comp_Y print("当前计算的总像素数 = " + str(Comp_Tot))
    猜你喜欢
    • 1970-01-01
    • 2017-05-10
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多