2017-07-27 7 views
2

最近リストを持つプログラムを作成しましたが、リストをマトリックスに変更する必要があります。私は長方形を描くことによってグリッドをプログラムしました。今、私はそれをクリックすると矩形の色を変更したいと思います。私のプログラムでは、すべてのプログラムはうまくいきましたが、今は行列を使用する必要があります。なぜなら、残りのプログラムのために行列が必要だからです。私はすでにすべて0の行列を持っていますが、今は矩形をクリックすると0を1に変更します。イベントが発生したときに、行列の要素を0から1に変更します。

x = 5 
y = 5 

height = 30 
width = 50 
size = 20 
color = (255,255,255) 
new_color = (0,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 
     y += 20 

rects = [[0 for i in range(width)] for j in range(height)] 
draw_grid() 


while 1: 
    clock.tick(30) 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      sys.exit() 


    if menu == 'start': 

     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) 

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

     pygame.display.flip() 

これは私がリストで使用したコードですが、すでにリストをマトリックスに置き換えています。このコードを実行すると、エラーが発生します。

ValueError: too many values to unpack 

この問題を解決するにはどうすればよいですか?

+1

rectはリストのリストですが、 'for rect、rect in color:'の2タプルのリストとして扱います。それは動作しません。 – Zinki

+0

代わりに何をすればよいですか? – AV13

+0

'rects'リストの0と1はrectの色を表しますか? 'pygame.Rect'をリストに格納したくない場合は、衝突検出と描画コードを変更する必要があります。 – skrx

答えて

1

矩形を描画するには、行列を反復処理し、値(0または1)に応じて白い矩形または緑の矩形を描画します。 (色をマトリックスに直接保存することもできますが、それ以外の何かをしたいかどうかは分かりません)

クリックしたセルの色を変更するには、マウスコードを床で分割して細胞を(size+1)、例えばx = mouse_x // (size+1)。次にmatrix[y][x] = 1と設定します。

import sys 
import pygame 


WHITE = pygame.Color('white') 
GREEN = pygame.Color('green') 


def draw_grid(screen, matrix, size): 
    """Draw rectangles onto the screen to create a grid.""" 
    # Iterate over the matrix. First rows then columns. 
    for y, row in enumerate(matrix): 
     for x, color in enumerate(row): 
      rect = pygame.Rect(x*(size+1), y*(size+1), size, size) 
      # If the color is white ... 
      if color == 0: 
       pygame.draw.rect(screen, WHITE, rect) 
      # If the color is green ... 
      elif color == 1: 
       pygame.draw.rect(screen, GREEN, rect) 


def main(): 
    screen = pygame.display.set_mode((800, 600)) 
    clock = pygame.time.Clock() 

    height = 30 
    width = 50 
    size = 20 # Cell size. 
    matrix = [[0 for i in range(width)] for j in range(height)] 

    done = False 

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

     if pygame.mouse.get_pressed()[0]: 
      # To change the color, calculate the indexes 
      # of the clicked cell like so: 
      mouse_x, mouse_y = pygame.mouse.get_pos() 
      x = mouse_x // (size+1) 
      y = mouse_y // (size+1) 
      matrix[y][x] = 1 

     screen.fill((30, 30, 30)) 
     # Now draw the grid. Pass all needed values to the function. 
     draw_grid(screen, matrix, size) 

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


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

ありがとう!これはまさに私が埋め尽くしたものです!私はもう一つ質問があります:draw_grid関数ではまず:yの場合列挙する行(行列)。なぜあなたは行を使用していますか?そして、次の線の色に? – AV13

+0

'row'は、0と1を含む行列のサブリストです。次の行では、 'row'(サブリスト)を繰り返して色を表す0と1を取得するので、私は変数に' color'という名前を付けました。 – skrx

関連する問題