0

So my ball animation for my multiplayer pong game is broken. Instead of moving and bouncing normally, the ball draws it self again after moving.

How do i fix the ball to not like clone itself after it moves 5 pixels? This is the animation code:

enter code here
import pygame

pygame.init()

#Creating the window
screen_width, screen_height = 1000, 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Pong")

#FPS
FPS = 60
clock = pygame.time.Clock()

#Game Variables
light_grey = (170,170,170)

#Paddles
paddle_1 = pygame.Rect(screen_width - 30, screen_height/2 - 70, 20,100)
paddle_2 = pygame.Rect(10, screen_height/2 - 70, 20, 100)

#Ball
ball = pygame.Rect(screen_width/2, screen_height/2, 30, 30)
ball_xVel = 5
ball_yVel = 5

def ball_animation():
    global ball_xVel, ball_yVel
    ball.x += ball_xVel
    ball.y += ball_yVel

    if ball.x > screen_width or ball.x < 0:
        ball_xVel *= -1
    if ball.y > screen_height or ball.y < 0:
        ball_yVel *= -1

def draw():
        pygame.draw.ellipse(screen, light_grey,  ball)
        pygame.draw.rect(screen, light_grey, paddle_1)
        pygame.draw.rect(screen, light_grey, paddle_2)


def main():

    #main game loop
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    
        draw()
        ball_animation()


        pygame.display.update()
        clock.tick(FPS)

    pygame.quit()


if __name__ == "__main__":
    main()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Yuhry
  • 11
  • 4

1 Answers1

0

You have to clear the display in every frame with pygame.Surface.fill:

screen.fill(0)

The typical PyGame application loop has to:

def main():

    # main application loop
    run = True
    while run:
        
        # event loop
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        # update and move objects
        ball_animation()

        # clear the display
        screen.fill(0)

        # draw the scene   
        draw()

        # update the display
        pygame.display.update()

        # limit frames per second
        clock.tick(FPS)

pygame.quit()
exit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174