python海龟画图模块制作的水波纹模拟和跟着鼠标指针前进的左右造型的鱼

'''水波纹模拟和跟着鼠标指针前进的左右造型的鱼.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()
    fish.left_right = abs(dx)/dx                # -1,1
    fish.left_right += 1                        # 换成 0, 2
    fish.left_right = int(fish.left_right // 2) # 向左还是向右?0向左的图片表,1向右的图片表
    fish.images = fish_images[fish.left_right]  # 挑选当前fish的造型列表
    angle = math.degrees(math.atan2(dy,dx))
    fish.setheading(angle)
    if fish.distance(x,y)>50:fish.fd(2)
    # ball.goto(x,y)

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

You must log in to read the rest of this article. Please log in or register as a user.