Skip to content

Instantly share code, notes, and snippets.

@00p513-dev
Created March 12, 2021 18:17
Show Gist options
  • Save 00p513-dev/1d02e93d80da489a82c154ed942b5b78 to your computer and use it in GitHub Desktop.
Save 00p513-dev/1d02e93d80da489a82c154ed942b5b78 to your computer and use it in GitHub Desktop.
PyGame Pong
# Import the pygame module
import pygame
# Import pygame.locals for easier access to key coordinates
from pygame.locals import (
K_UP,
K_DOWN,
K_ESCAPE,
KEYDOWN,
QUIT,
)
# Initialize pygame
pygame.init()
# Define constants for the screen width and height
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
# Create the screen object
# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Define a Player object by extending pygame.sprite.Sprite
# The surface drawn on the screen is now an attribute of 'player'
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.surf = pygame.Surface((15, 50))
self.surf.fill((255, 255, 255))
self.rect = self.surf.get_rect()
self.rect.left = 15
self.rect.top = (SCREEN_HEIGHT / 2)
# Move the sprite based on user keypresses
def update(self, pressed_keys):
if pressed_keys[K_UP]:
self.rect.move_ip(0, -5)
if pressed_keys[K_DOWN]:
self.rect.move_ip(0, 5)
# Keep player on the screen
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH
if self.rect.top <= 0:
self.rect.top = 0
if self.rect.bottom >= SCREEN_HEIGHT:
self.rect.bottom = SCREEN_HEIGHT
class Ball(pygame.sprite.Sprite):
x = 2
y = 1
def __init__(self):
super(Ball, self).__init__()
self.surf = pygame.Surface((10, 10))
self.surf.fill((255, 255, 255))
self.rect = self.surf.get_rect()
self.rect.left = 100
self.rect.top = (SCREEN_HEIGHT / 2)
def update(self, pressed_keys):
self.rect.move_ip(self.x, self.y)
# Bounce ball against the paddle/player
if pygame.sprite.collide_rect(self, player):
self.x = self.x * -1
# Keep ball on the screen
if self.rect.left < 0:
self.x = self.x * -1
if self.rect.right > SCREEN_WIDTH:
self.x = self.x * -1
if self.rect.top <= 0:
self.y = self.y * -1
if self.rect.bottom >= SCREEN_HEIGHT:
self.y = self.y * -1
# Instantiate player.
player = Player()
# Instantiate ball.
ball = Ball()
# Variable to keep the main loop running
running = True
# Main loop
while running:
# Look at every event in the queue
for event in pygame.event.get():
# Did the user hit a key?
if event.type == KEYDOWN:
# Was it the Escape key? If so, stop the loop.
if event.key == K_ESCAPE:
running = False
# Did the user click the window close button? If so, stop the loop.
elif event.type == QUIT:
running = False
# Get the set of keys pressed and check for user input
pressed_keys = pygame.key.get_pressed()
# Update the player sprite based on user keypresses
player.update(pressed_keys)
# Move the ball
ball.update(pressed_keys)
# Fill the screen with black
screen.fill((0, 0, 0))
# Draw the player on the screen
screen.blit(player.surf, player.rect)
# Draw the ball on the screen
screen.blit(ball.surf, ball.rect)
# Update the display
pygame.display.flip()
pygame.display.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment