嵌套 for 循环 运行 仅在 Python 中网格的第一行

Nested for loop running only on first row of a grid in Python

我正在开发 picross 的一个版本,但我在使用为方块着色的函数时遇到了问题。给它一个包含 10 个列表的列表,这些列表表示网格的行,其中每个列表的长度为 10 个元素。它们可以有 0、1 或 2 个条目。0 应该表示方块没有被标记为正确或错误,1 是正确的 space,2 是错误的 space。该代码正确地为第一行着色,但不影响其他 9 行。它还为游戏 window 带来了闪烁,这让我很不满意。这是函数,之后我会添加整个游戏代码。

def colorSquares(player_guess):
draw_y = 101
draw_x = 101
for row in player_guess:
    for position in row:
        rect = pygame.Rect(draw_x, draw_y, 43,43)
        if position == 0:
            pygame.draw.rect(win, BLACK, rect)
        elif position == 1:
            pygame.draw.rect(win, BLUE, rect)
        elif position == 2:
            pygame.draw.rect(win, GRAY, rect)
        draw_x += 45
    draw_y += 45
from itertools import zip_longest
from random import randint
import pygame
import math
BLACK = (0,0,0)
WHITE = (255,255,255)
BLUE = (25, 193, 212)
GRAY = (160, 181, 159)
win_height = 600
win_width = 600
def main():
    global win, clock, rows, columns, player_guess
    rows = [[randint(0,1) for i in range(10)] for x in range(10)]
    columns = list([x for x in y if x is not None] for y in zip_longest(*rows))
    player_guess = [[0 for i in range(10)] for i in range(10)]
    pygame.init()
    win = pygame.display.set_mode((win_height,win_width))
    clock = pygame.time.Clock()
    win.fill(BLACK)
    run = True
    while run:
        drawGrid()
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    x = pygame.mouse.get_pos()[0]
                    y = pygame.mouse.get_pos()[1]
                    if x > 100 and y > 100 and x < 550 and y < 550:
                        row_pos = (x - 100)// 45
                        col_pos = (y - 100)// 45
                        if player_guess[col_pos][row_pos] == 1:
                            player_guess[col_pos][row_pos] = 0
                        else:
                            player_guess[col_pos][row_pos] = 1
                        print("left ",row_pos,col_pos)
                        print(player_guess[col_pos][row_pos])
                if event.button == 3:
                    x = pygame.mouse.get_pos()[0]
                    y = pygame.mouse.get_pos()[1]
                    if x > 100 and y > 100 and x < 550 and y < 550:
                        row_pos = (x - 100)// 45
                        col_pos = (y - 100)// 45
                        if player_guess[col_pos][row_pos] == 2:
                            player_guess[col_pos][row_pos] = 0
                        else:
                            player_guess[col_pos][row_pos] = 2
                        print("left ",row_pos,col_pos)
                        print(player_guess[col_pos][row_pos])
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            colorSquares()
                    
        pygame.display.update()
def drawGrid():
    blockSize = 45 #Set the size of the grid block
    for x in range(100, 550, blockSize):
        for y in range(100, 550, blockSize):
            rect = pygame.Rect(x, y, blockSize, blockSize)
            pygame.draw.rect(win, WHITE, rect, 1)
def colorSquares():
    draw_y = 101
    draw_x = 101
    for row in player_guess:
        for position in row:
            rect = pygame.Rect(draw_x, draw_y, 43,43)
            if position == 0:
                pygame.draw.rect(win, BLACK, rect)
            elif position == 1:
                pygame.draw.rect(win, BLUE, rect)
            elif position == 2:
                pygame.draw.rect(win, GRAY, rect)
            draw_x += 45
        draw_y += 45
main()

我知道它还不是很完善,但我希望在我专注于对其进行大量调整之前让它工作。也就是说,欢迎提出任何建议。

您需要在每一行中“重新启动”draw_x

def colorSquares():
    draw_y = 101
    # draw_x = 101                      # <--- DELETE
    for row in player_guess:
        draw_x = 101                    # <--- INSERT
        for position in row:
            rect = pygame.Rect(draw_x, draw_y, 43,43)
            if position == 0:
                pygame.draw.rect(win, BLACK, rect)
            elif position == 1:
                pygame.draw.rect(win, BLUE, rect)
            elif position == 2:
                pygame.draw.rect(win, GRAY, rect)
            draw_x += 45
        draw_y += 45

您可以使用列表简化代码并使用 enumerate:

def colorSquares():
    colors = [BLACK, BLUE, GRAY]
    for y, row in enumerate(player_guess):
        for x, position in enumerate(row):
            rect = pygame.Rect(101 + x * 45, 101 + y * 45, 43,43)
            if position < len(colors): 
                pygame.draw.rect(win, colors[position], rect)