-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
65 lines (46 loc) · 1.32 KB
/
utils.py
File metadata and controls
65 lines (46 loc) · 1.32 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
import re
import pygame
class Position:
def __init__(self, pos, y=None):
if isinstance(pos, (tuple, list)):
self._x, self._y = pos
elif isinstance(pos, (float, int)):
self._x, self._y = pos, y
elif isinstance(pos, pygame.Vector2):
self._x, self._y = pos
@property
def x(self):
return self._x
@x.setter
def x(self, value):
self._x = value
@property
def y(self):
return self._x
@y.setter
def y(self, value):
self._x = value
@property
def xy(self):
return self._x, self._y
@xy.setter
def xy(self, value):
self._x, self._y = value
@property
def int_xy(self):
return int(self._x), int(self._y)
def __getitem__(self, key):
return self.xy[item]
def __setitem__(self, key, value):
d = list(self.xy)
d[key] = value
def __str__(self):
return f"({self._x}, {self._y})"
def natural_sort(l):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
return sorted(l, key=alphanum_key)
def image_load(path):
return pygame.image.load(str(path))
def object_to_rect(obj):
return pygame.Rect(obj.x, obj.y, obj.width, obj.height)