2

I am using python 2.7 to make a simple game and whenever I try to draw a rectangle I get this:

Traceback (most recent call last): File "C:/Users/HP/Desktop/experiment.py", line 335, in (WIDTH ,HEIGHT)]) TypeError: invalid color argument

Here's the relevant code

BLACK = (0, 0, 0)
grid = []
for row in range(10):
    #add empty array
    grid.append([])
    for column in range(11):
        grid[row].append(BLACK)
WIDTH = 30
HEIGHT = 30
MARGIN = 5
#starting pygame
pygame.init()

#Setting window dimensions
WINDOW_SIZE = [425,320]
screen = pygame.display.set_mode(WINDOW_SIZE)
stopped = False
while not stopped:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            stopped = True
#draw grid
    for row in range(10):
        for column in range(11):
            Color = grid[row][column]
            pygame.draw.rect(screen,Color,
                             [((MARGIN + WIDTH) * column + MARGIN, (MARGIN + HEIGHT) * row + MARGIN),
                              (WIDTH ,HEIGHT)])
    clock.tick(60)

    pygame.display.flip()


pygame.quit()

Any assistance whatsoever would be greatly appreciated!! Thanks in advance!

EDIT: If needs be here's the entire script in pastebin https://pastebin.com/jCXT0M4a

Pardon the quality of it

  • The error seems self-explanatory to me. You are supplying a `(WIDTH, HEIGHT)` tuple when a color was expected. Double-check your arguments against the formal parameters for the function call. – Knowledge Cube Jun 27 '17 at 16:05
  • 1
    I checked the documentation and for pygame.draw.rect() the parameters are: Surface, color, Rect, width=0 and the parameters for pygame.Rect() are ((left, top), (width, height)) so I don't know why it's not working – Daniel Hannon Jun 27 '17 at 16:18
  • 1
    I just ran this code and added the correct import statements and didn't get any error. Would you mind adding a pastebin link to the rest of your code? – ikhaliq15 Jun 27 '17 at 16:22
  • 1
    here's the entire script pardon the size of it and the quality, I'm still learning https://pastebin.com/jCXT0M4a – Daniel Hannon Jun 27 '17 at 16:28
  • 1
    I'm not able to check your code on my own right now, but it does look like what you have *should* work, so I'm confused right now as well... Have you tried defining your Rect argument as a separate variable on its own and passing that in to your `pygame.draw.rect()` call? Might change where you are getting your error at and provide further clues. – Knowledge Cube Jun 27 '17 at 16:31

1 Answers1

1

Print the grid and the Color in the for loop and you'll see that some rows of the grid contain lists of tuples instead of just tuples. The refresh function causes the problem, especially these lines:

row1 = [right_side[0],front[0],left_side[0],back[0]]
row2 = [right_side[1],front[1],left_side[1],back[1]]
row3 = [right_side[2],front[2],left_side[2],back[2]]

It seems to me that row1-3 should be lists of tuples, but you create lists of lists of tuples:

[[(0, 255, 0), (0, 255, 0), (0, 255, 0)], [(255, 0, 0), (255, 0, 0), (255, 0, 0)], [(255, 255, 0), (255, 255, 0), (255, 255, 0)], [(0, 0, 255), (0, 0, 255), (0, 0, 255)]]

In Python 3.5 and above you can use the newest additional unpacking features that allow you to unpack lists into other lists with the star * operator.

row1 = [*right_side[0], *front[0], *left_side[0], *back[0]]

In other versions you can use one of these techniques.

skrx
  • 19,980
  • 5
  • 34
  • 48