Reputation: 35
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
Upvotes: 2
Views: 333
Reputation: 20438
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.
Upvotes: 1