Created
July 22, 2015 18:22
-
-
Save fsjoyti/d10991a94fa727e7ee61 to your computer and use it in GitHub Desktop.
Pong The Python Game
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
# 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 | |
LEFT = False | |
RIGHT = True | |
# 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 | |
global LEFT,RIGHT | |
ball_pos = [WIDTH/2,HEIGHT/2] | |
ball_vel = [0,0] | |
if direction == 1: | |
LEFT = False | |
RIGHT = True | |
elif direction == 0: | |
LEFT = True | |
RIGHT = False | |
if (LEFT == False and RIGHT==True) : | |
ball_vel=[random.randrange(3, 6),-random.randrange(1, 3)] | |
elif (LEFT==True and RIGHT == False): | |
ball_vel=[-random.randrange(3,4),-random.randrange(2, 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 paddle1_pos, paddle2_pos, paddle1_vel, paddle2_vel # these are numbers | |
global score1, score2 # these are ints | |
paddle1_pos = [PAD_WIDTH/2, (HEIGHT-PAD_HEIGHT) / 2] | |
paddle2_pos = [WIDTH - PAD_WIDTH/2,PAD_HEIGHT / 2] | |
paddle1_vel = 0 | |
paddle2_vel = 0 | |
score1 = 0 | |
score2 = 0 | |
spawn_ball(random.randrange(2)) | |
#helper function to turn score 1 and 2 as strings | |
def display_score1(): | |
global score1 | |
return str(score1) | |
def display_score2(): | |
global score2 | |
return str(score2) | |
# helper funcction to update the position of the ball | |
def ball_Position(): | |
global ball_pos, ball_vel | |
ball_pos[0]+=ball_vel[0] | |
ball_pos[1]+=ball_vel[1] | |
#helper function to prevent the ball from bouncing off the canvas in the y direction | |
def keep_track_of_y_direction(): | |
global ball_pos, ball_vel | |
if ball_pos[1] <= BALL_RADIUS: | |
ball_vel[1] = - ball_vel[1] | |
elif ball_pos[1] >= (HEIGHT - BALL_RADIUS): | |
ball_vel[1] = - ball_vel[1] | |
#helper function to check and update paddle position | |
def update_paddle_position(): | |
global paddle1_pos, paddle2_pos, paddle1_vel, paddle2_vel | |
if (paddle1_pos[1] + paddle1_vel)>=0 and(paddle1_pos[1] + paddle1_vel) <= HEIGHT - PAD_HEIGHT: | |
paddle1_pos[1] += paddle1_vel | |
if (paddle2_pos[1] + paddle2_vel)>=0 and(paddle2_pos[1] + paddle2_vel) <= HEIGHT - PAD_HEIGHT: | |
paddle2_pos[1] += paddle2_vel | |
def draw(canvas): | |
global score1, score2, paddle1_pos, paddle2_pos, ball_pos, ball_vel | |
# 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 ball | |
if (ball_pos[0]<=BALL_RADIUS + PAD_WIDTH): | |
if (ball_pos[1]>=paddle2_pos[1] and ball_pos[1]<=(paddle2_pos[1] + PAD_HEIGHT)): | |
ball_vel[0] = -ball_vel[0]*1.1 | |
else : | |
spawn_ball(1) | |
score2 = score1+1 | |
elif ball_pos[0] >= (WIDTH - BALL_RADIUS - PAD_WIDTH): | |
if (ball_pos[1]>=paddle1_pos[1] and ball_pos[1]<=(paddle1_pos[1] + PAD_HEIGHT)): | |
ball_vel[0] = -ball_vel[0]*1.1 | |
else : | |
spawn_ball(0) | |
score1 = score1+1 | |
ball_Position() | |
keep_track_of_y_direction() | |
# draw ball | |
canvas.draw_circle(ball_pos, BALL_RADIUS, 0.1, "Yellow", "Yellow") | |
# update paddle's vertical position, keep paddle on the screen | |
update_paddle_position() | |
# draw paddles | |
canvas.draw_line([paddle1_pos[0], paddle1_pos[1]],[PAD_WIDTH/2,paddle1_pos[1]+ PAD_HEIGHT], PAD_WIDTH, "Red") | |
canvas.draw_line([paddle2_pos[0], paddle2_pos[1]],[WIDTH- PAD_WIDTH/2, paddle2_pos[1]+PAD_HEIGHT], PAD_WIDTH, "Red") | |
# determine whether paddle and ball collide | |
# draw scores | |
canvas.draw_text(display_score1(), (185, 40), 40, "Red") | |
canvas.draw_text(display_score2(), (400, 40), 40, "Red") | |
def keydown(key): | |
global paddle1_vel, paddle2_vel | |
paddle1_acc = 2 | |
paddle2_acc= 2 | |
if key==simplegui.KEY_MAP["s"]: | |
paddle1_vel += paddle1_acc | |
if key==simplegui.KEY_MAP["up"]: | |
paddle2_vel -= paddle2_acc | |
if key==simplegui.KEY_MAP["w"]: | |
paddle1_vel -= paddle1_acc | |
if key == simplegui.KEY_MAP["down"]: | |
paddle2_vel += paddle2_acc | |
def keyup(key): | |
global paddle1_vel, paddle2_vel | |
paddle_acc =2 | |
if key==simplegui.KEY_MAP["w"]: | |
paddle1_vel = 0 | |
if key==simplegui.KEY_MAP["down"]: | |
paddle2_vel = 0 | |
if key==simplegui.KEY_MAP["s"]: | |
paddle1_vel = 0 | |
if key == simplegui.KEY_MAP["up"]: | |
paddle2_vel = 0 | |
def restart(): | |
new_game() | |
# create frame | |
frame = simplegui.create_frame("Pong", WIDTH, HEIGHT) | |
frame.add_button("Restart",restart) | |
frame.set_draw_handler(draw) | |
frame.set_keydown_handler(keydown) | |
frame.set_keyup_handler(keyup) | |
# start frame | |
new_game() | |
frame.start() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment