2016-10-14 6 views
1

Pygameサーフェス内の個々のピクセルをランダムな色に変更するプログラムを作成しようとしています。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!

+0

静的なテレビ画面エフェクトの場合は、元の質問とは関係ありませんが、これまでに正確にこれを行った人のドライブバイコメントは、Comp_Totの1回限りのグローバルリストを作成した方がパフォーマンスが向上しますリストの中でrandom.shuffleを一度呼び出すだけです。 このテクニックを一時的な200x200 Surfaceに適用するだけでパフォーマンスをさらに向上させ、その画像を大きな画面に並べて表示しました。 –

+0

すごく感謝! このプログラムは、エフェクトを出力するのに時間がかかりますので、私はそのショットを与えます:) – Jake

答えて

1

エラーの原因となるコードには実際に2つの問題があります。最初は、既に述べたようにif event.type == 16:の内部

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

線を有していません。

もう1つの問題は非常に小さいですが、スクリーンをいっぱいにした後にサイズを変更すると正しく動作しなくなります。つまり、画面いっぱいに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 == pygame.VIDEORESIZEevent.type == 16を変更しました。

+0

これは動作します。どうもありがとう :) – Jake

0

ウィンドウはイベントが発生のサイズを変更するには、そのサイズの新しい表面を割り当てるために再びpygame.display.set_mode((w, h))を呼び出す必要があります。それ以外の場合は、元の1300x700 Surfaceインスタンスに書き込んでいます。

+0

これはゲームの最初の実行には機能しますが、再度実行するためにリターンキーを押すと、同じ時間がかかりますしかし、それは黒い表面を表示するだけです。これは、色が計算された後にサーフェスのサイズを変更する場合にも発生します。 アイデア – Jake

+0

event.type == 16の場合:#event type 16はウィンドウのサイズ変更イベントです Comp_X = event.dict ['size'] [0] Comp_Y = event.dict ['size'] [1] pygame.display 。(現在の計算された合計ピクセル= "+ str(Comp_Tot)) – Jake

関連する問題