Coursera_An Introduction to Interactive Programming in Python_Mini-project # 4 Pong
2014-10-20 22:57
645 查看
又一次迟交导致交不上了。。
http://www.codeskulptor.org/#user38_cniQ3MOltx_6.py
http://www.codeskulptor.org/#user38_cniQ3MOltx_6.py
# 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 time = 0 # initialize the position and velocity of paddle paddle1_pos = [HALF_PAD_WIDTH,HEIGHT/2] paddle2_pos = [WIDTH-HALF_PAD_WIDTH,HEIGHT/2] paddle1_vel = [0,0] paddle2_vel = [0,0] paddle_acc = 2 # initialize the score score1 = 0 score2 = 0 # initialize ball_pos and ball_vel for new bal in middle of table initial_ball_pos = [WIDTH/2,HEIGHT/2] ball_pos = [WIDTH/2,HEIGHT/2] ball_vel = [0,0] # 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_vel[0] = random.randrange(120, 240)/100 ball_vel[1] = - random.randrange(60, 180) /100 if direction == "LEFT": # direct at upper left ball_vel[0] = -1 *ball_vel[0] # define event handlers def new_game(): global paddle1_pos, paddle2_pos, paddle1_vel, paddle2_vel,ball_pos,ball_vel # these are numbers paddle1_pos = [HALF_PAD_WIDTH,HEIGHT/2] paddle2_pos = [WIDTH-HALF_PAD_WIDTH,HEIGHT/2] paddle1_vel = [0,0] paddle2_vel = [0,0] ball_pos = [WIDTH/2,HEIGHT/2] ball_vel = [0,0] global score1, score2 # these are ints score1 =0 score2 =0 # determine the start direction of a ball random_val = random.randint(0, 1) direction = "" if random_val == 1: direction = "LEFT" else: direction = "RIGHT" spawn_ball(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 ball_pos[0] = ball_pos[0] + ball_vel[0] ball_pos[1] = ball_pos[1] + ball_vel[1] # reflection in upper and down gutter 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] # test if the ball touches/colides with the left and right gutters if ball_pos[0] <= BALL_RADIUS: ball_pos[0] = initial_ball_pos[0] ball_pos[1] = initial_ball_pos[1] spawn_ball("RIGHT") score2 += 1 elif ball_pos[0] >= WIDTH: ball_pos[0] = initial_ball_pos[0] ball_pos[1] = initial_ball_pos[1] spawn_ball("LEFT") score1 += 1 # test if the ball touch the paddele if ball_pos[0]-BALL_RADIUS <= paddle1_pos[0]+HALF_PAD_WIDTH and (ball_pos[1]>=paddle1_pos[1]-HALF_PAD_HEIGHT and ball_pos[1]<=paddle1_pos[1]+HALF_PAD_HEIGHT ): ball_vel[0] =- ball_vel[0] ball_vel[0] += ball_vel[0]*0.1 ball_vel[1] += ball_vel[1]*0.1 if ball_pos[0]+BALL_RADIUS >= paddle2_pos[0]-HALF_PAD_WIDTH and (ball_pos[1]>=paddle2_pos[1]-HALF_PAD_HEIGHT and ball_pos[1]<=paddle2_pos[1]+HALF_PAD_HEIGHT ): ball_vel[0] =- ball_vel[0] ball_vel[0] += ball_vel[0]*0.1 ball_vel[1] += ball_vel[1]*0.1 # draw ball canvas.draw_circle(ball_pos, BALL_RADIUS, 12, 'White','White') # update paddle's vertical position, keep paddle on the screen # draw paddles if paddle1_pos[1] > HALF_PAD_HEIGHT and paddle1_pos[1] < HEIGHT-HALF_PAD_HEIGHT: paddle1_pos[1] += paddle1_vel[1] elif paddle1_pos[1] <= HALF_PAD_HEIGHT: paddle1_pos[1] += paddle_acc elif paddle1_pos[1] >= HEIGHT-HALF_PAD_HEIGHT: paddle1_pos[1] -= paddle_acc canvas.draw_polygon([[paddle1_pos[0]-HALF_PAD_WIDTH, paddle1_pos[1]-HALF_PAD_HEIGHT], [paddle1_pos[0]-HALF_PAD_WIDTH, paddle1_pos[1]+HALF_PAD_HEIGHT], [paddle1_pos[0]+HALF_PAD_WIDTH, paddle1_pos[1]+HALF_PAD_HEIGHT], [paddle1_pos[0]+HALF_PAD_WIDTH, paddle1_pos[1]-HALF_PAD_HEIGHT]], 12, 'White', 'White') if paddle2_pos[1] > HALF_PAD_HEIGHT and paddle2_pos[1] < HEIGHT-HALF_PAD_HEIGHT: paddle2_pos[1] += paddle2_vel[1] elif paddle2_pos[1] <= HALF_PAD_HEIGHT: paddle2_pos[1] += paddle_acc elif paddle2_pos[1] >= HEIGHT-HALF_PAD_HEIGHT: paddle2_pos[1] -= paddle_acc canvas.draw_polygon([[paddle2_pos[0]-HALF_PAD_WIDTH, paddle2_pos[1]-HALF_PAD_HEIGHT], [paddle2_pos[0]-HALF_PAD_WIDTH, paddle2_pos[1]+HALF_PAD_HEIGHT], [paddle2_pos[0]+HALF_PAD_WIDTH, paddle2_pos[1]+HALF_PAD_HEIGHT], [paddle2_pos[0]+HALF_PAD_WIDTH, paddle2_pos[1]-HALF_PAD_HEIGHT]], 12, 'White', 'White') # draw scores canvas.draw_text(str(score1), (WIDTH/2-100, 100), 30, 'White') canvas.draw_text(str(score2), (WIDTH/2+100, 100), 30, 'White') def keydown(key): global paddle1_vel, paddle2_vel if key==simplegui.KEY_MAP["w"]: paddle1_vel[1] = -paddle_acc elif key==simplegui.KEY_MAP["s"]: paddle1_vel[1] = paddle_acc elif key==simplegui.KEY_MAP["down"]: paddle2_vel[1] = paddle_acc elif key==simplegui.KEY_MAP["up"]: paddle2_vel[1] = -paddle_acc def keyup(key): global paddle1_vel, paddle2_vel if key==simplegui.KEY_MAP["w"]: paddle1_vel[1] = 0 elif key==simplegui.KEY_MAP["s"]: paddle1_vel[1] = 0 elif key==simplegui.KEY_MAP["down"]: paddle2_vel[1] = 0 elif key==simplegui.KEY_MAP["up"]: paddle2_vel[1] = 0 def restart_handler(): 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) button1 = frame.add_button('Restart', restart_handler) # start frame new_game() frame.start()
相关文章推荐
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 7 Spaceship
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 1 Rock-paper-scissors
- Mini-project # 4 - "Pong"___An Introduction to Interactive Programming in Python"RICE"
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 2 Guess the number
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 6 Blackjack
- Mini-project # 4 - "Pong"___An Introduction to Interactive Programming in Python"RICE"
- Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project #4 —"Pong"
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 8 RiceRock
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 3 Stopwatch
- Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project — “Guess the number” game
- Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project #3 —"Stopwatch: The Game"
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 5 Memory
- Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project— Rock-paper-scissors-lizard-Spock
- Mini-project # 1 - Rock-paper-scissors-___An Introduction to Interactive Programming in Python"RICE"
- Mini-project # 1 - Rock-paper-scissors-___An Introduction to Interactive Programming in Python"RICE"
- Coursera_An Introduction to Interactive Programming in Python_Music Player
- [Coursera][Rice] An introduction to interactive Programming in Python Week 1
- [Coursera][Rice] An introduction to interactive Programming in Python Week 2 3
- An Introduction to Interactive Programming in Python 第四课作业 Pong
- 【coursera 学习笔记】An Introduction to Interactive Programming in Python--week1