Untitled

 avatar
unknown
plain_text
2 years ago
1.5 kB
1
Indexable
import pygame

# Initialize the game
pygame.init()

# Set the screen size
screen = pygame.display.set_mode((800, 600))

# Set the title and icon
pygame.display.set_caption("Ping Pong")
icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)

# Set the player properties
player_1_x = 50
player_1_y = 250
player_1_speed = 0

player_2_x = 750
player_2_y = 250
player_2_speed = 0

# Set the ball properties
ball_x = 400
ball_y = 300
ball_speed_x = 0.5
ball_speed_y = 0.5

# Set the game loop to run until the user exits
running = True
while running:

    # Set the background color
    screen.fill((0, 0, 0))

    # Handle the player and ball movements
    player_1_y += player_1_speed
    player_2_y += player_2_speed
    ball_x += ball_speed_x
    ball_y += ball_speed_y

    # Create the players
    pygame.draw.rect(screen, (255, 255, 255), (player_1_x, player_1_y, 10, 50))
    pygame.draw.rect(screen, (255, 255, 255), (player_2_x, player_2_y, 10, 50))

    # Create the ball
    pygame.draw.circle(screen, (255, 255, 255), (ball_x, ball_y), 10)

    # Handle the player and ball collisions
    if ball_x >= player_2_x:
        if ball_y >= player_2_y and ball_y <= player_2_y + 50:
            ball_speed_x = -ball_speed_x
    elif ball_x <= player_1_x:
        if ball_y >= player_1_y and ball_y <= player_1_y + 50:
            ball_speed_x = -ball_speed_x

    if ball_y >= 600:
        ball_speed_y = -ball_speed_y
    elif ball_y <= 0:
        ball_speed_y = -ball_speed_y

    # Handle the user inputs
Editor is loading...