Created
July 30, 2018 18:02
-
-
Save mohdsaquib93/64f9924fdde315931fe99b297883c6de to your computer and use it in GitHub Desktop.
Basic version of Pong
This file contains hidden or 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
# Implementation of classic arcade game Pong | |
import simplegui | |
import random | |
# initialize globals - pos and vel encode vertical info for paddles | |
WIDTH = 600 | |
HEIGHT = 400 | |
BALL_RADIUS = 20 | |
PAD_WIDTH = 8 | |
PAD_HEIGHT = 80 | |
HALF_PAD_WIDTH = PAD_WIDTH / 2 | |
HALF_PAD_HEIGHT = PAD_HEIGHT / 2 | |
ball_pos = list() | |
ball_vel = list() | |
paddle1_pos = 0 | |
paddle2_pos = 0 | |
paddle1_vel = 0 | |
paddle2_vel = 0 | |
p1_score = 0 | |
p2_score = 0 | |
# initialize ball_pos and ball_vel for new bal in middle of table | |
# if direction is RIGHT, the ball's velocity is upper right, else upper left | |
def spawn_ball(direction): | |
global ball_pos, ball_vel # these are vectors stored as lists | |
ball_pos = [WIDTH / 2, HEIGHT / 2] | |
hval = random.randrange(2, 4) | |
vval = random.randrange(-3, -1) | |
if direction == 'RIGHT': | |
ball_vel = [hval, vval] | |
else: | |
ball_vel = [-hval, vval] | |
# define event handlers | |
def new_game(): | |
global paddle1_pos, paddle2_pos, paddle1_vel, paddle2_vel, ball_vel # these are numbers | |
global p1_score, p2_score # these are ints | |
paddle1_pos = HEIGHT / 3 | |
paddle2_pos = HEIGHT / 3 | |
p1_score = 0 | |
p2_score = 0 | |
paddle1_vel = 0 | |
paddle2_vel = 0 | |
ball_vel = list() | |
spawn_ball('RIGHT') | |
def draw(canvas): | |
global score1, score2, paddle1_pos, paddle2_pos, ball_pos, ball_vel, p1_score, p2_score | |
# draw mid line and gutters | |
canvas.draw_line([WIDTH / 2, 0], [WIDTH / 2, HEIGHT], 1, "Black") | |
canvas.draw_line([PAD_WIDTH, 0], [PAD_WIDTH, HEIGHT], 1, "Black") | |
canvas.draw_line([WIDTH - PAD_WIDTH, 0], | |
[WIDTH - PAD_WIDTH, HEIGHT], 1, "Black") | |
# update ball | |
ball_pos[0] += ball_vel[0] | |
ball_pos[1] += ball_vel[1] | |
if ball_pos[1] <= BALL_RADIUS or HEIGHT - ball_pos[1] <= BALL_RADIUS: | |
ball_vel[1] = -ball_vel[1] | |
elif ball_pos[0] <= BALL_RADIUS + PAD_WIDTH: | |
if ball_pos[1] >= paddle1_pos and ball_pos[1] <= paddle1_pos + PAD_HEIGHT: | |
ball_vel[0] *= 1.1 | |
ball_vel[0] = -ball_vel[0] | |
else: | |
ball_vel[0] = -ball_vel[0] | |
p2_score += 1 | |
spawn_ball('RIGHT') | |
elif WIDTH - ball_pos[0] <= BALL_RADIUS + PAD_WIDTH: | |
if ball_pos[1] >= paddle2_pos and ball_pos[1] <= paddle2_pos + PAD_HEIGHT: | |
ball_vel[0] *= 1.1 | |
ball_vel[0] = -ball_vel[0] | |
else: | |
ball_vel[0] = -ball_vel[0] | |
p1_score += 1 | |
spawn_ball('LEFT') | |
# draw ball | |
canvas.draw_circle(ball_pos, BALL_RADIUS, 2, 'Black', 'Red') | |
# update paddle's vertical position, keep paddle on the screen | |
paddle1_pos += paddle1_vel | |
if paddle1_pos <= 0: | |
paddle1_pos = 0 | |
elif paddle1_pos >= HEIGHT - PAD_HEIGHT: | |
paddle1_pos = HEIGHT - PAD_HEIGHT | |
paddle2_pos += paddle2_vel | |
if paddle2_pos <= 0: | |
paddle2_pos = 0 | |
elif paddle2_pos >= HEIGHT - PAD_HEIGHT: | |
paddle2_pos = HEIGHT - PAD_HEIGHT | |
paddle1_x = [HALF_PAD_WIDTH, paddle1_pos] | |
paddle1_y = [HALF_PAD_WIDTH, paddle1_pos + PAD_HEIGHT] | |
paddle2_x = [WIDTH - HALF_PAD_WIDTH, paddle2_pos] | |
paddle2_y = [WIDTH - HALF_PAD_WIDTH, paddle2_pos + PAD_HEIGHT] | |
# draw paddles | |
canvas.draw_line(paddle1_x, paddle1_y, PAD_WIDTH, 'Blue') | |
canvas.draw_line(paddle2_x, paddle2_y, PAD_WIDTH, 'Blue') | |
# draw scores | |
canvas.draw_text(str(p1_score), [200, 50], 25, 'Black') | |
canvas.draw_text(str(p2_score), [500, 50], 25, 'Black') | |
def keydown(key): | |
global paddle1_vel, paddle2_vel | |
if key == simplegui.KEY_MAP['w']: | |
paddle1_vel -= 3 | |
if key == simplegui.KEY_MAP['s']: | |
paddle1_vel += 3 | |
if key == simplegui.KEY_MAP['up']: | |
paddle2_vel -= 3 | |
if key == simplegui.KEY_MAP['down']: | |
paddle2_vel += 3 | |
def keyup(key): | |
global paddle1_vel, paddle2_vel | |
paddle1_vel = 0 | |
paddle2_vel = 0 | |
def button(): | |
new_game() | |
# create frame | |
frame = simplegui.create_frame("Pong", WIDTH, HEIGHT) | |
frame.add_button('Restart', button) | |
frame.set_draw_handler(draw) | |
frame.set_keydown_handler(keydown) | |
frame.set_keyup_handler(keyup) | |
frame.set_canvas_background('White') | |
# start frame | |
new_game() | |
frame.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment