Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.9 kB
0
Indexable
Never
import pygame
import random

# Initialize Pygame
pygame.init()

# Set up the game window
window_width = 640
window_height = 480
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("Pong")

# Set up the game clock
clock = pygame.time.Clock()

# Set up the game variables
ball_speed = 5
ball_x = window_width // 2
ball_y = window_height // 2
ball_direction = random.choice(["NE", "SE", "SW", "NW"])
paddle_speed = 5
left_paddle_x = 20
left_paddle_y = window_height // 2 - 50
right_paddle_x = window_width - 40
right_paddle_y = window_height // 2 - 50

# Main game loop
while True:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
            
    # Move the ball
    if ball_direction == "NE":
        ball_x += ball_speed
        ball_y -= ball_speed
    elif ball_direction == "SE":
        ball_x += ball_speed
        ball_y += ball_speed
    elif ball_direction == "SW":
        ball_x -= ball_speed
        ball_y += ball_speed
    elif ball_direction == "NW":
        ball_x -= ball_speed
        ball_y -= ball_speed
        
    # Check for collisions with walls
    if ball_y <= 0:
        if ball_direction == "NE":
            ball_direction = "SE"
        else:
            ball_direction = "SW"
    elif ball_y >= window_height:
        if ball_direction == "SE":
            ball_direction = "NE"
        else:
            ball_direction = "NW"
    if ball_x <= 0:
        if ball_direction == "NW":
            ball_direction = "NE"
        else:
            ball_direction = "SE"
    elif ball_x >= window_width:
        if ball_direction == "NE":
            ball_direction = "NW"
        else:
            ball_direction = "SW"
        
    # Check for collisions with paddles
    if ball_x <= left_paddle_x + 20 and left_paddle_y - 20 <= ball_y <=