<gh_stars>0
# 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
ball_pos = [WIDTH /2 , HEIGHT/2]
ball_vel = [5,0]
score1 = 0
score2 = 0
paddle1_pos = [PAD_WIDTH, HEIGHT/2]
paddle2_pos = [600, HEIGHT/2]
paddle1_vel = 0
paddle2_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 # these are vectors stored as lists
    ball_pos = [WIDTH /2 , HEIGHT/2]
    if RIGHT == True: # upwards and towards the right
    ball_vel = [(random.randrange(120,240)/60),-(random.randrange(60,180)/60)]
    else: #upwards and towards the left if direction == LEFT.
    ball_vel = [-(random.randrange(120,240)/60),-(random.randrange(60,180)/60)]

# define event handlers
def new_game():
    global paddle1_pos, paddle2_pos, paddle1_vel, paddle2_vel  # these are numbers
    global score1, score2  # these are ints
    paddle1_pos = [PAD_WIDTH, HEIGHT/2]
    paddle2_pos = [600, HEIGHT/2]
    paddle1_vel = 0
    paddle2_vel = 0
    score1 = 0
    score2 = 0
    spawn_ball(LEFT)

    def draw(c):
        global score1, score2, paddle1_pos, paddle2_pos, ball_pos, ball_vel


    # draw mid line and gutters
    c.draw_line([WIDTH / 2, 0],[WIDTH / 2, HEIGHT], 1, "White")
    c.draw_line([PAD_WIDTH, 0],[PAD_WIDTH, HEIGHT], 1, "White")
    c.draw_line([WIDTH - PAD_WIDTH, 0],[WIDTH - PAD_WIDTH, HEIGHT], 1, "White")

    # update ball
    ball_pos[0] += ball_vel[0]
    ball_pos[1] += ball_vel[1]

    # collide and reflect off of top of canvas
    if ball_pos[1] <= BALL_RADIUS:
        ball_vel[1] = - ball_vel[1]

    # collide and reflect off of bottom canvas
    if ball_pos[1] >= (HEIGHT-1) - BALL_RADIUS:
        ball_vel[1] = - ball_vel[1]

    # collide and reflect off paddle
    if HALF_PAD_WIDTH+BALL_RADIUS<= ball_pos[0] <= WIDTH-PAD_WIDTH:
        if ball_pos[0] <= PAD_WIDTH+BALL_RADIUS:
            if paddle1_pos[1]-40 <= ball_pos[1] <= paddle1_pos[1]+40:
                ball_vel[0] = - (ball_vel[0]*1.01)
            elif ball_pos[0] >= WIDTH-PAD_WIDTH-BALL_RADIUS:
                if paddle2_pos[1]-40 <= ball_pos[1] <= paddle2_pos[1]+40:
                    ball_vel[0] = - (ball_vel[0]*1.01)
                elif (ball_pos[0] > WIDTH - PAD_WIDTH - BALL_RADIUS):
                    score1 += 1
                    global RIGHT, LEFT
                    RIGHT = False
                    LEFT = True
                    spawn_ball(LEFT)
                elif (ball_pos[0] < PAD_WIDTH):
                    score2 += 1
                    RIGHT = True
                    LEFT = False
                    spawn_ball(RIGHT)

    # draw ball
    c.draw_circle(ball_pos, BALL_RADIUS, 2, "Black", "White")

    # update paddle's vertical position, keep paddle on the screen
    if HALF_PAD_HEIGHT <= paddle1_pos[1] + paddle1_vel <= HEIGHT - HALF_PAD_HEIGHT:
        paddle1_pos[1] += paddle1_vel
        if HALF_PAD_HEIGHT <= paddle2_pos[1] + paddle2_vel <= HEIGHT - HALF_PAD_HEIGHT:
            paddle2_pos[1] += paddle2_vel

    # draw paddles
    c.draw_line([0, paddle1_pos[1]],[PAD_WIDTH, paddle1_pos[1]], PAD_HEIGHT, "White")
    c.draw_line([(600-PAD_WIDTH), paddle2_pos[1]],[600, paddle2_pos[1]], PAD_HEIGHT, "White")

    # draw scores
    c.draw_text (str(score1), [150, 50], 35, "White")
    c.draw_text (str(score2), [450, 50], 35, "White")
    c.draw_text ("W/S", [145, 385], 15, "White")
    c.draw_text ("UP/DOWN", [420, 385], 15, "White")
    c.draw_text ("Wong's Pong", [195,390] , 35, "White")

    def keydown(key):
        global paddle1_vel, paddle2_vel
        vel = 2
        if key == simplegui.KEY_MAP["up"]:
            paddle2_vel -= vel
        elif key == simplegui.KEY_MAP["down"]:
            paddle2_vel += vel
        elif key == simplegui.KEY_MAP["w"]:
            paddle1_vel -= vel
        elif key == simplegui.KEY_MAP["s"]:
            paddle1_vel += vel

            def keyup(key):
                global paddle1_vel, paddle2_vel
                if key == simplegui.KEY_MAP["up"]:
                    paddle2_vel = 0
                elif key == simplegui.KEY_MAP["down"]:
                    paddle2_vel = 0
                elif key == simplegui.KEY_MAP["w"]:
                    paddle1_vel = 0
                elif key == simplegui.KEY_MAP["s"]:
                    paddle1_vel = 0

