An Introduction to Interactive Programming in Python 第五周作业
程序员文章站
2022-07-14 10:05:01
...
这个当时做的时候花费了一些时间,。主要功能WS,箭头上下控制两侧板子移动,来弹走小球,每碰撞一次,小球速度会增加一倍。
import simplegui
import random
WIDTH = 600
HEIGHT = 400
BALL_RADIUS = 20
BALL_POS = [WIDTH/2 ,HEIGHT/2 ]
PAD_WIDTH = 8
PAD_HEIGHT = 80
HALF_PAD_WIDTH = PAD_WIDTH / 2
HALF_PAD_HEIGHT = PAD_HEIGHT / 2
PAD1_POS = [HALF_PAD_WIDTH ,HEIGHT/2 ]
PAD2_POS = [WIDTH - HALF_PAD_WIDTH - 1 ,HEIGHT/2 ]
PAD1_vel = 0
PAD2_vel = 0
BALL_VEL = [0,0]
player1 = 0
player2 = 0
LEFT = False
RIGHT = True
def spawn_ball(direction):
global BALL_POS, BALL_VEL # these are vectors stored as lists
BALL_POS = [300, 200]
BALL_VEL[0] = (1 if direction == 'RIGHT' else -1)
BALL_VEL[1] = random.choice([-1,1])
def new_game():
spawn_ball('RIGHT')
def draw(canvas):
global PAD2_vel ,PAD1_vel,player1,player2,BALL_POS, BALL_VEL,PAD1_POS,PAD2_POS
#DRAW PAD
PAD1_POS[1] = PAD1_POS[1] + PAD1_vel
PAD2_POS[1] = PAD2_POS[1] + PAD2_vel
if PAD1_POS[1] < 40:
PAD1_POS[1] = 40
if PAD1_POS[1] > 360:
PAD1_POS[1] = 360
if PAD2_POS[1] < 40:
PAD2_POS[1] = 40
if PAD2_POS[1] > 360:
PAD2_POS[1] = 360
canvas.draw_polygon([(0, PAD1_POS[1]-HALF_PAD_HEIGHT), (8, PAD1_POS[1]-HALF_PAD_HEIGHT), (8,PAD1_POS[1] + HALF_PAD_HEIGHT),(0,PAD1_POS[1] + HALF_PAD_HEIGHT)], 1, 'White', 'White')
canvas.draw_polygon([(592, PAD2_POS[1]-HALF_PAD_HEIGHT), (600, PAD2_POS[1]-HALF_PAD_HEIGHT), (600,PAD2_POS[1]+HALF_PAD_HEIGHT),(592,PAD2_POS[1]+HALF_PAD_HEIGHT)], 1, 'White', 'White')
# draw ball
BALL_POS[0] += BALL_VEL[0]
BALL_POS[1] += BALL_VEL[1]
if BALL_POS[1] <= BALL_RADIUS or BALL_POS[1] >= HEIGHT -BALL_RADIUS:
BALL_VEL[1] = - BALL_VEL[1]
if BALL_POS[0] >= 572 and PAD2_POS[1]-HALF_PAD_HEIGHT <= BALL_POS[1] <= PAD2_POS[1]+HALF_PAD_HEIGHT:
BALL_VEL[0] = - BALL_VEL[0]
BALL_VEL[0] -= 1
elif BALL_POS[0] > 572:
player1 += 1
print "right",BALL_VEL[1]
spawn_ball('LEFT')
if BALL_POS[0] <= 28 and PAD1_POS[1]-HALF_PAD_HEIGHT <= BALL_POS[1] <= PAD1_POS[1] + HALF_PAD_HEIGHT:
BALL_VEL[0] = - BALL_VEL[0]
BALL_VEL[0] +=1
elif BALL_POS[0] < 25:
player2 += 1
print "left",BALL_VEL[1]
spawn_ball('RIGHT')
canvas.draw_circle(BALL_POS, BALL_RADIUS, 1, 'White', 'White')
canvas.draw_text(str(player1), [250,80], 40, 'White')
canvas.draw_text(str(player2), [350,80], 40, 'White')
def keydown(key):
global PAD2_vel,PAD1_vel
PAD1_acc = 5
PAD2_acc = 5
if key == simplegui.KEY_MAP["down"]:
PAD2_vel += PAD2_acc
elif key == simplegui.KEY_MAP["up"]:
PAD2_vel -= PAD2_acc
elif key == simplegui.KEY_MAP["W"]:
PAD1_vel -= PAD1_acc
elif key == simplegui.KEY_MAP["S"]:
PAD1_vel += PAD1_acc
def keyup(key):
global PAD2_vel,PAD1_vel
if key == simplegui.KEY_MAP["down"] or key == simplegui.KEY_MAP["up"]:
PAD2_vel = 0
elif key == simplegui.KEY_MAP["W"] or key == simplegui.KEY_MAP["S"]:
PAD1_vel = 0
def reset():
global player1,player2,PAD1_POS,PAD2_POS
PAD1_POS = [HALF_PAD_WIDTH ,HEIGHT/2 ]
PAD2_POS = [WIDTH - HALF_PAD_WIDTH - 1 ,HEIGHT/2 ]
player1 = 0
player2 = 0
spawn_ball('RIGHT')
# Create a frame and assign callbacks to event handlers
frame = simplegui.create_frame("pong", 600, 400)
frame.set_keydown_handler(keydown)
frame.set_keyup_handler(keyup)
frame.set_draw_handler(draw)
frame.add_button("reset",reset,200)
# Start the frame animation
frame.start()
new_game()