Skip to content

Instantly share code, notes, and snippets.

@anirudhjayaraman
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anirudhjayaraman/f22fbaf6779ba005d286 to your computer and use it in GitHub Desktop.
Save anirudhjayaraman/f22fbaf6779ba005d286 to your computer and use it in GitHub Desktop.
Code for IIPP Mini Project [Pong]
# Implementation of classic arcade game Pong by Anirudh Jayaraman
# coded on 24 June 2015 10:20 PM
from simplegui import *
from random import *
# 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
LEFT = False
RIGHT = True
paddle_vel_increment_rate = 4
pause = True; not_pause = not pause
game_status = 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]
if direction == RIGHT:
ball_vel = [randrange(2,5), -randrange(1,4)]
if direction == LEFT:
ball_vel = [-randrange(2,5), -randrange(1,4)]
# define event handlers
def new_game():
global paddle1_pos, paddle2_pos, paddle1_vel, paddle2_vel # these are numbers
global score1, score2 # these are ints
global pause, not_pause; pause = True; not_pause = not pause
paddle1_vel = paddle2_vel = 0
score1 = 0; score2 = 0
paddle1_pos, paddle2_pos = HEIGHT/2, HEIGHT/2
left_or_right = randrange(2) ## to randomly spawn the ball
if left_or_right == 1: ## towards left or right direction
left_or_right = RIGHT
else:
left_or_right = LEFT
spawn_ball(left_or_right)
def draw(canvas):
global score1, score2, paddle1_pos, paddle2_pos, ball_pos, ball_vel, ball_vel_temp
global paddle1_vel, paddle2_vel
global game_status
global frame
if game_status == 0:
frame.set_canvas_background("Yellow")
canvas.draw_text("Implementation of classic arcade game Pong by Anirudh Jayaraman", [20, 20], 15, "Red", "monospace")
canvas.draw_text("Press SPACEBAR to start!", [20, 100], 30, "Red", "monospace")
canvas.draw_text("~ RESTART button (top-left) starts a fresh game", [20, 150], 20, "Black", "monospace")
canvas.draw_text("~ W/S keys move LEFT PADDLE up/down", [20, 170], 20, "Black", "monospace")
canvas.draw_text("~ UP/DOWN ARROW keys move RIGHT PADDLE up/down", [20, 190], 20, "Black", "monospace")
canvas.draw_text("~ Pressing the 'P' key pauses / unpauses game", [20, 210], 20, "Black", "monospace")
canvas.draw_image(image, (600 / 2, 331 / 2), (600, 331), (300, 350), (600,150))
if game_status == 1:
frame.set_canvas_background("Green")
# draw mid line and gutters
canvas.draw_line([WIDTH / 2, 0],[WIDTH / 2, HEIGHT], 1, "White")
canvas.draw_line([PAD_WIDTH, 0],[PAD_WIDTH, HEIGHT], 1, "White")
canvas.draw_line([WIDTH - PAD_WIDTH, 0],[WIDTH - PAD_WIDTH, HEIGHT], 1, "White")
# update paddle's vertical position, keep paddle on the screen
if (HEIGHT - HALF_PAD_HEIGHT) >= (paddle1_pos + paddle1_vel) >= HALF_PAD_HEIGHT:
paddle1_pos += paddle1_vel
if (HEIGHT - HALF_PAD_HEIGHT) >= (paddle2_pos + paddle2_vel) >= HALF_PAD_HEIGHT:
paddle2_pos += paddle2_vel
# determine whether paddle and ball collide and update ball
if ball_pos[0] <= BALL_RADIUS + PAD_WIDTH:
if (paddle1_pos - HALF_PAD_HEIGHT) <= ball_pos[1] <= (paddle1_pos + HALF_PAD_HEIGHT):
ball_vel[0] = - 1.1 * ball_vel[0]
else:
spawn_ball(RIGHT)
score2 += 1
if score2 == 10:
pause_game()
if ball_pos[0] >= (WIDTH - BALL_RADIUS - PAD_WIDTH):
if (paddle2_pos - HALF_PAD_HEIGHT) <= ball_pos[1] <= (paddle2_pos + HALF_PAD_HEIGHT):
ball_vel[0] = - 1.1 * ball_vel[0]
else:
spawn_ball(LEFT)
score1 += 1
if score1 == 10:
pause_game()
if ball_pos[1] <= BALL_RADIUS:
ball_vel[1] = - ball_vel[1]
if ball_pos[1] >= (HEIGHT - BALL_RADIUS):
ball_vel[1] = - ball_vel[1]
ball_pos[0] += ball_vel[0]
ball_pos[1] += ball_vel[1]
# draw ball
canvas.draw_circle(ball_pos, BALL_RADIUS, 1, "Aqua", "Aqua" )
# draw paddles
canvas.draw_polygon([[0, paddle1_pos - HALF_PAD_HEIGHT],\
[0, paddle1_pos + HALF_PAD_HEIGHT],\
[PAD_WIDTH, paddle1_pos + HALF_PAD_HEIGHT],\
[PAD_WIDTH, paddle1_pos - HALF_PAD_HEIGHT]],1, "Yellow", "Yellow")
canvas.draw_polygon([[WIDTH - PAD_WIDTH, paddle2_pos - HALF_PAD_HEIGHT],\
[WIDTH - PAD_WIDTH, paddle2_pos + HALF_PAD_HEIGHT],\
[WIDTH, paddle2_pos + HALF_PAD_HEIGHT],\
[WIDTH, paddle2_pos - HALF_PAD_HEIGHT]],1, "Yellow", "Yellow")
# draw scores
canvas.draw_text(str(score1), [230,50], 40, "Yellow", "monospace")
canvas.draw_text(str(score2), [340,50], 40, "Yellow", "monospace")
if score2 == 10 or score1 == 10:
canvas.draw_text("Game Over", [120, 300], 70, "Yellow", "monospace")
# instructions on how to play
global instructions_line1; instructions_line1 = "W / S move left paddles up and down"
global instructions_line2; instructions_line2 = "Up / Down arrows move right paddles up and down"
canvas.draw_text(instructions_line1, [25, HEIGHT - 55], 12, "Yellow", "monospace")
canvas.draw_text(instructions_line2, [25, HEIGHT - 35], 12, "Yellow", "monospace")
canvas.draw_text("P to pause / unpause", [25, HEIGHT - 15], 12, "Yellow", "monospace")
def start_game():
global game_status; game_status = 1
def keydown(key):
global paddle1_vel, paddle2_vel, paddle_vel_increment_rate
if key == KEY_MAP["w"] and pause == True:
paddle1_vel = - paddle_vel_increment_rate
if key == KEY_MAP["s"] and pause == True:
paddle1_vel = + paddle_vel_increment_rate
if key == KEY_MAP["up"] and pause == True:
paddle2_vel = - paddle_vel_increment_rate
if key == KEY_MAP["down"] and pause == True:
paddle2_vel = + paddle_vel_increment_rate
if key == KEY_MAP["p"]:
pause_game()
if key == KEY_MAP["space"]:
start_game()
def keyup(key):
global paddle1_vel, paddle2_vel
if key == KEY_MAP["s"]:
paddle1_vel = 0
if key == KEY_MAP["w"]:
paddle1_vel = 0
if key == KEY_MAP["down"]:
paddle2_vel = 0
if key == KEY_MAP["up"]:
paddle2_vel = 0
if key == KEY_MAP["p"]:
pass
if key == KEY_MAP["space"]:
pass
def new_game_button():
global game_status; game_status = 1
new_game()
def pause_game():
global score1, score2, paddle1_pos, paddle2_pos, ball_pos, ball_vel, ball_vel_temp
global paddle1_vel, paddle2_vel, paddle1_vel_temp, paddle2_vel_temp
global pause;
if score1 == 10 or score2 == 10:
pause = True
if pause == True:
ball_vel_temp = ball_vel
paddle1_vel_temp = paddle1_vel; paddle2_vel_temp = paddle2_vel
ball_vel = [0, 0]
paddle1_vel = paddle2_vel = 0
else:
ball_vel = ball_vel_temp
paddle1_vel = paddle1_vel_temp; paddle2_vel = paddle2_vel_temp
pause = not pause
# create frame
frame = create_frame("Pong", WIDTH, HEIGHT)
image = load_image('http://1.media.dorkly.cvcdn.com/46/35/a9397db865facf904b469a0b2bf29625-quiz-which-pong-character-are-you.jpg')
frame.set_draw_handler(draw)
frame.set_keydown_handler(keydown)
frame.set_keyup_handler(keyup)
frame.add_button("START / RESTART", new_game_button, 150)
frame.add_button("PAUSE / UNPAUSE", pause_game, 150)
# start frame
frame.start()
new_game()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment