Pong game, but the ball goes out of the window

Question:

I coded a Pong game but the ball goes out of the window.

import pygame
pygame.init()
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))


#Colors
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)


#Game Variables
VEL = 5
FPS = 60
CENTER = (WIDTH/2, HEIGHT/2)
DOTRAD = 10
DOT_speed_x = 2
DOT_speed_y = 0



DOT = pygame.Rect(CENTER[0], CENTER[1], DOTRAD, DOTRAD)

#Player
PLAYER1 = pygame.Rect(50, HEIGHT/2-75, 10, 150)
PLAYER2 = pygame.Rect(850, HEIGHT/2-75, 10, 150)



def draw_window():
    WIN.fill(WHITE)
    pygame.draw.rect(WIN, BLACK, PLAYER1)
    pygame.draw.rect(WIN, BLACK, PLAYER2)
    pygame.draw.circle(WIN, BLACK, DOT[0:2], DOTRAD)
    pygame.display.update()


def player1_handle_movement(keys_pressed):
    if keys_pressed[pygame.K_w] and PLAYER1.y > 0:      #Up
        PLAYER1.y -= VEL
    if keys_pressed[pygame.K_s] and PLAYER1.y + PLAYER1.height < HEIGHT:    #Down
        PLAYER1.y += VEL

def player2_handle_movement(keys_pressed):
    if keys_pressed[pygame.K_UP] and PLAYER2.y > 0:      #Up
        PLAYER2.y -= VEL
    if keys_pressed[pygame.K_DOWN] and PLAYER2.y + PLAYER2.height < HEIGHT:    #Down
        PLAYER2.y += VEL

def DOT_handle_movement (DOT_speed_x, DOT_speed_y):
    DOT.x += DOT_speed_x
    DOT.y += DOT_speed_y
    
    if DOT.top <= 0 or DOT.bottom >= HEIGHT:
        DOT_speed_y *= -1
    
    if DOT.left <= 0 or DOT.right >= WIDTH:
        DOT_speed_x *= -1
    

    
    print(DOT_speed_x, DOT_speed_y)
        

def main():
    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False        


        
        keys_pressed = pygame.key.get_pressed()
        player1_handle_movement(keys_pressed)
        player2_handle_movement(keys_pressed)
        DOT_handle_movement(DOT_speed_x, DOT_speed_y)
        
        draw_window()
    pygame.quit()
    
if __name__ == "__main__":
    main()

I print the DOT_speed_x and DOT_speed_y to see if it works that the ball detects that its out of the window.
Yes it does.
But its not changing the direction.
I saw so many tutorials that say that this method should work fine but for me it doesn’t.

Asked By: gott150

||

Answers:

In Python, there is no concept of in-out parameters. If you change DOT_speed_x or DOT_speed_y, only the parameter variable that is local to the function changes, but not the arguments that are passed to the function. You have to return the new values of DOT_speed_x and DOT_speed_y from the function.

def DOT_handle_movement(DOT_speed_x, DOT_speed_y):
    DOT.x += DOT_speed_x
    DOT.y += DOT_speed_y
    
    if DOT.top <= 0 or DOT.bottom >= HEIGHT:
        DOT_speed_y *= -1
    
    if DOT.left <= 0 or DOT.right >= WIDTH:
        DOT_speed_x *= -1
     
    return DOT_speed_x, DOT_speed_y
def main():
    DOT_speed_x = 2
    DOT_speed_y = 0

    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False        

        keys_pressed = pygame.key.get_pressed()
        player1_handle_movement(keys_pressed)
        player2_handle_movement(keys_pressed)
        
        DOT_speed_x, DOT_speed_y = DOT_handle_movement(DOT_speed_x, DOT_speed_y)
        
        draw_window()
    pygame.quit()
    

Note that it is absolutely not necessary that the function arguments and the functions parameter variables have the same name.

Answered By: Rabbid76
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.