嵌套用于仅在Python中网格的第一行上运行的循环



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

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)

最新更新