Stavan
Stavan

Reputation: 51

Pong Game Score Keeping in Pygame

Here is the code segment from my program which controls the score keeping. The problem is that it adds 1 to the score each time it touches the right wall as well as the left paddle, and it also subtracts a point whenever it touches the left wall. When all it should be doing is adding one every time it touches the right wall.

    FRAMECLOCK = pygame.time.Clock() #set frame rate
    SURFACEDISPLAY = pygame.display.set_mode((WIDTH,HEIGHT)) #Clear the surface on refresh
    pygame.display.set_caption ('Pong') #title of window 

    ballX = WIDTH/2 - PLACEMENTMARKER/2 #ball position on X axis at the start
    ballY = HEIGHT/2 - PLACEMENTMARKER/2 #ball position on Y axis at the start
    playerOnePosition = (HEIGHT - PADDLESIZE) /2 #paddle one position at the start
    playerTwoPosition = (HEIGHT - PADDLESIZE) /2 #paddle two position at the start
    score = 0

    #Sets starting position movement
    ballDirX = -1 #-1 = left 1 = right
    ballDirY = -1 # -1 = up 1 = down

    paddle1 = pygame.Rect(PADDLEDISTANCE,playerOnePosition, PLACEMENTMARKER,PADDLESIZE) #paddle one drawing 
    paddle2 = pygame.Rect(WIDTH - PADDLEDISTANCE - PLACEMENTMARKER, playerTwoPosition, PLACEMENTMARKER,PADDLESIZE) #paddle two drawing 
    ball = pygame.Rect(ballX, ballY, PLACEMENTMARKER, PLACEMENTMARKER)#ball drawing

    Pong() #calling the game surface in main function
    paddles(paddle1) #calling paddle 1 main function
    paddles(paddle2) #calling paddle 2 in main function
    pongball(ball) #calling ball in main function 

    while True: #game Loop
        for event in pygame.event.get(): #Checks to see if program is quit
            if event.type == QUIT:
                pygame.quit()
                sys.exit() #system quit

        Pong() #Otherwise it performs these functions 
        paddles(paddle1)
        paddles(paddle2)
        pongball(ball)

        displayScore(str(score))

Upvotes: 1

Views: 317

Answers (1)

DCA-
DCA-

Reputation: 1292

The checkscore function was resetting the score, not subtracting it. It is also explicitly adding one when you hit it with the paddle.

I've modified the function to only add when hitting the right wall and not subtracting upon hitting the left:

def checkscore (paddle1, ball, score, ballDirX):
    #this is where the program resets after a point is scored 
    if ball.right == WIDTH - PLACEMENTMARKER:
        score += 1
        return score
    #no points scored, return score unchanged 
    else: return score 

just substitute this function with the current checkscore() and everything should work

I'm assuming you've copied at least a large majority of this, make sure you read through everything thoroughly and try to understand each bit.

Upvotes: 1

Related Questions