2017-12-09 6 views
0

私は何もしないで黒から赤に変わる矩形を描画したいと思います。下のコードは、画面を埋める必要があります黒い矩形を1つ描画し、それを消去し、赤い四角形を描画し、ループを維持します。しかし、私はちょうど1つの赤い矩形を得る。私は間違って何をしていますか?何もせずに矩形の色を変える方法は?

import pygame 

# Define some colors 
BLACK = (0, 0, 0) 
WHITE = (255, 255, 255) 
GREEN = (0, 255, 0) 
RED = (255, 0, 0) 

pygame.init() 

# Set the width and height of the screen [width, height] 
size = (700, 500) 
screen = pygame.display.set_mode(size) 

pygame.display.set_caption("My Game") 

# Loop until the user clicks the close button. 
done = False 

# Used to manage how fast the screen updates 

clock = pygame.time.Clock() 


# -------- Main Program Loop ----------- 
while not done: 
    # --- Main event loop 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 
    screen.fill(WHITE) 
    pygame.draw.rect(screen, BLACK, [300, 200, 100, 100],0) 
    screen.fill(WHITE) 
    pygame.draw.rect(screen, RED, [300, 200, 100, 100],0) 

    pygame.display.flip() 

    clock.tick(60) 


pygame.quit() 
+0

もしあなたが '' import time'を実行した場合はどうなりますか?2.赤いものを遅らせるために黒い矩形を描いた後に 'time.sleep(3)'を追加しますか? –

+0

'fill'のみを一度使用し、' rect(...、current_color、...) 'に変数を使用します。これをループで変更します。そして 'pygame.time.Clock()。tick()'を使って速度を落とすか、タイマー(またはイベント)を使って色を変えるときを制御してください。 – furas

答えて

1

あなたはこの変数に色を変更するpygame.time.set_timer()で独自のイベントを使用して... ..

rect(...)に色で変数を使用してすることができます。

import pygame 

BLACK = (0, 0, 0) 
WHITE = (255, 255, 255) 
GREEN = (0, 255, 0) 
RED = (255, 0, 0) 

size = (700, 500) 

# --- start --- 

pygame.init() 
screen = pygame.display.set_mode(size) 

# start color 
color = RED 

# define own event type 
CHANGE_COLOR = pygame.USEREVENT + 1 
# create event every 250ms 
pygame.time.set_timer(CHANGE_COLOR, 250) # 250ms 

# --- mainloop --- 

done = False 
clock = pygame.time.Clock() 

while not done: 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 

     # check if get event 
     if event.type == CHANGE_COLOR: 
      # change color 
      if color == RED: 
       color = BLACK 
      else: 
       color = RED 

    screen.fill(WHITE) 
    pygame.draw.rect(screen, color, [300, 200, 100, 100],0) 
    pygame.display.flip() 

    clock.tick(60) 

pygame.quit() 

...または使用 pygame.time.get_ticks()は、現在の時刻を取得し、それが色を変更する時間があるかどうかを確認してください。

import pygame 

BLACK = (0, 0, 0) 
WHITE = (255, 255, 255) 
GREEN = (0, 255, 0) 
RED = (255, 0, 0) 
size = (700, 500) 

# --- start --- 

pygame.init() 
screen = pygame.display.set_mode(size) 

# start color  
color = RED 

# get current time 
current_time = pygame.time.get_ticks() 

# first change after 250 ms 
change_color_time = current_time + 250 

# --- mainloop --- 

done = False 
clock = pygame.time.Clock() 

while not done: 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 

    # get current time 
    current_time = pygame.time.get_ticks() 

    # check if it is time to change color 
    if current_time >= change_color_time: 
     # set new time to change color again 
     change_color_time = current_time + 250 
     # change color 
     if color == RED: 
      color = BLACK 
     else: 
      color = RED      

    screen.fill(WHITE) 
    pygame.draw.rect(screen, color, [300, 200, 100, 100],0) 
    pygame.display.flip() 

    clock.tick(60) 

pygame.quit() 
関連する問題