Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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
- ball_pos = [WIDTH/2, HEIGHT/2]
- if direction:
- ball_vel = [- (random.randrange(120, 240)) / 60.0, - (random.randrange(60, 180)) / 60.0]
- else:
- ball_vel = [random.randrange(120, 240) / 60.0, - (random.randrange(60, 180)) / 60.0]
- # define event handlers
- def new_game():
- global pad1_pos, pad2_pos, pad1_vel, pad2_vel, speed # these are numbers
- global score1, score2 # these are ints
- pad1_pos = HEIGHT / 2 - HALF_PAD_HEIGHT
- pad2_pos = HEIGHT / 2 - HALF_PAD_HEIGHT
- pad1_vel = 0
- pad2_vel = 0
- score1 = 0
- score2 = 0
- spawn_ball(random.randrange(2)) # Use random to choose initial direction
- def draw(canvas):
- global score1, score2, pad1_pos, pad2_pos, ball_pos, ball_vel
- # draw scores
- canvas.draw_text(str(score1), [WIDTH - WIDTH / 4, HEIGHT/3], 40, 'Yellow')
- canvas.draw_text(str(score2), [WIDTH / 4, HEIGHT/3], 40, 'Yellow')
- # draw mid line and gutters
- canvas.draw_line([WIDTH / 2, 0],[WIDTH / 2, HEIGHT], 1, "Orange")
- canvas.draw_line([PAD_WIDTH, 0],[PAD_WIDTH, HEIGHT], 1, "Orange")
- canvas.draw_line([WIDTH - PAD_WIDTH, 0],[WIDTH - PAD_WIDTH, HEIGHT], 1, "Orange")
- # update ball
- if ball_pos[0] <= PAD_WIDTH + BALL_RADIUS:
- if (pad1_pos <= ball_pos[1]) and (pad1_pos + PAD_HEIGHT >= ball_pos[1]):
- ball_vel[0] = - ball_vel[0]
- ball_vel[0] = ball_vel[0] + ball_vel[0]/ 10.0
- ball_vel[1] = ball_vel[1] + ball_vel[1]/ 10.0
- else:
- spawn_ball(0)
- score1 += 1
- elif ball_pos[0] >= WIDTH - PAD_WIDTH - BALL_RADIUS:
- if (pad2_pos <= ball_pos[1]) and (pad2_pos + PAD_HEIGHT >= ball_pos[1]):
- ball_vel[0] = - ball_vel[0]
- ball_vel[0] = ball_vel[0] + ball_vel[0]/ 10.0
- ball_vel[1] = ball_vel[1] + ball_vel[1]/ 10.0
- else:
- spawn_ball(1)
- score2 += 1
- elif (ball_pos[1] <= 0 + BALL_RADIUS) or (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, 'Navy', 'Navy')
- # update paddle's vertical position, keep paddle on the screen
- if 0 <= pad1_pos + pad1_vel <= HEIGHT - PAD_HEIGHT:
- pad1_pos += pad1_vel
- if 0 <= pad2_pos + pad2_vel <= HEIGHT - PAD_HEIGHT:
- pad2_pos += pad2_vel
- # draw paddles
- canvas.draw_line([HALF_PAD_WIDTH, pad1_pos], [HALF_PAD_WIDTH, pad1_pos + PAD_HEIGHT], PAD_WIDTH, 'Orange')
- canvas.draw_line([WIDTH - HALF_PAD_WIDTH, pad2_pos], [WIDTH - HALF_PAD_WIDTH, pad2_pos + PAD_HEIGHT], PAD_WIDTH, 'Orange')
- # define key input
- def keydown(key):
- global pad1_vel, pad2_vel
- speed = 10
- if key == simplegui.KEY_MAP['w']:
- pad1_vel -= speed
- elif key == simplegui.KEY_MAP['s']:
- pad1_vel += speed
- elif key == simplegui.KEY_MAP['up']:
- pad2_vel -= speed
- elif key == simplegui.KEY_MAP['down']:
- pad2_vel += speed
- def keyup(key):
- global pad1_vel, pad2_vel
- speed = 10
- if key == simplegui.KEY_MAP['w']:
- pad1_vel += speed
- elif key == simplegui.KEY_MAP['s']:
- pad1_vel -= speed
- elif key == simplegui.KEY_MAP['up']:
- pad2_vel += speed
- elif key == simplegui.KEY_MAP['down']:
- pad2_vel -= speed
- # create frame, register handlers
- frame = simplegui.create_frame("Pong", WIDTH, HEIGHT)
- frame.set_canvas_background('Olive')
- frame.set_draw_handler(draw)
- frame.set_keydown_handler(keydown)
- frame.set_keyup_handler(keyup)
- frame.add_button('Reset', new_game)
- # start frame
- new_game()
- frame.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement