街机上的鼠标跟踪行为

时间:2019-03-10 11:31:42

标签: python python-3.x mouseevent

我有一个arcade.Window,在其中创建了一个跟踪鼠标移动的眼球,出于某种原因,瞳孔在特定位置非常跳动。通过三角函数完成了学生需求的位置(我尝试过mathnumpy)。

下面的代码将在其上生成一个带有2个“眼睛”的窗口,该窗口跟踪鼠标并进行操作,您将看到怪异的行为。

我使用的是Mac OS Sierra 10.12.16

眼睛课:

import arcade
from typing import Tuple
import numpy as np

Color = Tuple[int, int, int]


class Eye:

   def __init__(self, center_x: float, center_y: float, radius: float,
             sclera_color: Color = arcade.color.WHITE_SMOKE,
             pupil_color: Color = arcade.color.BLACK,
             background_color: Color = arcade.color.ASH_GREY,
             pupil_scale: float = 0.2):
    """
    An Eye that can track a general x, y
    """
    self.center_x = center_x
    self.center_y = center_y
    self.radius = radius
    self.sclera_color = sclera_color
    self.pupil_color = pupil_color
    self.pupil_scale = pupil_scale
    self.background_color = background_color

    # this is the angle between the mouse position and the center position
    self.theta = None

    self.pupil_x = center_x
    self.pupil_y = center_y

def draw(self):
    """ Draws the eye at the center with the pupil centered as well """

    arcade.draw_circle_filled(self.center_x, self.center_y, self.radius, self.sclera_color)
    arcade.draw_circle_filled(self.pupil_x, self.pupil_y, self.radius * self.pupil_scale, self.pupil_color)
    arcade.draw_circle_outline(self.center_x, self.center_y, self.radius + 13, color=self.background_color,
                               border_width=self.radius * self.pupil_scale + 10)

def track(self, x, y):
    """ tracks an object at (x,y) """
    self.theta = np.arctan2((y - self.pupil_y), (x - self.pupil_x))
    if (y - self.center_y) ** 2 + (x - self.center_x) ** 2 <= (self.radius*(1-self.pupil_scale)) ** 2:
        self.pupil_x = x
        self.pupil_y = y
    else:
        self.pupil_x = np.cos(self.theta)*(self.radius*(1-self.pupil_scale)) + self.center_x
        self.pupil_y = np.sin(self.theta)*(self.radius*(1-self.pupil_scale)) + self.center_y

还有窗口主窗口:

import arcade
from examples.eye import Eye

SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
SCREEN_TITLE = "tracking eye"


class MyGame(arcade.Window):

def __init__(self, width, height, title):

    # Call the parent class's init function
    super().__init__(width, height, title)

    # Make the mouse disappear when it is over the window.
    # So we just see our object, not the pointer.
    self.set_mouse_visible(False)

    self.bg_color = arcade.color.ASH_GREY

    arcade.set_background_color(self.bg_color)

    # Create our eye
    self.eye1 = Eye(100, 300, 60)
    self.eye2 = Eye(300, 300, 60)

def on_draw(self):
    """ Called whenever we need to draw the window. """
    arcade.start_render()
    self.eye1.draw()
    self.eye2.draw()

def on_mouse_motion(self, x, y, dx, dy):
    self.eye1.track(x, y)
    self.eye2.track(x, y)


def main():
    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
    arcade.run()


if __name__ == "__main__":
    main()

0 个答案:

没有答案
相关问题