-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
61 lines (49 loc) · 1.53 KB
/
utilities.py
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
'''Define miscellaneous utilities'''
import copy
import numpy as np
class Vector:
def __init__(self, x=None, y=None):
self.x = x
self.y = y
def __add__(self, other):
x = self.x + other.x
y = self.y + other.y
return Vector(x, y)
def __sub__(self, other):
x = self.x - other.x
y = self.y - other.y
return Vector(x, y)
def __mul__(self, other):
x = self.x * other
y = self.y * other
return Vector(x, y)
def __str__(self):
return '({0}, {1})'.format(self.x, self.y)
def __iter__(self):
items = [self.x, self.y]
for i in items:
yield i
def magnitude(self):
return np.sqrt(self.x**2 + self.y**2)
def set_angle(self, angle):
angle = (angle * 2.0*np.pi) / 360.0
magnitude = self.magnitude()
self.x = magnitude * np.cos(angle)
self.y = magnitude * np.sin(angle)
def get_angle(self):
angle = (np.arccos(self.x / self.magnitude())*360.0) / (2.0*np.pi)
if self.y < 0:
angle = 360.0 - angle
return angle
def collision(entity1, entity2):
try:
rect1 = entity1.image.get_rect()
except AttributeError:
rect1 = copy.deepcopy(entity1.rect)
try:
rect2 = entity2.image.get_rect()
except AttributeError:
rect2 = copy.deepcopy(entity2.rect)
rect1.move_ip(entity1.position.x, entity1.position.y)
rect2.move_ip(entity2.position.x, entity2.position.y)
return rect1.colliderect(rect2)