# Implementation of classic arcade game Pong# 2018-Jan-23 by Louisaimport simpleguiimport random# Initialization of variablesWIDTH = 600HEIGHT = 400       BALL_RADIUS = 20PAD_WIDTH = 8PAD_HEIGHT = 80HALF_PAD_WIDTH = PAD_WIDTH / 2HALF_PAD_HEIGHT = PAD_HEIGHT / 2LEFT = FalseRIGHT = Truepaddle1_pos = HEIGHT/2paddle2_pos = HEIGHT/2paddle1_vel = 0paddle2_vel = 0# Ball starts in the middle of the table with no velocityball_pos = WIDTH/2, HEIGHT/2ball_vel = 0,0# if direction is RIGHT, the ball’s velocity is upper right, else upper leftdef spawn_ball(direction):    global ball_pos, ball_vel # these are vectors stored as lists    ball_pos = WIDTH/2,HEIGHT/2    ball_vel0 = (random.randrange(120,240))/60    ball_vel1 = (-random.randrange(60,80))/60    if direction == RIGHT:        ball_vel0 = ball_vel0        ball_vel1 = ball_vel1    elif direction == LEFT:        ball_vel0 = -ball_vel0        ball_vel1 = ball_vel1    return ball_vel    # define event handlersdef new_game():    global paddle1_pos, paddle2_pos    global score1, score2    paddle1_pos = HEIGHT/2 #Return paddles to the middle on new game    paddle2_pos = HEIGHT/2     score1 = 0 #Reset scores to zero on new game    score2 = 0    spawn_ball(random.choice(LEFT,RIGHT))#Ball goes either up left or up rightdef draw(canvas):    global score1, score2    global paddle1_pos, paddle2_pos, ball_pos     global ball_vel, paddle1_vel, paddle2_vel        # Center, Left and Right gutters respectively    canvas.draw_line(WIDTH / 2, 0,WIDTH / 2, HEIGHT, 1, “White”)    canvas.draw_line(PAD_WIDTH, 0,PAD_WIDTH, HEIGHT, 1, “Red”)    canvas.draw_line(WIDTH – PAD_WIDTH, 0,WIDTH – PAD_WIDTH, HEIGHT, 1, “Red”)            # update ball    ball_pos0 += ball_vel0    ball_pos1 += ball_vel1        #Change direction if ball collides with vertical walls    if ball_pos1 <= BALL_RADIUS or ball_pos1 >= HEIGHT – BALL_RADIUS:        ball_vel1 = – ball_vel1        #Check Restart if ball collides with left or right gutters    if ball_pos0 >= WIDTH – PAD_WIDTH:        spawn_ball(LEFT)        score1 += 1    elif ball_pos0 <= PAD_WIDTH:        spawn_ball(RIGHT)        score2 += 1         # draw ball    canvas.draw_circle(ball_pos,BALL_RADIUS,1,"White","White")        # Updating paddle's vertical positions separately        #Paddle 1    if paddle1_pos + paddle1_vel <= HALF_PAD_HEIGHT:        paddle1_pos = HALF_PAD_HEIGHT    elif paddle1_pos + paddle1_vel >= HEIGHT – HALF_PAD_HEIGHT:        paddle1_pos = HEIGHT – HALF_PAD_HEIGHT    else:        paddle1_pos += paddle1_vel                #Paddle 2    if paddle2_pos + paddle2_vel <= HALF_PAD_HEIGHT:        paddle2_pos = HALF_PAD_HEIGHT    elif paddle2_pos + paddle2_vel >= HEIGHT – HALF_PAD_HEIGHT:        paddle2_pos = HEIGHT – HALF_PAD_HEIGHT    else:        paddle2_pos += paddle2_vel            # draw paddles    #Left and Right paddles    canvas.draw_line(HALF_PAD_WIDTH, paddle1_pos – HALF_PAD_HEIGHT,HALF_PAD_WIDTH, paddle1_pos + HALF_PAD_HEIGHT, PAD_WIDTH, “Lime”)    canvas.draw_line(WIDTH-HALF_PAD_WIDTH, paddle2_pos – HALF_PAD_HEIGHT,WIDTH – HALF_PAD_WIDTH, paddle2_pos + HALF_PAD_HEIGHT, PAD_WIDTH, “Cyan”)        # Check whether paddle and ball collide and if they do then increase velocity by 10%    #If true new velocity is 110%    if ball_pos0 >= WIDTH – PAD_WIDTH – BALL_RADIUS:        if (ball_pos1 < paddle2_pos + HALF_PAD_HEIGHT and ball_pos1 > paddle2_pos – HALF_PAD_HEIGHT):            ball_vel0 = -(1.10 * ball_vel0)            ball_vel1 = (1.10 * ball_vel1)       elif ball_pos0 <= PAD_WIDTH + BALL_RADIUS:        if ball_pos1 < paddle1_pos + HALF_PAD_HEIGHT and ball_pos1 > paddle1_pos – HALF_PAD_HEIGHT:            ball_vel0 = -(1.10 * ball_vel0)            ball_vel1 = (1.10 * ball_vel1)              # draw scores    canvas.draw_text(str(score1),WIDTH/2 – WIDTH/4,50,46,”Lime”)    canvas.draw_text(str(score2),WIDTH/2 + WIDTH/4,50,46,”Cyan”)        def keydown(key):    global paddle1_vel, paddle2_vel #Paddles increase at a constant velocity of 3    if key == simplegui.KEY_MAP”w”:        paddle1_vel -= 3    elif key == simplegui.KEY_MAP”s”:        paddle1_vel += 3    if key == simplegui.KEY_MAP”up”:        paddle2_vel -= 3    elif key == simplegui.KEY_MAP”down”:        paddle2_vel += 3    return paddle1_vel, paddle2_vel       def keyup(key):    global paddle1_vel, paddle2_vel    if key == simplegui.KEY_MAP”w”:        paddle1_vel = 0    elif key == simplegui.KEY_MAP”s”:        paddle1_vel = 0    if key == simplegui.KEY_MAP”up”:        paddle2_vel = 0    elif key == simplegui.KEY_MAP”down”:        paddle2_vel = 0    return paddle1_vel, paddle2_vel# create frameframe = simplegui.create_frame(“Pong”, WIDTH, HEIGHT)frame.set_draw_handler(draw)frame.set_keydown_handler(keydown)frame.set_keyup_handler(keyup)reset = frame.add_button(‘Restart the Game’, new_game)# start framenew_game()frame.start()

x

Hi!
I'm Katy!

Would you like to get a custom essay? How about receiving a customized one?

Check it out