turtle_pymunk愤怒的小鸟简单版本程序

"""   这是用Python的海龟画图模块和pymunk模块制作的愤怒的小鸟简单版本程序。
physicbox是我编写的一个模块,它有PhysicBall类用来生成物理角色。
还设计了StaticPhysicBox类用来生成静止的盒子角色。 
"""
import pymunk                       # 导入pymunk模块
import turtle                       # 导入海龟模块,用它来渲染刚体
import random
import physicbox

def display_xy(event):
        """朝向鼠标指针"""
        x = event.x - screen.window_width()/2      #转换成海龟坐标系中的x坐标
        y = screen.window_height()/2 - event.y     #转换成海龟坐标系中的y坐标
        screen.title(str(x) + "," + str(y) + "turtle_pymunk愤怒的小鸟原型程序")
  
def draw_line(x,y):
    """画线"""
    fixed_dot.clear()
    fixed_dot.pendown()
    fixed_dot.goto(x,y)
    fixed_dot.penup()    
    fixed_dot.goto(fixed_position)
    virtual_ball.goto(x,y)

def shoot(x,y):
    virtual_ball.ondrag(None)                        # 取消虚拟球的拖曳事件
    dx =  (fixed_dot.xcor() - virtual_ball.xcor())/5 # 算出水平位移   
    dy =  (fixed_dot.ycor() - virtual_ball.ycor())/5 # 算出垂直位移                
    
    if int(dx) == 0 : dx = random.choice([-0.1,0.1])
    print("dx=",dx)
    direction = dx * 300,dy * 300                  # 方向向量,此处用来代表力的大小和方向 
    fixed_dot.clear()
    """下面是虚拟球先发射,碰到物理球后隐藏,给物理球一个力,让物理球发射"""
    def move():
        nonlocal dy
        x = virtual_ball.xcor() + dx
        y = virtual_ball.ycor() + dy        
        virtual_ball.goto(x,y)
        if virtual_ball.distance(fixed_dot) >= 50 :  # 如果到物理球的距离大于或等于球的直径,则认为是碰到了
            virtual_ball.ht()                        # 虚拟球隐藏  
            ball.body.force = direction              # 给力
        else:        
            screen.ontimer(move,10)
    move()

    
width,height = 1024,800
screen = turtle.Screen()                 # 新建海龟窗口,用于渲染形状的
screen.delay(0)
screen.title("turtle_pymunk愤怒的小鸟原型程序")
screen.setup(width,height)
screen.bgpic("images/bg.png")
screen.addshape("images/bird.gif")       # 50 x 50
 
screen.addshape("images/platform.gif")   # 168 x 166
screen.addshape("images/wood1.gif")      # 406 x 38
screen.addshape("images/wood2.gif")      # 166 x 82
screen.addshape("images/ground.gif")     # 1024x250
screen.addshape("images/verticle1.gif")  # 96x408
screen.addshape("images/verticle2.gif")  # 96x351
screen.addshape("images/verticle3.gif")  # 96x314
screen.addshape("images/verticle4.gif")  # 96x269
screen.addshape("images/verticle5.gif")  # 96x210
screen.addshape("images/verticle6.gif")  # 96x114
screen.addshape("images/pig.gif")        # 102x92


# 新建重力空间
space = pymunk.Space()                   # 设定重力空间
space.gravity = 0,-50                    # 设置重力参数 

# 新建静止的平台,参数为:重力空间,gif图形,图形宽高,坐标,用于放要发射的球
px,py  = -400,-300
ball_platform = physicbox.StaticPhysicBox(space,"images/platform.gif",(168,166),(px,py))

fixed_position = ball_position = (px,  py+166/2+50/2)   # 平台中心点y坐 + 平台高度/2 + 球高度/2,这样球刚好在平台上   
ball = physicbox.PhysicBall(space,"images/bird.gif",ball_position,25)  # 25是半径

# 拉皮筋要用到的固定点对象,它用来画线
fixed_dot = turtle.Turtle(visible=False)
fixed_dot.penup()
fixed_dot.goto(0,300)
fixed_dot.color("gray")
fixed_dot.write("按空格键重置",align='center',font=("黑体",32,"normal"))
fixed_dot.goto(ball_position)
fixed_dot.pensize(5)
fixed_dot.color("orange")

# 虚拟球用来显示拉的时候的球,并不是真正发射的球
virtual_ball = turtle.Turtle("images/bird.gif",visible=False)
virtual_ball.penup()
virtual_ball.goto(fixed_position)
virtual_ball.st()
virtual_ball.ondrag(draw_line)
virtual_ball.onrelease(shoot)

balls = []
balls.append(ball)
pig1 = ball_platform = physicbox.PhysicBall(space,"images/pig.gif",(200,270),25)
balls.append(pig1)

wood1 = physicbox.StaticPhysicBox(space,"images/wood1.gif",(406,38),(50, -200))
verticle1 = physicbox.StaticPhysicBox(space,"images/verticle1.gif",(96,408),( 200, 20)) 

screen.cv.bind("<Motion>",display_xy)    # 绑定鼠标移动事件

def reset_ball():
    """重置待发射的小球"""
    ball.reborn()
    virtual_ball.goto(fixed_position)
    virtual_ball.ondrag(draw_line)
    virtual_ball.st()

screen.onkey(reset_ball,"space")        # 按空格键重置小球
screen.listen()

while True:
    
    space.step(0.02)                     # 把空间中的弹球按重力原理等进行坐标等的更新
    for b in balls:
       b.turtle.goto(b.body.position)            
       
    screen.update()