2017-07-25 18 views
1

私はすでにグリッドをプログラムしましたが、グリッド内の1つの矩形の色を変更したいと思います。グリッド内の単一の矩形の色を変更するにはどうすればよいですか?

x = 5 
y = 5 

height = 30 
width = 50 
size = 20 
color = (255,255,255) 
new_color = (255,255,0) 

screen.fill((0,0,0)) 

def draw_grid(): 
    for y in range(height): 
     for x in range(width): 
      rect = pygame.Rect(x * (size + 1),y * (size + 1),size,size) 
      pygame.draw.rect(screen,color,rect) 
      x += 20 
      rects.append((rect,color)) 
     y += 20 

rects = [] 
colored_rects = [] 

while 1: 

    for event in pygame.event.get(): 
     if event.type == QUIT: 
      sys.exit() 

    draw_grid() 

    if pygame.mouse.get_pressed()[0]: 
     mouse_pos = pygame.mouse.get_pos() 
     for i,(rect,color) in enumerate(rects): 
      if rect.collidepoint(mouse_pos): 
       rects[i] = (rect,new_color) 
       colored_rects.append((rect,new_color)) 


    for rect,color in rects: 
     pygame.draw.rect(screen,color,rect) 
    for rect,new_color in colored_rects: 
     pygame.draw.rect(screen,new_color,rect) 

    pygame.display.flip() 
    clock.tick() 

は今、私は私がそれをクリックしたときに1つの矩形を変更したいのですが、後に、彼らは自動的に変更する必要があります(同じ色に触れる3つの長方形がある場合例えば、それらはすべて白にならなければなりません)。私は少し更新しましたが、まだいくつかの問題があります。たとえば、色が変わるまで矩形をクリックしなければならず、色を変えるのに多くの時間がかかります。

答えて

0

1つの解決策は、その色と共にタイル内に矩形を格納することです。マウスボタンが押された場合、rectanglesリストを繰り返し処理します。四角形がマウスに衝突する場合は、rectと新しい色のタプルを作成し、現在のインデックスのタプルを置き換えます。

import sys 
import pygame as pg 


def main(): 
    screen = pg.display.set_mode((640, 480)) 
    clock = pg.time.Clock() 

    height = 30 
    width = 50 
    size = 20 
    color = (255, 255, 255) 
    new_color = (255, 255, 0) 

    rectangles = [] 
    for y in range(height): 
     for x in range(width): 
      rect = pg.Rect(x * (size+1), y * (size+1), size, size) 
      # The grid will be a list of (rect, color) tuples. 
      rectangles.append((rect, color)) 

    done = False 

    while not done: 
     for event in pg.event.get(): 
      if event.type == pg.QUIT: 
       done = True 

     if pg.mouse.get_pressed()[0]: 
      mouse_pos = pg.mouse.get_pos() 
      # Enumerate creates tuples of a number (the index) 
      # and the rect-color tuple, so it looks like: 
      # (0, (<rect(0, 0, 20, 20)>, (255, 255, 255))) 
      # You can unpack them directly in the head of the loop. 
      for index, (rect, color) in enumerate(rectangles): 
       if rect.collidepoint(mouse_pos): 
        # Create a tuple with the new color and assign it. 
        rectangles[index] = (rect, new_color) 

     screen.fill((30, 30, 30)) 

     # Now draw the rects. You can unpack the tuples 
     # again directly in the head of the for loop. 
     for rect, color in rectangles: 
      pg.draw.rect(screen, color, rect) 

     pg.display.flip() 
     clock.tick(30) 


if __name__ == '__main__': 
    pg.init() 
    main() 
    pg.quit() 
    sys.exit() 
+0

私はこのコードを試しましたが、それは動作しますが、四角形をクリックすると1秒間だけ色が変わります。そして、それは常に動作しません。 – AV13

+0

更新されたコードをあなたの投稿に追加してください。 – skrx

+0

私はちょうどそれをudpated – AV13

関連する問題