简体   繁体   English

如何在pygame中将数组输出到显示器中?

[英]How do I output an array into the display in pygame?

I'm creating a game in which if a certain condition is met it will add the rect inside the array我正在创建一个游戏,如果满足某个条件,它将在数组中添加矩形

tail.append(pygame.draw.rect(screen, (0, 0, 0), (350, 350, 25, 25)))

and while it did really add it in the array when I printed the array in the console, but the rect disappears immediately from the display game.虽然当我在控制台中打印数组时它确实将它添加到数组中,但该矩形立即从显示游戏中消失。 I figured out that its because it only shows in when the the condition is met but after the coordinates changes it'll disappear which is only a second.我发现这是因为它只在满足条件时显示,但在坐标改变后它会消失,这只有一秒钟。

The entire scene has to be redrawn in each frame.整个场景必须在每一帧中重新绘制。 You need to draw the rectangles in the main application loop.您需要在主应用程序循环中绘制矩形。 Create a pygame.Rect object and append it to tail when the condition is met:创建一个pygame.Rect对象并在满足条件时将其附加到tail

tail.append(pygame.Rect(350, 350, 25, 25))

However, draw all the rectangles in tail in the application loop:但是,在应用程序循环中绘制tail所有矩形:

while True:

    # [...]

    for rect in tail:
        pygame.draw.rect(screen, (0, 0, 0), rect)

    # [...]

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

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