如何使用数组制作一个矩形,以便我可以同时在屏幕上显示多个?

标题很自我解释。我试图找出如何使用Python的这个俄罗斯方块游戏的数组做一个矩形。如何使用数组制作一个矩形,以便我可以同时在屏幕上显示多个?

下面的代码:

screen = pygame.display.set_mode((400,800)) 

#Rectangle Variables

x = 200

y = 0

width = 50

height = 50

thickness = 5

speed = 1

#Colors

red = (255,0,0)

white = (255,255, 255)

green = (0,255,0)

blue = (0,0,255)

while(True):

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit(); sys.exit();

#These lines^make the user able to exit out of the game window

y = y+1

pygame.draw.rect((screen) , red, (x,y,width,height), thickness)

pygame.display.update()

回答:

如果你想要做的就是添加矩形到一个数组,你可能只是这样做:

rectangles = [] 

while(True):

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit(); sys.exit();

#These lines^make the user able to exit out of the game window

y = y+1

rectangles.append(pygame.draw.rect((screen) , red, (x,y,width,height), thickness))

pygame.display.update()

回答:

如果你有一个位置列表,然后使用for环路画出来。

这里的位置是在像素

# --- constants --- (UPPER_CASE_NAMES) 

WIDTH = 50

HEIGHT = 50

RED = (255,0,0)

# --- main ---

rectangles_XY = [ (0, 0), (50, 0), (100, 0) ]

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

# PLEASE, don't put all in one line

# it makes code less readable.

pygame.quit()

sys.exit()

for x, y in rectangles_XY:

pygame.draw.rect(screen, RED, (x, y, WIDTH, HEIGHT), 0)

pygame.display.update()

这里位置在小区现在的位置(列,行)

# --- constants --- (UPPER_CASE_NAMES) 

WIDTH = 50

HEIGHT = 50

RED = (255,0,0)

# --- main ---

rectangles = [ (0, 0), (1, 0), (2, 0) ]

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

# PLEASE, don't put all in one line

# it makes code less readable.

pygame.quit()

sys.exit()

for column, row in rectangles:

x = column * WIDTH

y = row * HEIGHT

pygame.draw.rect(screen, RED, (x, y, WIDTH, HEIGHT), 0)

pygame.display.update()

以上是 如何使用数组制作一个矩形,以便我可以同时在屏幕上显示多个? 的全部内容, 来源链接: utcz.com/qa/258555.html

回到顶部