Having problems making Pong in Python with Pygame. Paddle extends when I move it

462 views Asked by At

So like many beginner python programmers, I have decided to code a pong game. This is my second attempt. The first was hard coded with no classes and few functions, so I have started from scratch. I currently have a paddle class and a main loop. I have coded the functionality for the paddles to move up and down, but I have a problem. When I press the keys to move the paddles, the just extend up and down, they don't actually move. Here is my code thus far:

#PONG GAME IN PYTHON WITH PYGAME

import pygame
import time

pygame.init()

white = (255, 244, 237)
black = (0, 0, 0)

largeFont = pygame.font.Font("pongFont.TTF", 75)
mediumFont = pygame.font.Font("pongFont.TTF", 50)
smallFont = pygame.font.Font("pongFont.TTF", 25)

displayWidth = 800
displayHeight = 600

gameDisplay = pygame.display.set_mode((displayWidth, displayHeight))
pygame.display.set_caption("Pong")

FPS = 60
menuFPS = 10
clock = pygame.time.Clock()

#Paddle Class
class Paddle:

    def __init__(self, player):

        self.length = 100
        self.width = 8
        self.yVelocity = 0
        self.y = (displayHeight - self.length) / 2

        #Puts player 1 paddle on left and player 2 on right
        if player == 1:
            self.x = 3 * self.width
        elif player == 2:
            self.x = displayWidth - 4 * self.width

    #Did paddle hit top or bottom?
    def checkWall(self):

        if self.y <= 0:
            return "top"
        elif self.y >= displayHeight - self.length:
            return "bottom"

    def stop(self):

        self.yVelocity = 0

    def moveUp(self):

        if self.checkWall() == "top":
            self.stop()
        else:
            self.yVelocity = -self.width

    def moveDown(self):

        if self.checkWall() == "bottom":
            self.stop()
        else:
            self.yVelocity = self.width

    #Draw the paddle
    def draw(self):

        self.y += self.yVelocity
        gameDisplay.fill(white, rect = [self.x, self.y, self.width,     self.length])

paddle1 = Paddle(1)
paddle2 = Paddle(2)

gameFinish = False

#Main Loop
while not gameFinish:

    #Event Loop
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

    #Get all pressed keys
    keyPressed = pygame.key.get_pressed()

    #Move paddle1 if s or w is pressed
    if keyPressed[pygame.K_w]:
        paddle1.moveUp()
    elif keyPressed[pygame.K_s]:
        paddle1.moveDown()
    else:
        paddle1.stop()

    #Move paddle2 if UP or DOWN is pressed
    if keyPressed[pygame.K_UP]:
        paddle2.moveUp()
    elif keyPressed[pygame.K_DOWN]:
        paddle2.moveDown()
    else:
        paddle2.stop()

    paddle1.draw()
    paddle2.draw()

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

Thanks in advance to anyone who can help!

2

There are 2 answers

0
Y. Tarion On

Clear the screen before, you are drawing a new paddle when the old paddle is still there.

Use something like gameDisplay.fill(white) to clear your screen.

3
Octo On

This is because you already have the paddle sprite on when you move it. Effectively what you want to do is destroy the old paddle and then draw the old one, otherwise the old one will still exists when you create the new one, making a merging effect.