【问题标题】:Can't draw a circle moving natually on the screen [duplicate]不能在屏幕上画一个自然移动的圆圈[重复]
【发布时间】:2023-03-23 21:04:02
【问题描述】:

我想画一个在屏幕上自然移动的白色圆圈,但我得到了很多以不连续方式绘制的白色圆圈,没有给人运动的错觉。如何修改此代码?

import pygame
from sys import exit
import math

BLACK = [0, 0, 0]
WHITE = [255, 255, 255]

class Game():
    def __init__(self):
        pygame.init()
        self.window = pygame.display.set_mode((500, 500), 0, 32)
        pygame.display.set_caption('Jumping ball')
        self.clock = pygame.time.Clock()

    def mainloop(self):
        while True:
            EVENTS = pygame.event.get()

            for event in EVENTS:
                if event.type == pygame.QUIT:
                    exit()

            time = self.clock.tick(60)  # [seconds]


            x, y = 100 + 50 * math.sin(time), 100 + 50 * math.cos(time)
            
            x, y = int(x), int(y)

            radius = 5

            pygame.draw.circle(self.window, WHITE, (x, y), radius)

            #self.screen.blit(ball, (x, y))
            
            pygame.display.update()
            


if __name__ == '__main__':
    game = Game()
    game.mainloop()

【问题讨论】:

    标签: pygame python-3.8


    【解决方案1】:

    您必须先将屏幕填充为黑色,然后再在其上绘制。

        def mainloop(self):
            while True:
                EVENTS = pygame.event.get()
    
                for event in EVENTS:
                    if event.type == pygame.QUIT:
                        exit()
    
                time = self.clock.tick(60)  # [seconds]
    
    
                x, y = 100 + 50 * math.sin(time), 100 + 50 * math.cos(time)
                
                x, y = int(x), int(y)
    
                radius = 5
                self.window.fill((0, 0, 0)) #Black
                pygame.draw.circle(self.window, WHITE, (x, y), radius)
                
                #self.screen.blit(ball, (x, y))
    

    【讨论】:

      【解决方案2】:

      您应该在每一次滴答声中清除屏幕,例如通过用纯色填充它。 此外,您将 x 和 y 设置为新的每个刻度,这不允许您创建大多数运动模式。我建议引入 x 和 y 变量,您可以在每个刻度上以小增量进行修改,如下所示:

      import pygame
      from sys import exit
      import math
      
      BLACK = [0, 0, 0]
      WHITE = [255, 255, 255]
      
      
      class Game():
          def __init__(self):
              pygame.init()
              self.window = pygame.display.set_mode((500, 500), 0, 32)
              pygame.display.set_caption('Jumping ball')
              self.clock = pygame.time.Clock()
              self.ball_x = 100
              self.ball_y = 100
      
          def mainloop(self):
              while True:
                  self.window.fill(BLACK)
      
                  EVENTS = pygame.event.get()
      
                  for event in EVENTS:
                      if event.type == pygame.QUIT:
                          exit()
      
                  time = self.clock.tick(60)  # [seconds]
      
                  self.ball_x -= math.sin(time)
                  self.ball_y -= math.cos(time)
      
                  x, y = int(self.ball_x), int(self.ball_y)
      
                  radius = 5
      
                  pygame.draw.circle(self.window, WHITE, (x, y), radius)
      
                  # self.screen.blit(ball, (x, y))
      
                  pygame.display.update()
      
      
      if __name__ == '__main__':
          game = Game()
          game.mainloop()
      

      【讨论】:

        猜你喜欢
        • 2022-01-05
        • 2012-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多