source: nagslang/game_object.py@ 62:1d67a8c9861d

Last change on this file since 62:1d67a8c9861d was 62:1d67a8c9861d, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

WTF whitespace?!?!?!?!

File size: 1.9 KB
Line 
1
2
3class Physicser(object):
4 def add_to_space(self, space):
5 raise NotImplementedError()
6
7 def remove_from_space(self, space):
8 raise NotImplementedError()
9
10 def render_position(self):
11 raise NotImplementedError()
12
13
14class NullPhysicser(Physicser):
15 def add_to_space(self, space):
16 pass
17
18 def remove_from_space(self, space):
19 pass
20
21 def render_position(self):
22 return (0, 0)
23
24
25class SingleShapePhysicser(Physicser):
26 def __init__(self, shape):
27 self._shape = shape
28
29 def add_to_space(self, space):
30 space.add(self._shape, self._shape.body)
31
32 def remove_from_space(self, space):
33 space.remove(self._shape, self._shape.body)
34
35 def render_position(self, surface):
36 pos = self._shape.body.position
37 import pymunk.pygame_util
38 return pymunk.pygame_util.to_pygame(pos, surface)
39 return (int(pos.x), int(pos.y))
40
41
42class Renderer(object):
43 def render(self, surface, pos):
44 raise NotImplementedError()
45
46
47class ImageRenderer(Renderer):
48 def __init__(self, image):
49 self._image = image
50
51 def render(self, surface, pos):
52 surface.blit(self._image, pos)
53
54
55class ShapeRenderer(Renderer):
56 def __init__(self, shape):
57 self._shape = shape
58
59 def render(self, surface, pos):
60 import pymunk.pygame_util
61 pymunk.pygame_util.draw(surface, self._shape)
62
63
64class GameObject(object):
65 """A representation of a thing in the game world.
66
67 This has a rendery thing, physicsy things and maybe some other things.
68 """
69
70 def __init__(self, renderer, physicser=None):
71 self.renderer = renderer
72 if physicser is None:
73 physicser = NullPhysicser()
74 self.physicser = physicser
75
76 def add_to_space(self, space):
77 self.physicser.add_to_space(space)
78
79 def render(self, surface):
80 return self.renderer.render(
81 surface, self.physicser.render_position(surface))
Note: See TracBrowser for help on using the repository browser.