-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgameObject.py
92 lines (74 loc) · 2.33 KB
/
gameObject.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
__author__ = 'Laurence Armstrong'
authorship_string = "%s created on %s by %s (%d)\n%s\n" % \
("gameObject.py", "9/04/15", __author__, 15062061, "-----" * 15) \
if __name__ == '__main__' else ""
print(authorship_string, end="")
class InstanceList:
def __init__(self, inp_handle, clock, font):
self.instances = []
self.input_handler = inp_handle
self.clock = clock
self.font = font
def logic(self):
for instance in self.instances:
instance.step()
def render(self, screen):
for instance in self.instances:
instance.draw(screen)
def instantiate(self, obj):
obj.instance_handler = self
obj.input_handler = self.input_handler
obj.clock = self.clock
self.instances.append(obj)
obj.awake()
return obj
def remove(self, inst):
for instance in self.instances:
if instance == inst:
del self.instances[self.instances.index(instance)]
def find_of_type(self, typ):
insts_of_type = []
for instance in self.instances:
if instance is typ or issubclass(type(instance), typ):
insts_of_type.append(instance)
return insts_of_type
def find_first_of_type(self, typ):
for instance in self.instances:
if instance is typ or issubclass(type(instance), typ):
return instance
return None
def remove_of_type(self, typ):
insts = self.find_of_type(typ)
for inst in insts:
inst.destroy()
def __str__(self):
string = ""
for instance in self.instances:
string += str(instance) + ", "
return string
class Object:
"""
@type instance_handler: InstanceList
@type input_handler: spaceInvaders.inputHander.InputHandler
@type clock: pygame.time.Clock
"""
x_speed = 0
y_speed = 0
input_handler = None
instance_handler = None
clock = None
def __init__(self, x=0, y=0, ):
self.x = x
self.y = y
def awake(self):
pass
def move(self):
self.x += self.x_speed
self.y += self.y_speed
def step(self):
self.move()
def draw(self, screen):
pass
def destroy(self):
self.instance_handler.remove(self)