2016-10-14 65 views
1

我试图建立一个程序,将Pygame表面内的每个像素更改为随机颜色。Pygame没有响应变量更新

对于一个固定的和恒定的表面大小(例如,1300 x 700)这个工程和整个表面都是随机的颜色填充,但我试图调整pygame的pygame.RESIZABLE功能的表面(通过更新程序工作的计算出的X和Y值),每当表面被调整大小时,Pygame表面只会为程序初始表面宽度和高度中的像素输出随机颜色的像素(在此情况下为1300 x 700 ),而其余的表面是黑色的(即我设置的背景色),即使当我将响应屏幕高度和宽度的变量(并用于迭代所有像素)打印到程序日志中时,他们按预期更新。

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

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

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!

+0

与您的原始问题无关,但是作为以前完成此操作的人员(对于静态电视屏幕效果),如果您创建一次性Comp_Tot全局列表,您将获得更好的性能颜色,然后只需在列表中调用random.shuffle一次。 我通过将此技术应用于临时的200x200表面,然后将该图像平铺到我的大屏幕上,进一步提高了性能。 –

+0

很感谢! 这个程序需要一段时间来输出效果,所以我会给出一个镜头:) – Jake

回答

1

导致错误的代码实际上有两个问题。第一不具有如已经提到的if event.type == 16:内部的

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

线。

另一个问题非常小,但如果在填充屏幕后重新调整大小,则会导致其无法正常工作,也就是说,填充屏幕后永远不会将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 == 16event.type == pygame.VIDEORESIZE,因为它是更具可读性。

+0

This works!非常感谢 :) – Jake

0

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

+0

这适用于游戏的第一次运行,但是当我按下回车键再次运行时,它需要相同的时间量以计算,但它只是显示一个黑色的表面。这也发生如果我调整颜色后计算表面。 任何想法? – Jake

+0

if event.type == 16:#event type 16 is the window resizing event 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(“current calculated total pixles =”+ str(Comp_Tot)) – Jake