Mercurial > skaapsteker
annotate skaapsteker/sprites/base.py @ 191:993f4f55eb93
Minor item changes.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Wed, 06 Apr 2011 21:35:30 +0200 |
parents | 9d08f99b5ddf |
children | 897eec397cbb |
rev | line source |
---|---|
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
1 """Basic sprite classes.""" |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
2 |
35 | 3 from pygame import Rect |
4 | |
22
b815471d4b95
Move sprite base class into physics since they're tightly coupled.
Simon Cross <hodgestar@gmail.com>
parents:
18
diff
changeset
|
5 from skaapsteker.physics import Sprite |
59
1be1ca704346
Add Layers constants. Set Monsters to player's layer by default
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
6 from skaapsteker.constants import Layers |
47 | 7 from skaapsteker import data |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
8 from skaapsteker import dialogue |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
9 |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
10 |
35 | 11 TILE_SIZE = (64, 64) |
12 | |
97
a1d95c6152a0
Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
13 # Collision Layers (values are ids not numbers) |
a1d95c6152a0
Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
14 PC_LAYER = 0 |
a1d95c6152a0
Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
15 MONSTER_LAYER = 1 |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
16 NPC_LAYER = 2 |
97
a1d95c6152a0
Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
17 |
35 | 18 |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
19 class Monster(Sprite): |
97
a1d95c6152a0
Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
20 |
47 | 21 image_file = None |
97
a1d95c6152a0
Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
22 collision_layer = MONSTER_LAYER |
a1d95c6152a0
Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
23 collides_with = set([PC_LAYER]) |
47 | 24 |
117
b361d9e17c26
Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
25 debug_color = (240, 120, 120) |
b361d9e17c26
Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
26 |
152
60138b935bc0
Make enemies block by default, so we can jump off them
Neil Muller <drnlmuller@gmail.com>
parents:
144
diff
changeset
|
27 block = True |
60138b935bc0
Make enemies block by default, so we can jump off them
Neil Muller <drnlmuller@gmail.com>
parents:
144
diff
changeset
|
28 |
25
fe87d828d093
Very basic enemy support.
Jeremy Thurgood <firxen@gmail.com>
parents:
22
diff
changeset
|
29 def __init__(self, pos, **opts): |
fe87d828d093
Very basic enemy support.
Jeremy Thurgood <firxen@gmail.com>
parents:
22
diff
changeset
|
30 Sprite.__init__(self) |
47 | 31 self.image = data.load_image('sprites/' + self.image_file) |
25
fe87d828d093
Very basic enemy support.
Jeremy Thurgood <firxen@gmail.com>
parents:
22
diff
changeset
|
32 self.starting_tile_pos = pos |
174
8774c170a232
Slightly better enemy handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
163
diff
changeset
|
33 self.rect = self.image.get_rect(midbottom=(pos[0]*TILE_SIZE[0]+TILE_SIZE[0]/2, (pos[1]+1)*TILE_SIZE[1])) |
8774c170a232
Slightly better enemy handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
163
diff
changeset
|
34 self.collide_rect = self.rect.move(0, 0) |
186
72e92893ccb8
Use layers for floor check
Neil Muller <drnlmuller@gmail.com>
parents:
174
diff
changeset
|
35 self.floor_rect = Rect(self.collide_rect.topleft, (self.collide_rect.width, 2)) |
59
1be1ca704346
Add Layers constants. Set Monsters to player's layer by default
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
36 self._layer = Layers.PLAYER |
25
fe87d828d093
Very basic enemy support.
Jeremy Thurgood <firxen@gmail.com>
parents:
22
diff
changeset
|
37 self.setup(**opts) |
fe87d828d093
Very basic enemy support.
Jeremy Thurgood <firxen@gmail.com>
parents:
22
diff
changeset
|
38 |
fe87d828d093
Very basic enemy support.
Jeremy Thurgood <firxen@gmail.com>
parents:
22
diff
changeset
|
39 |
fe87d828d093
Very basic enemy support.
Jeremy Thurgood <firxen@gmail.com>
parents:
22
diff
changeset
|
40 def setup(self): |
fe87d828d093
Very basic enemy support.
Jeremy Thurgood <firxen@gmail.com>
parents:
22
diff
changeset
|
41 pass |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
42 |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
43 |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
44 class NPC(Sprite): |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
45 |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
46 image_file = None |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
47 collision_layer = NPC_LAYER |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
48 collides_with = set([PC_LAYER]) |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
49 |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
50 debug_color = (240, 240, 240) |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
51 |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
52 block = True |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
53 |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
54 def __init__(self, pos, **opts): |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
55 Sprite.__init__(self) |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
56 self.image = data.load_image('sprites/' + self.image_file) |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
57 self.starting_tile_pos = pos |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
58 self.rect = self.image.get_rect(midbottom=(pos[0]*TILE_SIZE[0]+TILE_SIZE[0]/2, (pos[1]+1)*TILE_SIZE[1])) |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
59 self.collide_rect = self.rect.move(0, 0) |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
60 self._layer = Layers.PLAYER |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
61 self.setup(**opts) |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
62 |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
63 def setup(self, dsm, world): |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
64 self.dsm = dialogue.DSM(dsm, world) |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
65 |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
66 |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
67 class Projectile(Sprite): |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
68 gravitates = False |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
69 |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
70 |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
71 class Item(Sprite): |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
72 mobile = False |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
73 gravitates = False |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
74 |
127
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
75 image_file = None |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
76 collision_layer = MONSTER_LAYER |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
77 collides_with = set([PC_LAYER]) |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
78 |
191 | 79 portable = True |
127
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
80 |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
81 def __init__(self, pos, **opts): |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
82 Sprite.__init__(self) |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
83 self.image = data.load_image('sprites/' + self.image_file) |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
84 self.starting_tile_pos = pos |
163 | 85 self.rect = self.image.get_rect(midbottom=(pos[0]*TILE_SIZE[0]+TILE_SIZE[0]/2, (pos[1]+1)*TILE_SIZE[1])) |
86 self.collide_rect = self.rect.move(0, 0) | |
127
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
87 self._layer = Layers.PLAYER |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
88 self.setup(**opts) |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
89 |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
90 |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
91 def setup(self): |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
92 pass |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
93 |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
94 |
191 | 95 def get_debug_color(self): |
96 if self.portable: | |
97 return (240, 0, 240) | |
98 return (0, 0, 240) | |
99 | |
100 | |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
101 class Geography(Sprite): |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
102 mobile = False |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
103 gravitates = False |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
104 collides_with = set([PC_LAYER, MONSTER_LAYER, NPC_LAYER]) |
144
6b488e1351a5
Buggy ground implementation. Make the world less bouncy
Neil Muller <drnlmuller@gmail.com>
parents:
127
diff
changeset
|
105 is_ground = True |
6b488e1351a5
Buggy ground implementation. Make the world less bouncy
Neil Muller <drnlmuller@gmail.com>
parents:
127
diff
changeset
|
106 bounce_factor = (0.0, 0.0) |
28
94d5ebaa912f
Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
107 |
35 | 108 def __init__(self, pos, image): |
109 Sprite.__init__(self) | |
110 self.tile_pos = pos | |
111 self.image = image | |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
112 self.collide_rect = self.image.get_bounding_rect(1) |
186
72e92893ccb8
Use layers for floor check
Neil Muller <drnlmuller@gmail.com>
parents:
174
diff
changeset
|
113 self.floor_rect = Rect(self.collide_rect.topleft, (self.collide_rect.width, 2)) |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
114 self.rect = self.image.get_rect() |
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
115 self.rect_offset = self.collide_rect.left - self.rect.left, self.rect.top - self.rect.top |
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
116 self.collide_rect.topleft = pos[0] * TILE_SIZE[0] + self.rect_offset[0], pos[1] * TILE_SIZE[1] + self.rect_offset[1] |
186
72e92893ccb8
Use layers for floor check
Neil Muller <drnlmuller@gmail.com>
parents:
174
diff
changeset
|
117 self.floor_rect.topleft = pos[0] * TILE_SIZE[0] + self.rect_offset[0], pos[1] * TILE_SIZE[1] + self.rect_offset[1] |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
118 self.rect.topleft = pos[0] * TILE_SIZE[0], pos[1] * TILE_SIZE[1] |
35 | 119 |
117
b361d9e17c26
Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
120 def get_debug_color(self): |
b361d9e17c26
Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
121 if self.floor or self.block: |
b361d9e17c26
Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
122 return (240, 240, 0) |
b361d9e17c26
Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
123 return (0, 240, 0) |
b361d9e17c26
Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
124 |
35 | 125 |
127
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
126 def find_sprite(descr, mod_name=None): |
28
94d5ebaa912f
Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
127 """Create a sprite object from a dictionary describing it.""" |
94d5ebaa912f
Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
128 descr = descr.copy() |
127
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
129 cls_name = descr.pop("type") |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
130 if mod_name is None: |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
131 mod_name, cls_name = cls_name.rsplit(".", 1) |
28
94d5ebaa912f
Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
132 mod_name = ".".join(["skaapsteker.sprites", mod_name]) |
94d5ebaa912f
Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
133 mod = __import__(mod_name, fromlist=[cls_name]) |
94d5ebaa912f
Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
134 cls = getattr(mod, cls_name) |
94d5ebaa912f
Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
135 return cls(**descr) |
35 | 136 |