-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathelements.py
77 lines (60 loc) · 1.95 KB
/
elements.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import pygame as p
import inspect
import global_values as g
import graphics as gfx
import actions
class Element:
"""
Base class for all game objects
"""
def __init__(self, rect, z_index=0):
self.rect = rect
self.set_from_rect()
self.z_index = z_index
self.class_names = []
self.visible_override = None
self.deleted = False
#add to lists of game elements
for base_class in inspect.getmro(self.__class__):
name = "class_"+base_class.__name__
self.class_names.append(name)
if g.elements.get(name, False):
g.elements[name].append(self)
else:
g.elements[name] = [self]
pipe_name = self.__class__.__name__+str(g.element_id_number)
self.pipe = actions.Pipe(pipe_name)
g.element_id_number += 1
g.element_list.append(self)
def set_from_rect(self):
"""
Update this rectangle's position from their rectangular position
"""
self.x = self.rect.x
self.y = self.rect.y
def update_rect(self):
"""
Update's this element's rect based on it's position
"""
self.rect.x = self.x
self.rect.y = self.y
def update(self):
"""
Update this element
"""
self.update_rect()
def draw(self):
g.camera.draw_rect("green", self.rect, 1)
def delete(self):
if not self.deleted:
self.deleted = True
self.pipe.delete()
for class_name in self.class_names:
g.elements[class_name].remove(self)
g.element_list.remove(self)
def __getstate__(self):
print("ELEMENT")
state = gfx.pickle_state(self.__dict__)
return state
def __setstate__(self, state):
self.__dict__ = gfx.unpickle_state(state)