0

I would like to draw a white circle moving naturally on the screen, but I get lots of white circles drawn in a discontinuous way without giving the illusion of movement. How can I modify this code?

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()
Andrea
  • 101
  • 7

2 Answers2

1

You have to first fill the screen in black and then draw on it again.

    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))
1

You should clear the screen every tick, e.g. by filling it with a solid color. Also, you set x and y new every tick, which does not allow you to create most movement patterns. I suggest introducing x and y variables that you can modify in small increments each tick, like so:

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()
Jotha
  • 418
  • 2
  • 7