-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgaze_bubble.py
More file actions
140 lines (101 loc) · 4.04 KB
/
Copy pathgaze_bubble.py
File metadata and controls
140 lines (101 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import pygame
import math
import random
import numpy as np
from utils import get_angle_between_vectors
class GazeBubble:
def __init__(
self,
center: list[int | float],
radius: int | float,
color: tuple[int, int, int],
line_thickness: int = 2,
vertices: int = 8,
max_size: int | float = 30,
animation_speed: float = 0.70,
speed: float = 60,
) -> None:
self.center: list[int | float] = center
self.radius: int | float = radius
self.color: tuple[int, int, int] = color
self.line_thickness: int = line_thickness
self.vertices: int = vertices
self.min_size: float = 2 * animation_speed + 3
self.max_random_size: int | float = max_size
self.min_random_size: int | float = self.max_random_size // 2
self.animation_speed: float = animation_speed
self.animation_counter = 0
self.sizes: list[int | float] = [
random.randint(int(self.min_random_size), int(self.max_random_size))
for _ in range(self.vertices)
]
self.points: list[tuple[int, int]] = self.calc_points()
self.speed: float = speed
self.direction: list[int] = [0, 0]
def draw(self, screen: pygame.Surface) -> None:
self.sizes = [
(
random.randint(int(self.min_random_size), int(self.max_random_size))
if self.sizes[x] <= self.min_size
else self.sizes[x] - self.animation_speed
)
for x in range(self.vertices)
]
for i in range(-1, len(self.points) - 1):
p1 = self.points[i]
p2 = self.points[i + 1]
l = math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2)
rect = pygame.Rect(0, 0, l, self.sizes[i])
centerx = p1[0] + 0.5 * (p2[0] - p1[0])
centery = p1[1] + 0.5 * (p2[1] - p1[1])
surface = pygame.Surface(rect.size, pygame.SRCALPHA)
if i % 2 == 0:
pygame.draw.arc(
surface=surface,
color=self.color,
rect=rect,
start_angle=0,
stop_angle=math.pi,
width=self.line_thickness,
)
else:
pygame.draw.arc(
surface=surface,
color=self.color,
rect=rect,
start_angle=math.pi,
stop_angle=2 * math.pi,
width=self.line_thickness,
)
zero_degrees_vector = [0, 1]
# we have to invert the y-coordinate because the y-axis of the Cartesian coordinate system is inverted to the y-axis of pygame
edge_vector = [
p2[0] - p1[0],
-1 * (p2[1] - p1[1]),
]
degrees = get_angle_between_vectors(v1=zero_degrees_vector, v2=edge_vector)
surface = pygame.transform.rotate(surface=surface, angle=450 - degrees)
screen.blit(
surface,
(
centerx - 0.5 * surface.get_width(),
centery - 0.5 * surface.get_height(),
),
)
def calc_points(self) -> list[tuple[int, int]]:
points = []
angle = 360 / self.vertices
for i in range(self.vertices):
x = self.center[0] + self.radius * math.sin(i * math.radians(angle))
y = self.center[1] - self.radius * math.cos(i * math.radians(angle))
points.append((x, y))
return points
def move(self) -> None:
self.center[0] = self.center[0] + self.speed * self.direction[0]
self.center[1] = self.center[1] + self.speed * self.direction[1]
self.points = self.calc_points()
def set_direction(self, direction: list[float]) -> None:
if direction != [0, 0]:
self.direction = list(np.array(direction) / np.linalg.norm(direction))
else:
self.direction = [0, 0]