Skip to content

Instantly share code, notes, and snippets.

@aneury1
Last active April 3, 2024 06:43
Show Gist options
  • Save aneury1/e659bc90ae490d8da7f963e2b9a7e155 to your computer and use it in GitHub Desktop.
Save aneury1/e659bc90ae490d8da7f963e2b9a7e155 to your computer and use it in GitHub Desktop.
import sys
import random
import pygame
# FROM https://www.youtube.com/watch?v=o-6pADy5Mdg
pygame.init()
screen_width= 1280
screen_height=900
clock = pygame.time.Clock()
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('pong')
ball = pygame.Rect(screen_width/2-16, screen_height/2-16, 30,30)
player = pygame.Rect(screen_width-20, screen_height/2, 10, 140)
opponent = pygame.Rect(10, screen_height/2 -70, 10, 140)
font = pygame.font.Font(None, 36) # None means default font, 36 is the size
bx = 7
by = 7
pspeed = 0
ospeed = 0
bg_color = pygame.Color('grey12')
light_grey = (200,200,200)
pscore=0
oscore=0
text_content = "Hello, Pygame!"
# Get the rectangle bounding the text surface
def print_text(x,y,text_content):
global screen
global font
# Render the text
text_color = pygame.Color("white")
text_surface = font.render(text_content, True, text_color)
text_rect = text_surface.get_rect(center=(x, y))
# Blit the text surface onto the screen
screen.blit(text_surface, text_rect)
def proc_event():
global pspeed
global ospeed
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
pspeed += 7
if event.key == pygame.K_UP:
pspeed -= 7
if event.key == pygame.K_DOWN:
ospeed += 7
if event.key == pygame.K_UP:
ospeed -= 7
if event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN:
pspeed -= 7
if event.key == pygame.K_UP:
pspeed += 7
if event.key == pygame.K_DOWN:
ospeed -= 7
if event.key == pygame.K_UP:
ospeed += 7
def update_ball():
ball.x += bx
ball.y += by
player.y += pspeed
opponent.y += ospeed
def reset_ball_on_collition():
global bx
global by
global ball
ball.center = (screen_width / 2, screen_height/2)
bx *= random.choice((-1,1))
by *= random.choice((-1,1))
def check_ball_animation_physics():
## Ball Animation
global by
global bx
if(ball.top <= 0 or ball.bottom >= screen_height):
by *= -1
if(ball.left <= 0 or ball.right >= screen_width):
global pscore
global oscore
if ball.left <= 0:
pscore +=1
else:
oscore +=1
reset_ball_on_collition()
if(ball.colliderect(player) or ball.colliderect(opponent)):
bx *= -1
def check_and_update_player_opponent_physics():
# Player
if player.top <= 0:
player.top =0
if player.bottom >= screen_height:
player.bottom = screen_height
# Opponet
if opponent.top <= 0:
opponent.top =0
if opponent.bottom >= screen_height:
opponent.bottom = screen_height
def display_entities():
pygame.draw.rect(screen, light_grey, player)
pygame.draw.rect(screen, light_grey, opponent)
pygame.draw.ellipse(screen, light_grey, ball)
pygame.draw.aaline(screen, light_grey, (screen_width/2,0), (screen_width/2,screen_height))
score = str(f"P1: {pscore} VS P2: {oscore}")
print_text(screen_width/2-100,10, score)
while True:
proc_event()
update_ball()
check_ball_animation_physics()
check_and_update_player_opponent_physics()
screen.fill(bg_color)
display_entities()
pygame.display.flip()
clock.tick(60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment