Eyes
https://simplegametutorials.github.io/pygamezero/
eyes.py
1import pygame, pgzrun
2import math
3
4
5TITLE = "Eyes"
6
7
8def draw_eye(eye_x, eye_y, size):
9 mouse_x, mouse_y = pygame.mouse.get_pos()
10 distance_x = mouse_x - eye_x
11 distance_y = mouse_y - eye_y
12 distance = math.sqrt(distance_x ** 2 + distance_y ** 2)
13 distance = min(distance, size - 20)
14 angle = math.atan2(distance_y, distance_x)
15 angle_deg = angle * 180 / math.pi
16 pupil_x = eye_x + (math.cos(angle) * distance)
17 pupil_y = eye_y + (math.sin(angle) * distance)
18 screen.draw.filled_circle((eye_x, eye_y), size, color=(255, 255, 255))
19 screen.draw.filled_circle((pupil_x, pupil_y), 15, color=(0, 0, 100))
20 # screen.draw.filled_circle((pupil_x, pupil_y), size * 0.3, color=(0, 0, 100))
21
22def update():
23 pass
24
25
26template = '''distance X: %d
27distance Y: %d
28distance: %s
29angle: %s'''
30
31
32
33def draw():
34 screen.fill((0, 0, 0))
35 draw_eye(200, 200, 50)
36 draw_eye(285, 200, 40)
37 draw_eye(200, 200, 50)
38 draw_eye(285, 200, 40)
39
40
41
42
43 # screen.draw.text(template % (distance_x, distance_y, distance, angle_deg), (0, 0))
44
45
46
47
48
49
50pgzrun.go()