2022寒假7天课之迷宫游戏_鼠标牵引课程源代码

2022寒假7天课之迷宫游戏_鼠标牵引课程源代码

Python迷宫游戏鼠标牵引

"""
   04_迷宫游戏_鼠标牵引.py
"""
from sprites import Sprite,mouse_pos

def collision_check():
    laoshu.nextshape()             # 下一个造型
                       
    if laoshu.collidecolor(black): # 如果碰到黑色则倒退4
        laoshu.bk(4)
    elif laoshu.collidecolor(green):# 否则如果碰到绿色则返回真
        return True
    
maze = Sprite('2.png')
maze.ondrag(None)  
screen = maze.getscreen()

laoshu = Sprite(['res/rat1.png','res/rat2.png'])
laoshu.scale(0.4)

screen.listen()
black = (0,0,0)
green = (0,255,85)
while True:
    m = mouse_pos()
    laoshu.heading(m)
    if laoshu.distance(m)>=10:
        laoshu.fd(2)    
        t  = collision_check()  # 调用函数,并且返回t
        if t:break              # 如果t是真,则中断while循环    
    
    screen.update()

w = Sprite(visible=False)
w.color('red')
w.write('Game Over',align='center',font=('',100,'normal'))
screen.mainloop()




    
    

李兴球

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