# 跟着鼠标指针前进的左右造型的鱼

### 跟着鼠标指针前进的左右造型的鱼

```'''水波纹模拟和跟着鼠标指针前进的左右造型的鱼.py '''
import math
from turtle import Turtle,Screen     # 导入Turtle类和Screen命令
from random import randint

def follow_mouse(event):
"""本函数让小海龟面朝鼠标指针移动
由于海龟画图的坐标和tkinter画布原生坐标不一样，所以要进行坐标转换。
"""
x = event.x - width//2                   # 转换成海龟坐标系中的x坐标
y = height//2 - event.y                  # 转换成海龟坐标系中的y坐标
dy = y - fish.ycor()
dx = x - fish.xcor()

def animate_fish():
"""鱼切换造型"""

def animate_screen():
"""背景切换"""

if __name__ == "__main__":

bg_index = 0
width,height = 800,600

fish_right_images = ["images/fish_right0.gif","images/fish_right1.gif"]
fish_left_images = ["images/fish_left0.gif","images/fish_left1.gif"]

fish_right_images = ["fishs/" + "0" * (4-len(str(i))) + str(i) +".gif" for i in range(1,9)]
fish_left_images = ["fishs/" + "0" * (4-len(str(i))) + str(i) +"-.gif" for i in range(1,9)]

fish_images = [fish_left_images,fish_right_images]

#waveimages = ["waves/" + (4-len(str(i))) * "0" + str(i) + ".png" for i in range(1,21)]
waveimages = [f"waves/{i:04d}.png" for i in range(1,21)]
bg_amounts = len(waveimages)

screen = Screen()
screen.title('跟着鼠标指针移动_by李兴球')     # 写上窗口标题
screen.setup(width,height)           # 设定窗口大小
screen.delay(0)

fish = Turtle(shape = "turtle")
fish.penup()
fish.speed(0)
fish.left_right = 0                       # 用左边的图还是右边的图,0左,1右
fish.images = fish_right_images           # 初始用右边的一系列图
fish.costume_index = 0                    # 自定义一个属性，用于造型切换索引
fish.costume_amounts = len(fish_right_images) # 一个方向的造型数量，两个方向是一样的数量
animate_fish()
animate_screen()
screen.cv.bind("<Motion>",follow_mouse)   # 画布绑定鼠标移动事件
screen.mainloop()

```

(会员专属：能浏览所有文章，下载所有带链接的Python资源。)