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

Time:10-21

I am working on a version of picross, and I'm having trouble with the function that colors the squares. It is given a list of 10 lists representing the rows of the grid, where each list is 10 elements long. They can have entries of 0, 1, or 2. 0 should mean that the square has not been marked as correct or wrong, 1 is a correct space, and 2 is an incorrect space. The code correctly colors the first row but doesn't affect the other 9 rows. It has also introduced a flickering to the game window that I am unhappy with. Here is the function, and I'll add the entire game code afterwards.

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()

I know that it's not very polished yet, but I'd like to get it working before I focus on tuning it much. That said, any and all advice is welcome.

CodePudding user response:

You need to "restart" draw_x in each row:

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

You can simplify the code with a list and using 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)
  • Related