У меня есть arcade.Window
, в котором я создал глазное яблоко, которое отслеживает движения мыши, по какой-то причине ученик очень нервный в определенных местах.Положение потребностей ученика определяется с помощью тригонометрических функций (я пробовал и math
, и numpy
).
Приведенный ниже код создаст окно с двумя «глазами» на нем, которое будет отслеживать мышь, игратьвокруг него, и вы увидите странное поведение.
Я использую Mac OS Sierra 10.12.16
Класс Eye:
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()