简体   繁体   English

用Pygame绘制正方形; draw方法只需要整数?

[英]Drawing squares with Pygame; draw method only takes integers?

I am trying to draw some black squares on a white screen using Pygame. 我正在尝试使用Pygame在白色屏幕上绘制一些黑色正方形。 Here is my problem using the pygame.draw.rect() method: 这是我使用pygame.draw.rect()方法的问题:

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. 您正在遍历数组(元组)的元素,而不是A的索引。有几种方法可以执行所需的操作。 Here are the first two I thought of: 这是我想到的前两个:

for i,_ in enumerate(A):

for i in range(len(A)):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM