如何在pygame中使形状在屏幕上循环

时间:2018-01-21 12:03:44

标签: python pygame

    import pygame
    pygame.init()
    screen = pygame.display.set_mode((1600,900))
    red=(255,0,0)
    blue=(0,0,204)
    white=(255,255,255)
    pygame.display.set_caption("Orbit")
    gameLoop=True 
    clock=pygame.time.Clock()
    while gameLoop:

      dy=10 
      dx=10
      x=600
      y=250



      for event in pygame.event.get():

          if (event.type==pygame.QUIT):
           gameLoop=False
      x=x +dx 
      y=y +dy

     screen.fill(white)
     pygame.draw.circle(screen,red,[800,450],50,0)
     pygame.draw.circle(screen,blue,[x,y],10,0)
     pygame.display.update()
     clock.tick(50)
    pygame.quit()

我在这里编写了这个有两个圆圈的代码,我希望得到一个(较小的一个)在屏幕上循环但是由于某种原因它不动,我不知道我做错了什么我已经阅读并搜索了一些对我没那么帮助的东西。非常感谢任何建议,谢谢你的时间

1 个答案:

答案 0 :(得分:2)

您的xy值是在游戏循环中定义的,因此,即使您正在向他们添加dxdy,您也可以#39 ;每帧重新将它们重置为原始值。

如果您将x=600y=250移至while循环之前,它将开始移动。