Pong Source Code

TheToolbox

Elite Member
Joined
Jul 25, 2021
Messages
1,568
Reaction score
643
I started with coding in game development. At the time I learnt it and developed a few games, there was no chat gpt. I was surprised today how easy it is now to recreate pong in seconds :D Maybe a little food for thought to quickly create a small product. And of course I also share the source code, have fun with it :)

Python:
import pygame
import sys

# Initialize pygame
pygame.init()

# Screen size and field colors
display_width, display_height = 800, 600
background_color = (0, 0, 0)
paddle_color = (255, 255, 255)
ball_color = (255, 255, 255)

# Create the game window
screen = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("Pong")

# Game variables
clock = pygame.time.Clock()
FPS = 60

# Paddles
paddle_width, paddle_height = 10, 100
player1_x, player1_y = 50, (display_height - paddle_height) // 2
player2_x, player2_y = display_width - 50 - paddle_width, (display_height - paddle_height) // 2
player_speed = 5

# Ball
ball_size = 15
ball_x, ball_y = display_width // 2, display_height // 2
ball_speed_x, ball_speed_y = 4, 4

# Main game loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Key detection for Player 1
    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP] and player1_y > 0:
        player1_y -= player_speed
    if keys[pygame.K_DOWN] and player1_y < display_height - paddle_height:
        player1_y += player_speed

    # Player 2 follows the ball
    if ball_y < player2_y + paddle_height // 2:
        player2_y -= player_speed
    if ball_y > player2_y + paddle_height // 2:
        player2_y += player_speed

    # Boundaries for Player 2
    if player2_y < 0:
        player2_y = 0
    if player2_y > display_height - paddle_height:
        player2_y = display_height - paddle_height

    # Ball movement
    ball_x += ball_speed_x
    ball_y += ball_speed_y

    # Ball collision with walls
    if ball_y <= 0 or ball_y >= display_height - ball_size:
        ball_speed_y *= -1

    # Ball collision with paddles
    if (player1_x < ball_x < player1_x + paddle_width and player1_y < ball_y < player1_y + paddle_height) or \
       (player2_x < ball_x < player2_x + paddle_width and player2_y < ball_y < player2_y + paddle_height):
        ball_speed_x *= -1

    # Ball out of bounds
    if ball_x < 0 or ball_x > display_width:
        ball_x, ball_y = display_width // 2, display_height // 2
        ball_speed_x *= -1

    # Update the screen
    screen.fill(background_color)
    
    # Draw paddles
    pygame.draw.rect(screen, paddle_color, (player1_x, player1_y, paddle_width, paddle_height))
    pygame.draw.rect(screen, paddle_color, (player2_x, player2_y, paddle_width, paddle_height))
    
    # Draw ball
    pygame.draw.ellipse(screen, ball_color, (ball_x, ball_y, ball_size, ball_size))

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

# Exit the game
pygame.quit()
sys.exit()
 
Nice job recreating Pong! It's amazing how quickly things can come together now with tools like ChatGPT. Keep up the great work!
 
this is very cool, has anyone managed to make a real commercial game with Pygame?
 
this is very cool, has anyone managed to make a real commercial game with Pygame?
Quite possibly many have. RenPy which is the go-to visual novel engine most indie visual novels use was built on top of PyGame iirc. And steam has lots. LOTS of those.
 
Quite possibly many have. RenPy which is the go-to visual novel engine most indie visual novels use was built on top of PyGame iirc. And steam has lots. LOTS of those.
Thanks for the info, I have no idea what I'm doing and the money on Steam seems incredibly free-flowing. I really need to get into game design
 
Back
Top