An Introduction to Interactive Programming in Python 第五周作业

这个当时做的时候花费了一些时间,。主要功能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()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值