如何在Pygame中进行自主运动?
我是Pygame的新手,我正在尝试在这里做有史以来最简单的事情,我只想在没有用户输入的情况下在屏幕上移动一个圆圈。我似乎只在网上找到有关如何使用键移动对象的建议,而不是我想要的,我只希望它自己移动,以便用户可以观看其移动。
这是我试图用来做到这一点的方法。
代码从这里开始:
import pygame module in this program
import pygame
pygame.init()
white = (255, 255, 255)
green = (0, 255, 0)
blue = (0, 0, 128)
black = (0, 0, 0)
red = (255, 0, 0)
X = 400
Y = 400
display_surface = pygame.display.set_mode((X, Y ))
pygame.display.set_caption('Drawing')
display_surface.fill(white)
x,y=[300,50]
pygame.draw.circle(display_surface,green, (x, y), 20, 0)
clock = pygame.time.Clock()
def move():
global y
y=y+1
while True :
clock.tick(30)
move()
for event in pygame.event.get() :
# if event object type is QUIT
# then quitting the pygame
# and program both.
if event.type == pygame.QUIT :
pygame.quit()
quit()
pygame.display.update()
代码在此处结束:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在每个帧中重新绘制场景:
典型的PyGame应用程序循环必须:
pygame.time.clock.tick.tick
pygame.event.get()
。BLIT
所有对象)You have to redraw the scene in every frame:
The typical PyGame application loop has to:
pygame.time.Clock.tick
pygame.event.pump()
orpygame.event.get()
.blit
all the objects)pygame.display.update()
orpygame.display.flip()