Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project #4 —"Pong"

In this project, we will build a version of Pong, one of the first arcade video games (1972). While Pong is not particularly exciting compared to today's video games, Pong is relatively simple to build and provides a nice opportunity to work on the skills that you will need to build a game likeAsteroids. As usual, we have provided a program template that can be used to guide your development of Pong.

 

# 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
    
    if direction == RIGHT:
        #ball_vel[0] = random.randrange(2, 4)
        #ball_vel[1] = random.randrange(1, 3)
        ball_vel[0] += 1 
        ball_vel[1] += 1   
       
    elif direction == LEFT:
        #ball_vel[0] = - random.randrange(2, 4)
        #ball_vel[1] = random.randrange(1, 3)
        ball_vel[0] -= 1 
        ball_vel[1] -= 1 
        
def ini_pad():
    global score1, score2, paddle1_pos, paddle1_p, paddle2_pos, paddle2_p, paddle1_vel, paddle2_vel
    paddle1_p = HEIGHT / 2 - HALF_PAD_HEIGHT
    paddle2_p = HEIGHT / 2 - HALF_PAD_HEIGHT
    score1 = 0
    score2 = 0
    paddle1_vel = 0
    paddle2_vel = 0
    paddle1_pos = [[0,0], [0,0], [0,0], [0,0]]
    paddle2_pos = [[0,0], [0,0], [0,0], [0,0]]
    
# define event handlers
def new_game():
    global paddle1_pos, paddle1_p, paddle2_pos, paddle2_p, paddle1_vel, paddle2_vel  # these are numbers
    global score1, score2  # these are ints
    global ball_pos, ball_vel
    
    ball_pos = [WIDTH / 2, HEIGHT / 2]
    ball_vel = [1, 1]
    ini_pad()
    spawn_ball(RIGHT)

def draw(canvas):
    global score1, score2, ball_pos, ball_vel
    global paddle1_pos, paddle1_p, paddle2_pos, paddle2_p, paddle1_vel, paddle2_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
    ball_pos[0] += ball_vel[0]  
    ball_pos[1] += ball_vel[1]
    #collision detection
    if ball_pos[0] <=  BALL_RADIUS + PAD_WIDTH:
        ball_vel[0] = - ball_vel[0]
    elif ball_pos[0] >= WIDTH - PAD_WIDTH - BALL_RADIUS:
        ball_vel[0] = - ball_vel[0]
    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]
    # draw ball
    canvas.draw_circle(ball_pos, BALL_RADIUS, 5, 'Blue', 'White')
    # update paddle's vertical position, keep paddle on the screen

    paddle1_p += paddle1_vel 
    if paddle1_p <= 0:
        paddle1_P = 0
    if paddle1_p >= HEIGHT - PAD_WIDTH:
        paddle1_p = HEIGHT - PAD_WIDTH
    paddle1_pos[0] = [0, paddle1_p]
    paddle1_pos[1] = [PAD_WIDTH, paddle1_p]
    paddle1_pos[2] = [PAD_WIDTH, paddle1_p + PAD_HEIGHT]
    paddle1_pos[3] = [0, paddle1_p + PAD_HEIGHT]
    
    
    paddle2_p += paddle2_vel 
    if paddle2_p <= 0:
        paddle2_P = 0
    if paddle2_p >= HEIGHT - PAD_WIDTH:
        paddle2_p = HEIGHT - PAD_WIDTH
    paddle2_pos[0] = [WIDTH - PAD_WIDTH, paddle2_p]
    paddle2_pos[1] = [WIDTH, paddle2_p]
    paddle2_pos[2] = [WIDTH, paddle2_p + PAD_HEIGHT]
    paddle2_pos[3] = [WIDTH - PAD_WIDTH, paddle2_p + PAD_HEIGHT]
    
    
   
    # draw paddles
    canvas.draw_polygon(paddle1_pos, 1, 'Blue', 'White')
    canvas.draw_polygon(paddle2_pos, 1, 'Blue', 'White')
    # determine whether paddle and ball collide    
    if ball_pos[0] <= BALL_RADIUS + PAD_WIDTH:
        if ball_pos[1] >= paddle1_pos[0][1] and ball_pos[1] <= paddle1_pos[2][1]:
            spawn_ball(RIGHT)
        else:
            score2 += 1
    if ball_pos[0] >= WIDTH - PAD_WIDTH - BALL_RADIUS:
        if ball_pos[1] >= paddle2_pos[0][1] and ball_pos[1] <= paddle2_pos[2][1]:
            spawn_ball(LEFT)
        else:
            score1 += 1
            
    # draw scores
    canvas.draw_text(str(score1),[WIDTH / 2 - 40, 40], 30, 'White')
    canvas.draw_text(str(score2),[WIDTH / 2 + 20, 40], 30, 'White')
def keydown(key):
    global paddle1_vel, paddle2_vel
    if key == simplegui.KEY_MAP['w']:
        paddle1_vel = -8
    elif key == simplegui.KEY_MAP['s']:
        paddle1_vel = 8
      
    if key == simplegui.KEY_MAP['up']:
        paddle2_vel = -8
    elif key == simplegui.KEY_MAP['down']:
        paddle2_vel = 8
        
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
def button_restart():
    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)
frame.add_button("Restart",button_restart,100)

# start frame
new_game()
frame.start()

 

posted @ 2015-09-12 22:07  唐雎  阅读(328)  评论(0编辑  收藏  举报