Make Pong with python

Table of Content

  1. Setup Enviroment
  2. Pygame Boilerplate
  3. Creating paddles
  4. Making the ball
  5. Score System

Image of final product

Pong Final Image

Download Python and install Pygames

Get VS Code, Python and Pygames

Get Visual Studio Code because why use any other IDEs Download VS Code

Get Python from Python website, Click Here!

To install Pygame, in terminal enter
For Windows py -m pip install -U pygame --user
For Mac python3 -m pip install -U pygame --user

Check if they are installed correctly

Check Python
_ For Windows: py --version
_ For Mac: python3 --version

Check Pygame: pip show pygame

Download Satoshi Font (Save in same folder as Pong.py)

If you use another font change Satoshi-Variable.ttf to file name in the code,
Download Font.

Pygame Base

Use Ultimate Technique ctrl + c and ctrl + v

import pygame, sys, random
 
pygame.init()
 
WIDTH, HEIGHT = 1280, 720       
 
FONT = pygame.font.SysFont("Satoshi-Variable.ttf", int(WIDTH/20))
 
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))   # Make Screen this size
pygame.display.set_caption("Pong")
CLOCK = pygame.time.Clock()
 
while True:                     # Keep Pygames active until closed
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
 
    pygame.display.update()     # Update game frame 
    CLOCK.tick(300)             # Advance game tick (0.3 s)

Game Design Simplified

Creating Paddle vars

pygame.display.set_caption("Pong")
CLOCK = pygame.time.Clock()
 
# NEW CODE HERE
# Paddles
player = pygame.Rect(WIDTH-110, HEIGHT/2 -50, 10,100)
opponent = pygame.Rect(110, HEIGHT/2 -50, 10, 100)
opponent_score, player_score = 0, 0
# NEW CODE #END 

while True:                     # Keep Pygames active until closed

Paddle inputs

while True:
    # NEW CODE HERE
    keys_pressed = pygame.key.get_pressed()

    if keys_pressed[pygame.K_UP]:
        if player.top > 0:
            player.top -= 2
    if keys_pressed[pygame.K_DOWN]:
        if player.bottom < HEIGHT:
            player.top += 2

    if keys_pressed[pygame.K_w]:
            if opponent.top > 0:
                opponent.top -= 2
        if keys_pressed[pygame.K_s]:
            if opponent.bottom < HEIGHT:
                opponent.top += 2

Displaying Balls

# Paddle INPUTS
    ...

# NEW CODE HERE
SCREEN.fill("black")

pygame.draw.rect(SCREEN, "white", player)
pygame.draw.rect(SCREEN, "white", opponent)
# NEW CODE END

pygame.display.update()
CLOCK.tick(300)

Creating Ball vars

# Paddle vars 
    ... 

# Ball
    ball_radius = 20
    ball = pygame.Rect(WIDTH/2 -10, HEIGHT/2 -10, ball_radius,ball_radius)
    x_speed, y_speed = 1,1

Updating ball position

ball.x += x_speed * 2
ball.y += y_speed * 2

Drawing Ball

SCREEN.fill("black")

pygame.draw.rect(SCREEN, "white", player)
pygame.draw.rect(SCREEN, "white", opponent)

# NEW CODE HERE
pygame.draw.circle(SCREEN, 'white', ball.center,10)

pygame.display.update()
CLOCK.tick(300)

Game Design simplified

Detecting ball on edges

if ball.y + ball_radius >= HEIGHT or ball.y - ball_radius <= 0:
    y_speed *= -1

if ball.x <= 0:
    player_score += 1
    ball.center = (WIDTH/2, HEIGHT/2)
    x_speed, y_speed = random.choice([-1, 1]), random.choice([-1, 1])

if ball.x >= WIDTH:
    opponent_score += 1
    ball.center = (WIDTH/2, HEIGHT/2)
    x_speed, y_speed = random.choice([-1, 1]), random.choice([-1, 1])

Detecting ball on Paddle

if player.x - ball.width <= ball.x <= player.x and ball.y in range(player.top-ball.width, player.bottom+ball.width):    
    x_speed = -1
    # More Code soon

Calculating Angle to bounce ball from paddle

        x_speed = -1
        # NEW CODE HERE
        middleplayer = player.y + player.height/2
        differenceY =  ball.y - middleplayer
        y_speed = differenceY / (player.height/2)

Use Ultimate Techiinique for the opponent paddle detection

if opponent.x - ball.width <= ball.x <= opponent.x and ball.y in range(opponent.top-ball.width, opponent.bottom+ball.width):
            x_speed = 1

            middleopponent = opponent.y + opponent.height/2
            differenceY =  ball.y - middleopponent
            y_speed = differenceY / (opponent.height/2)

Creating new String var

player_score_text = FONT.render(str(player_score), True, "white")
opponent_score_text = FONT.render(str(opponent_score), True, "white")

Drawing Scores

# NEW CODE HERE
SCREEN.blit(player_score_text, (WIDTH/2+50, 50))
SCREEN.blit(opponent_score_text, (WIDTH/2-50, 50))
# NEW CODE END

pygame.display.update()
CLOCK.tick(300)

Creating dotted line

Game Design Simplified: Dotted lines are just rectangles repeating at an interval

def drawDotLine():
    for i in range(-16, HEIGHT, HEIGHT//14):
        pygame.draw.rect(SCREEN, (255,255,255), (WIDTH//2+10, i, 10, HEIGHT//20))
pygame.draw.rect(SCREEN, "white", player)
pygame.draw.rect(SCREEN, "white", opponent)
pygame.draw.circle(SCREEN, 'white', ball.center,10)

drawDotLine()           # NEW CODE HERE

Thank you for participating, ALSO please don't forget to check in, this is how we get money from the College of Engienering in the future

Fun Fact, Food costs money, and that is something we don't have
We are broke :(

Soly Image Caption