# create frame
frame = simplegui.create_frame("Pong", WIDTH, HEIGHT)
frame.set_draw_handler(draw)
frame.set_keydown_handler(keydown)
frame.set_keyup_handler(keyup)
button1 = frame.add_button("Reset", new_game)

# start frame
new_game()
frame.start()

# 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
ball_pos = [WIDTH /2 , HEIGHT/2]
ball_vel = [5,0]
score1 = 0
score2 = 0
paddle1_pos = [PAD_WIDTH, HEIGHT/2]
paddle2_pos = [600, HEIGHT/2]
paddle1_vel = 0
paddle2_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 # these are vectors stored as lists
    ball_pos = [WIDTH /2 , HEIGHT/2]
    if RIGHT == True: # upwards and towards the right
    ball_vel = [(random.randrange(120,240)/60),-(random.randrange(60,180)/60)]
    else: #upwards and towards the left if direction == LEFT.
    ball_vel = [-(random.randrange(120,240)/60),-(random.randrange(60,180)/60)]

# define event handlers
def new_game():
    global paddle1_pos, paddle2_pos, paddle1_vel, paddle2_vel  # these are numbers
    global score1, score2  # these are ints
    paddle1_pos = [PAD_WIDTH, HEIGHT/2]
    paddle2_pos = [600, HEIGHT/2]
    paddle1_vel = 0
    paddle2_vel = 0
    score1 = 0
    score2 = 0
    spawn_ball(LEFT)

    def draw(c):
        global score1, score2, paddle1_pos, paddle2_pos, ball_pos, ball_vel


    # draw mid line and gutters
    c.draw_line([WIDTH / 2, 0],[WIDTH / 2, HEIGHT], 1, "White")
    c.draw_line([PAD_WIDTH, 0],[PAD_WIDTH, HEIGHT], 1, "White")
    c.draw_line([WIDTH - PAD_WIDTH, 0],[WIDTH - PAD_WIDTH, HEIGHT], 1, "White")

    # update ball
    ball_pos[0] += ball_vel[0]
    ball_pos[1] += ball_vel[1]

    # collide and reflect off of top of canvas
    if ball_pos[1] <= BALL_RADIUS:
        ball_vel[1] = - ball_vel[1]

    # collide and reflect off of bottom canvas
    if ball_pos[1] >= (HEIGHT-1) - BALL_RADIUS:
        ball_vel[1] = - ball_vel[1]

    # collide and reflect off paddle
    if HALF_PAD_WIDTH+BALL_RADIUS<= ball_pos[0] <= WIDTH-PAD_WIDTH:
        if ball_pos[0] <= PAD_WIDTH+BALL_RADIUS:
            if paddle1_pos[1]-40 <= ball_pos[1] <= paddle1_pos[1]+40:
                ball_vel[0] = - (ball_vel[0]*1.01)
            elif ball_pos[0] >= WIDTH-PAD_WIDTH-BALL_RADIUS:
                if paddle2_pos[1]-40 <= ball_pos[1] <= paddle2_pos[1]+40:
                    ball_vel[0] = - (ball_vel[0]*1.01)
                elif (ball_pos[0] > WIDTH - PAD_WIDTH - BALL_RADIUS):
                    score1 += 1
                    global RIGHT, LEFT
                    RIGHT = False
                    LEFT = True
                    spawn_ball(LEFT)
                elif (ball_pos[0] < PAD_WIDTH):
                    score2 += 1
                    RIGHT = True
                    LEFT = False
                    spawn_ball(RIGHT)

    # draw ball
    c.draw_circle(ball_pos, BALL_RADIUS, 2, "Black", "White")

    # update paddle's vertical position, keep paddle on the screen
    if HALF_PAD_HEIGHT <= paddle1_pos[1] + paddle1_vel <= HEIGHT - HALF_PAD_HEIGHT:
        paddle1_pos[1] += paddle1_vel
        if HALF_PAD_HEIGHT <= paddle2_pos[1] + paddle2_vel <= HEIGHT - HALF_PAD_HEIGHT:
            paddle2_pos[1] += paddle2_vel

    # draw paddles
    c.draw_line([0, paddle1_pos[1]],[PAD_WIDTH, paddle1_pos[1]], PAD_HEIGHT, "White")
    c.draw_line([(600-PAD_WIDTH), paddle2_pos[1]],[600, paddle2_pos[1]], PAD_HEIGHT, "White")

    # draw scores
    c.draw_text (str(score1), [150, 50], 35, "White")
    c.draw_text (str(score2), [450, 50], 35, "White")
    c.draw_text ("W/S", [145, 385], 15, "White")
    c.draw_text ("UP/DOWN", [420, 385], 15, "White")
    c.draw_text ("Wong's Pong", [195,390] , 35, "White")

    def keydown(key):
        global paddle1_vel, paddle2_vel
        vel = 2
        if key == simplegui.KEY_MAP["up"]:
            paddle2_vel -= vel
        elif key == simplegui.KEY_MAP["down"]:
            paddle2_vel += vel
        elif key == simplegui.KEY_MAP["w"]:
            paddle1_vel -= vel
        elif key == simplegui.KEY_MAP["s"]:
            paddle1_vel += vel

            def keyup(key):
                global paddle1_vel, paddle2_vel
                if key == simplegui.KEY_MAP["up"]:
                    paddle2_vel = 0
                elif key == simplegui.KEY_MAP["down"]:
                    paddle2_vel = 0
                elif key == simplegui.KEY_MAP["w"]:
                    paddle1_vel = 0
                elif key == simplegui.KEY_MAP["s"]:
                    paddle1_vel = 0

# create frame
frame = simplegui.create_frame("Pong", WIDTH, HEIGHT)
frame.set_draw_handler(draw)
frame.set_keydown_handler(keydown)
frame.set_keyup_handler(keyup)
button1 = frame.add_button("Reset", new_game)

# start frame
new_game()
frame.start()
# Implementation of classic arcade game Pong # initialize globals - pos and vel encode vertical info for paddles # 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 # these are vectors stored as lists # upwards and towards the right #upwards and towards the left if direction == LEFT. # define event handlers # these are numbers # these are ints # draw mid line and gutters # update ball # collide and reflect off of top of canvas # collide and reflect off of bottom canvas # collide and reflect off paddle # draw ball # update paddle's vertical position, keep paddle on the screen # draw paddles # draw scores # create frame # start frame