Make Pong with python

Download Python and install 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 Python
_ For Windows: py --version
_ For Mac: python3 --version
Check Pygame: pip show pygame
If you use another font change Satoshi-Variable.ttf to file name in the code,
Download Font.
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)
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
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
# 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)
# 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
ball.x += x_speed * 2
ball.y += y_speed * 2
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)
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])
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
x_speed = -1
# NEW CODE HERE
middleplayer = player.y + player.height/2
differenceY = ball.y - middleplayer
y_speed = differenceY / (player.height/2)
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)
player_score_text = FONT.render(str(player_score), True, "white")
opponent_score_text = FONT.render(str(opponent_score), True, "white")
# 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)
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 :(
