Created
June 1, 2017 04:57
-
-
Save msanatan/6782356da867ccf9abca8844564e7600 to your computer and use it in GitHub Desktop.
Pygame Pong - not the way I would naturally code this, to be used in a class for teaching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import sys | |
import random | |
SCREEN_SIZE = WIDTH, HEIGHT = (800, 640) | |
BLACK = (0, 0, 0) | |
WHITE = (255, 255, 255) | |
GREEN = (50, 255, 50) | |
BLUE = (50, 50, 255) | |
SCORE_LIMIT = 5 | |
pygame.init() | |
font = pygame.font.SysFont('arial', 100) | |
screen = pygame.display.set_mode(SCREEN_SIZE) | |
pygame.display.set_caption('Pong') | |
clock = pygame.time.Clock() | |
paused = False | |
game_over = False | |
# Store player and ball data in dicts | |
ball = { | |
'x': 0, | |
'y': 0, | |
'colour': GREEN, | |
'radius': 10, | |
'velocity': { | |
'x': 3, | |
'y': 3 | |
}, | |
'original_velocity': { | |
'x': 3, | |
'y': 3 | |
} | |
} | |
player = { | |
'x': 0, | |
'y': 0, | |
'colour': WHITE, | |
'width': 10, | |
'height': 80, | |
'velocity': { | |
'x': 4, | |
'y': 4 | |
}, | |
'score': { | |
'value': 0, | |
'x': 80, | |
'y': 30 | |
} | |
} | |
opponent = { | |
'x': 0, | |
'y': 0, | |
'colour': WHITE, | |
'width': 10, | |
'height': 80, | |
'velocity': { | |
'x': 4, | |
'y': 4 | |
}, | |
'score': { | |
'value': 0, | |
'x': WIDTH - 150, | |
'y': 30 | |
} | |
} | |
def ball_init(): | |
ball['x'] = (WIDTH // 2) - ball['radius'] | |
ball['y'] = (HEIGHT // 2) - ball['radius'] | |
ball['velocity']['x'] = ball['original_velocity']['x'] * random.choice([-1, 1]) | |
ball['velocity']['y'] = ball['velocity']['y'] * random.choice([-1, 1]) | |
pygame.time.wait(1000) | |
def player_init(): | |
player['x'] = 10 | |
player['y'] = (HEIGHT // 2) - (player['height'] // 2) | |
def opponent_init(): | |
opponent['x'] = WIDTH - 10 - player['width'] | |
opponent['y'] = (HEIGHT // 2) - (player['height'] // 2) | |
def init(): | |
ball_init() | |
player_init() | |
opponent_init() | |
def within_y_range(paddle, ball): | |
return paddle['y'] <= ball['y'] <= paddle['y'] + paddle['height'] | |
def ball_update(): | |
# Check collision first | |
# Hit a paddle? | |
if ((ball['x'] - ball['radius'] <= player['x'] + player['width'] | |
and within_y_range(player, ball)) | |
or (ball['x'] + ball['radius'] >= opponent['x'] | |
and within_y_range(opponent, ball))): | |
ball['velocity']['x'] *= -1.2 | |
# Hit the top or bottom? | |
if ball['y'] - ball['radius'] <= 0 or ball['y'] + ball['radius'] >= HEIGHT: | |
ball['velocity']['y'] *= -1 | |
ball['x'] += int(ball['velocity']['x']) | |
ball['y'] += int(ball['velocity']['y']) | |
# Otherwise check for scoring | |
if ball['x'] - ball['radius'] <= player['x']: | |
opponent['score']['value'] += 1 | |
ball_init() | |
elif ball['x'] + ball['radius'] >= opponent['x'] + opponent['width']: | |
player['score']['value'] += 1 | |
ball_init() | |
def player_update(key_presses): | |
if player['y'] >= 0: | |
if key_presses[pygame.K_w] or key_presses[pygame.K_UP]: | |
player['y'] -= player['velocity']['y'] | |
if player['y'] + player['height'] <= HEIGHT: | |
if key_presses[pygame.K_s] or key_presses[pygame.K_DOWN]: | |
player['y'] += player['velocity']['y'] | |
def opponent_update(): | |
if ball['x'] >= WIDTH // 2: | |
if ball['y'] < opponent['y'] and opponent['y'] >= 0: | |
opponent['y'] -= opponent['velocity']['y'] | |
elif (ball['y'] > opponent['y'] | |
and (opponent['y'] + opponent['height']) <= HEIGHT): | |
opponent['y'] += opponent['velocity']['y'] | |
else: | |
if opponent['y'] < HEIGHT // 2: | |
opponent['y'] += opponent['velocity']['y'] | |
else: | |
opponent['y'] -= opponent['velocity']['y'] | |
def update(key_presses): | |
ball_update() | |
player_update(key_presses) | |
opponent_update() | |
def render(): | |
screen.fill(BLACK) | |
# Draw score | |
player_score = font.render(str(player['score']['value']), True, WHITE) | |
opponent_score = font.render(str(opponent['score']['value']), True, WHITE) | |
screen.blit(player_score, (player['score']['x'], player['score']['y'])) | |
screen.blit(opponent_score, (opponent['score']['x'], opponent['score']['y'])) | |
# Draw ball | |
pygame.draw.circle(screen, ball['colour'], (ball['x'], ball['y']), | |
ball['radius']) | |
# Draw players | |
pygame.draw.rect(screen, player['colour'], ((player['x'], player['y']), | |
(player['width'], player['height']))) | |
pygame.draw.rect(screen, opponent['colour'], ((opponent['x'], opponent['y']), | |
(opponent['width'], opponent['height']))) | |
if game_over: | |
game_over_score = font.render('Game Over', True, BLUE) | |
screen.blit(game_over_score, ((WIDTH // 2) - 275, (HEIGHT // 2) - 200)) | |
pygame.display.update() | |
clock.tick(60) | |
def check_game_over(): | |
return (player['score']['value'] == SCORE_LIMIT or | |
opponent['score']['value'] == SCORE_LIMIT) | |
# Setup game before loop starts | |
init() | |
# Start game loop | |
while True: | |
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_SPACE and not game_over: | |
paused = not paused | |
if not paused and not game_over: | |
game_over = check_game_over() | |
if not game_over: | |
update(pygame.key.get_pressed()) | |
render() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Open source the results haha