I am trying to draw some black squares on a white screen using Pygame. Here is my problem using the pygame.draw.rect()
method:
pygame.draw.rect(screen, BLACK, (10, 10, 10, 10), 0) #no error
But I actually have lots of black square to plot so I want something like this:
a1 = (10, 10, 10, 10)
a2 = (20, 20, 10, 10)
A = [a1, a2]
for i in A:
pygame.draw.rect(screen, BLACK, A[i], 0) #gives error
TypeError: list indices must be integers, not tuple
But if I try this I don't get an error:
pygame.draw.rect(screen, BLACK, a1, 0) #no error
Any ideas?
You're looping over the elements of the array (tuples) instead of the indexes of A. There are several ways to do what you want. Here are the first two I thought of:
for i,_ in enumerate(A):
for i in range(len(A)):
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.