turtle和pymunk愤怒的小鸟发射准备程序by李兴球

turtle和pymunk愤怒的小鸟发射准备程序by李兴球

"""为了用python海龟画图模块和pymunk模块制作愤怒的小鸟,
先编写了这个发射程序。操作方法:拖曳绿球,然后松开即可发射。
读者可以根据这个原型作品用海龟作图模块制作很多小游戏哦。
"""

__author__ = "李兴球"
__date__ = "2019/4/4"
__website__ = "www.lixingqiu.com"
from turtle import *

def draw_line(x,y):
    """画线"""
    fixed_dot.clear()
    fixed_dot.pendown()
    fixed_dot.goto(x,y)
    fixed_dot.penup()    
    fixed_dot.goto(fixed_position)
    ball.goto(x,y)    
    
def shoot(x,y):
    ball.ondrag(None)
    dx =  (fixed_dot.xcor() - ball.xcor())/5    
    dy =  (fixed_dot.ycor() - ball.ycor())/5
    fixed_dot.clear()
    def move():
        nonlocal dy
        x = ball.xcor() + dx
        y = ball.ycor() + dy
        dy = dy - 0.5
        ball.goto(x,y)
        if abs(x)>width//2 or abs(y)>height//2:
            ball.ht()
        else:
            screen.ontimer(move,10)
    move() 
     
    
fixed_position = -300,-150          # 固定点
width,height = 1024,600
screen = Screen()
screen.delay(0)
screen.bgcolor("navy")
screen.setup(width,height)
screen.title("turtle和pymunk愤怒的小鸟发射准备程序by李兴球")
screen.addshape("绿球.gif")

fixed_dot = Turtle(visible=False)
fixed_dot.penup()
fixed_dot.goto(fixed_position)
fixed_dot.pensize(5)
fixed_dot.color("cyan")

ball = Turtle("绿球.gif")
ball.penup()
ball.goto(fixed_position)

ball.ondrag(draw_line)
ball.onrelease(shoot)

"""下面是物理引擎要用到的循环 
while True:
    print(10)
    screen.update()
"""
    
screen.mainloop()

李兴球

李兴球的博客是Python创意编程原创博客

评论已关闭。