-
-
Save soleshka/0d3705e8e5cac85e638a9ca698297c07 to your computer and use it in GitHub Desktop.
Simple pong game . To execute under codeskulptor.org
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
| # Mini-project #6 - Pong | |
| """ | |
| This game implements the Pong game. | |
| Use up/wodn and w/s to control the paddles | |
| Date : 12/29/2017 | |
| Implementor : Oleg Greenberg | |
| Environmnet : codeskulptor | |
| """ | |
| # 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 | |
| paddle1_pos = [HALF_PAD_WIDTH,HEIGHT/2] | |
| paddle2_pos = [WIDTH - HALF_PAD_WIDTH,HEIGHT/2] | |
| ball_pos = [WIDTH/2,HEIGHT/2] | |
| ball_rad = 20 | |
| ball_col1 = 'Yellow' | |
| ball_col2 = 'White' | |
| ball_vel = [random.randrange(120, 240)/80,random.randrange(60, 180)/80] | |
| paddle1_vel = [0,0] | |
| paddle2_vel = [0,0] | |
| vel_incr = 3 | |
| vel_speedup = 1.2 | |
| end_game_b = False | |
| score1 = 0 | |
| score2 = 0 | |
| score_size = 40 | |
| score1_color = "Red" | |
| score2_color = "Blue" | |
| #check bounderies bounce | |
| def bounce_pos_vert(): | |
| global ball_vel ,ball_pos | |
| #down side | |
| if ball_pos[1] + ball_rad >= HEIGHT: | |
| ball_vel[1] = - ball_vel[1] | |
| if ball_pos[1] - ball_rad <= 0: | |
| ball_vel[1] =- ball_vel[1] | |
| ball_pos[1] += ball_vel[1] | |
| def bounce_pos_horiz(): | |
| global ball_vel ,ball_pos , paddle2_vel ,paddle1_vel | |
| #right side | |
| if (ball_pos[0] + ball_rad) >= (WIDTH - PAD_WIDTH)\ | |
| and ball_pos[1] in range(int(paddle2_pos[1])-HALF_PAD_HEIGHT,int(paddle2_pos[1])+HALF_PAD_HEIGHT): #ball is in width of the paddle | |
| ball_vel[0] =- vel_speedup*ball_vel[0] | |
| paddle2_vel[1] = vel_speedup*paddle2_vel[1] | |
| paddle1_vel[1] = vel_speedup*paddle1_vel[1] | |
| #print "right side", ball_vel[0] | |
| if (ball_pos[0] - ball_rad) <= PAD_WIDTH\ | |
| and ball_pos[1] in range(int(paddle1_pos[1])-HALF_PAD_HEIGHT,int(paddle1_pos[1])+HALF_PAD_HEIGHT): #ball is in width of the paddle | |
| ball_vel[0] =- vel_speedup*ball_vel[0] | |
| paddle2_vel[1] = vel_speedup*paddle2_vel[1] | |
| paddle1_vel[1] = vel_speedup*paddle1_vel[1] | |
| # print "left side", ball_vel[0] | |
| ball_pos[0] += ball_vel[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,end_game_b # these are vectors stored as lists | |
| ball_pos = [WIDTH/2,HEIGHT/2] | |
| ball_vel = [random.randrange(120, 240)/80,random.randrange(60, 180)/80] | |
| end_game_b = False | |
| ball_vel[1] =-ball_vel[1] | |
| if direction == RIGHT: | |
| pass | |
| elif direction == LEFT: | |
| ball_vel[0] =-ball_vel[0] | |
| else: | |
| print "[ERROR]: Bad direction input. Eciting.." | |
| quit() | |
| # 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 ball_pos,ball_vel | |
| score1 = 0 | |
| score2 = 0 | |
| direction = random.randrange(2) | |
| spawn_ball(bool(direction)) | |
| 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 | |
| bounce_pos_vert() | |
| bounce_pos_horiz() | |
| # draw ball | |
| canvas.draw_circle(ball_pos, ball_rad, 5, ball_col2, ball_col1) | |
| # update paddle's vertical position, keep paddle on the screen | |
| if (paddle1_pos[1] - HALF_PAD_HEIGHT + paddle1_vel[1]>=0 and\ | |
| paddle1_pos[1] + HALF_PAD_HEIGHT + paddle1_vel[1] <=HEIGHT): | |
| paddle1_pos[1] += paddle1_vel[1] | |
| if (paddle2_pos[1] - HALF_PAD_HEIGHT + paddle2_vel[1]>=0 and\ | |
| paddle2_pos[1] + HALF_PAD_HEIGHT + paddle2_vel[1] <=HEIGHT): | |
| paddle2_pos[1] += paddle2_vel[1] | |
| # draw paddles | |
| p1 = [0,paddle1_pos[1]-HALF_PAD_HEIGHT] | |
| p2 = [PAD_WIDTH,paddle1_pos[1]-HALF_PAD_HEIGHT] | |
| p3 = [PAD_WIDTH,paddle1_pos[1]+HALF_PAD_HEIGHT] | |
| p4 = [0,paddle1_pos[1]+HALF_PAD_HEIGHT] | |
| canvas.draw_polygon([p1,p2,p3,p4], 12, score1_color) | |
| p1 = [WIDTH,paddle2_pos[1]-HALF_PAD_HEIGHT] | |
| p2 = [WIDTH-PAD_WIDTH,paddle2_pos[1]-HALF_PAD_HEIGHT] | |
| p3 = [WIDTH-PAD_WIDTH,paddle2_pos[1]+HALF_PAD_HEIGHT] | |
| p4 = [WIDTH,paddle2_pos[1]+HALF_PAD_HEIGHT] | |
| canvas.draw_polygon([p1,p2,p3,p4], 12, score2_color) | |
| # determine whether paddle and ball collide | |
| direction = determine_colide() | |
| # draw scores | |
| if end_game_b: | |
| spawn_ball(direction) | |
| canvas.draw_text(str(score1),(WIDTH/4,HEIGHT/4),score_size,score1_color) | |
| canvas.draw_text(str(score2),(WIDTH-WIDTH/4,HEIGHT/4),score_size,score2_color) | |
| def determine_colide(): | |
| global end_game_b,score1,score2 | |
| if ball_pos[0]+ball_rad > WIDTH: | |
| end_game_b = True | |
| score1 +=1 | |
| return LEFT | |
| if ball_pos[0] -ball_rad < 0: | |
| end_game_b = True | |
| score2 +=1 | |
| return RIGHT | |
| def keydown(key): | |
| global paddle1_vel, paddle2_vel | |
| #update paddle right | |
| if key == simplegui.KEY_MAP["up"]: | |
| paddle2_vel[1] = -vel_incr | |
| elif key == simplegui.KEY_MAP["down"]: | |
| paddle2_vel[1] = vel_incr | |
| #update paddle left | |
| if key == simplegui.KEY_MAP["w"]: | |
| paddle1_vel[1] = -vel_incr | |
| elif key == simplegui.KEY_MAP["s"]: | |
| paddle1_vel[1] = vel_incr | |
| def keyup(key): | |
| global paddle1_vel, paddle2_vel | |
| #update paddle right | |
| if key == simplegui.KEY_MAP["up"]: | |
| paddle2_vel[1] = 0 | |
| elif key == simplegui.KEY_MAP["down"]: | |
| paddle2_vel[1] = 0 | |
| #update paddle left | |
| if key == simplegui.KEY_MAP["w"]: | |
| paddle1_vel[1] = 0 | |
| elif key == simplegui.KEY_MAP["s"]: | |
| paddle1_vel[1] = 0 | |
| #button reset handler | |
| def btn_reset_hl(): | |
| new_game() | |
| # create frame | |
| frame = simplegui.create_frame("Pong", WIDTH, HEIGHT) | |
| frame.set_draw_handler(draw) | |
| frame.set_keydown_handler(keydown) | |
| frame.set_keyup_handler(keyup) | |
| btn_reset = frame.add_button('Restart',btn_reset_hl) | |
| # start frame | |
| new_game() | |
| frame.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment