junglejourney
view tools/makesprites.py @ 201:72baa6318192
Added the final two enemies.
Fixed enemy type selection to include the fifth enemy type.
| author | David Boddie <david@boddie.org.uk> |
|---|---|
| date | Sat Oct 08 23:06:44 2011 +0200 |
| parents | 43f7143d39a1 |
| children |
line source
1 #!/usr/bin/env python
3 """
4 Copyright (C) 2011 David Boddie <david@boddie.org.uk>
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 """
20 def read_xpm(path, symbols = None):
22 lines = open(path).readlines()
23 char_lines = filter(lambda line: line.startswith('"'), lines)
24 strings = map(lambda line: line.strip()[1:-2], char_lines)
25 strings[-1] = strings[-1][:-1]
27 width, height, colours = map(int, strings[0].split()[:3])
28 strings = strings[-height:]
30 if not symbols:
31 symbols = [(".", 0), ("#", "1"), ("+", 2), ("@", 3)]
33 data = []
35 for s in strings:
37 for symbol, value in symbols:
38 s = s.replace(symbol, str(value))
40 data.append(s)
42 return data
45 tiles = [read_xpm("images/flowers.xpm", [(".", "0"), ("@", "1"), ("+", "2")]),
46 read_xpm("images/leaf1.xpm"),
47 read_xpm("images/leaf2.xpm"),
48 read_xpm("images/flowers2.xpm", [(".", "0"), ("#", "1"), ("+", "2"), ("@", "3")]),
49 read_xpm("images/leaf6.xpm", [(".", "0"), ("+", "2")]),
50 read_xpm("images/leaf4.xpm", [(".", "0"), ("#", "1"), ("+", "2"), ("@", "3")]),
51 read_xpm("images/flowers3.xpm", [(".", "0"), ("@", "1"), ("+", "2"), ("#", "3")]),
52 read_xpm("images/leaf5.xpm"),
53 read_xpm("images/leaf3.xpm")]
55 chars = [read_xpm("images/left1.xpm"),
56 read_xpm("images/left2.xpm"),
57 read_xpm("images/right1.xpm"),
58 read_xpm("images/right2.xpm"),
59 read_xpm("images/up1.xpm"),
60 read_xpm("images/up2.xpm"),
61 read_xpm("images/down1.xpm"),
62 read_xpm("images/down2.xpm"),
64 read_xpm("images/demise1.xpm", [(".", "0"), ("+", "2"), ("@", "3")]),
65 read_xpm("images/demise2.xpm", [(".", "0"), ("+", "2"), ("@", "3")]),
66 read_xpm("images/demise3.xpm", [(".", "0"), ("@", "2"), ("+", "3")]),
67 read_xpm("images/demise4.xpm", [(".", "0"), ("@", "2"), ("+", "3")]),
69 read_xpm("images/ball1.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
70 read_xpm("images/ball2.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
71 read_xpm("images/boomerang1.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
72 read_xpm("images/boomerang2.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
73 read_xpm("images/mace1.xpm", [("+", "0"), (".", "3")]),
74 read_xpm("images/mace2.xpm", [("+", "0"), (".", "3")]),
75 read_xpm("images/fire1.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
76 read_xpm("images/fire2.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
78 read_xpm("images/birdld1.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
79 read_xpm("images/birdld2.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
80 read_xpm("images/birdld3.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
81 read_xpm("images/birdld4.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
82 read_xpm("images/birdrd1.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
83 read_xpm("images/birdrd2.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
84 read_xpm("images/birdrd3.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
85 read_xpm("images/birdrd4.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
87 read_xpm("images/waspld1.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
88 read_xpm("images/waspld2.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
89 read_xpm("images/waspld3.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
90 read_xpm("images/waspld4.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
91 read_xpm("images/wasprd1.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
92 read_xpm("images/wasprd2.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
93 read_xpm("images/wasprd3.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
94 read_xpm("images/wasprd4.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
96 read_xpm("images/snakeld1.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
97 read_xpm("images/snakeld2.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
98 read_xpm("images/snakeld3.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
99 read_xpm("images/snakeld4.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
100 read_xpm("images/snakerd1.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
101 read_xpm("images/snakerd2.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
102 read_xpm("images/snakerd3.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
103 read_xpm("images/snakerd4.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
105 read_xpm("images/lizardld1.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
106 read_xpm("images/lizardld2.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
107 read_xpm("images/lizardld3.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
108 read_xpm("images/lizardld4.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
109 read_xpm("images/lizardrd1.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
110 read_xpm("images/lizardrd2.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
111 read_xpm("images/lizardrd3.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
112 read_xpm("images/lizardrd4.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
114 read_xpm("images/beastld1.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
115 read_xpm("images/beastld2.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
116 read_xpm("images/beastld3.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
117 read_xpm("images/beastld4.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
118 read_xpm("images/beastrd1.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
119 read_xpm("images/beastrd2.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
120 read_xpm("images/beastrd3.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
121 read_xpm("images/beastrd4.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
123 # Enemy emerge
124 read_xpm("images/emerge1.xpm", [(".", "0"), ("+", "1"), ("#", "2"), ("@", "3")]),
125 read_xpm("images/emerge2.xpm", [(".", "0"), ("+", "1"), ("#", "3"), ("@", "2")]),
126 read_xpm("images/emerge3.xpm", [(".", "0"), ("+", "1"), ("#", "2"), ("@", "3")]),
127 read_xpm("images/emerge4.xpm", [(".", "0"), ("+", "1"), ("#", "2"), ("@", "3")]),
129 # Enemy demise
130 read_xpm("images/explode1.xpm", [(".", "0"), ("+", "1"), ("#", "2"), ("@", "3")]),
131 read_xpm("images/explode2.xpm", [(".", "0"), ("+", "1"), ("#", "2"), ("@", "3")]),
132 read_xpm("images/explode3.xpm", [(".", "0"), ("+", "1"), ("#", "2"), ("@", "3")]),
133 read_xpm("images/explode4.xpm", [(".", "0"), ("+", "1"), ("#", "2"), ("@", "3")]),
135 # Weapons
136 read_xpm("images/weapon1.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
137 read_xpm("images/weapon2.xpm", [(".", "0"), ("@", "1"), ("+", "2")]),
138 read_xpm("images/weapon3.xpm", [(".", "0"), ("#", "1"), ("+", "2"), ("@", "3")]),
139 read_xpm("images/weapon4.xpm", [(".", "0"), ("@", "1"), ("+", "3")]),
141 # Treasure
142 read_xpm("images/key.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
143 read_xpm("images/chest.xpm", [(".", "0"), ("+", "1"), ("@", "3")]),
144 read_xpm("images/statue.xpm", [(".", "0"), ("+", "2"), ("@", "3")]),
145 read_xpm("images/jewel.xpm", [(".", "0"), ("@", "1"), ("+", "2"), ("#", "3")]),
146 read_xpm("images/health.xpm", [(".", "0"), ("#", "1"), ("+", "2"), ("@", "3")]),
148 # exit
149 read_xpm("images/exit1.xpm", [("+", "0"), ("#", "1"), (".", "2"), ("@", "3")]),
150 read_xpm("images/exit2.xpm", [("+", "0"), ("#", "1"), (".", "2"), ("@", "3")]),
152 # final exit
153 read_xpm("images/finalexitl.xpm", [(".", "0"), ("#", "1"), ("+", "2"), ("@", "3")]),
154 read_xpm("images/finalexitr.xpm", [(".", "0"), ("#", "1"), ("+", "2"), ("@", "3")]),
155 ]
157 title = read_xpm("images/title-screen.xpm")
159 completed = read_xpm("images/complete-screen.xpm", [("+", "0"), ("@", "1"), (".", "2"), ("#", "3")])
160 overlay = read_xpm("images/overlay.xpm", [(".", "0"), ("+", "2")])
162 def read_sprite(lines):
164 data = ""
166 # Read 8 rows at a time.
167 for row in range(0, len(lines), 8):
169 # Read 4 columns at a time.
170 for column in range(0, len(lines[0]), 4):
172 # Read the rows.
173 for line in lines[row:row + 8]:
175 shift = 3
176 byte = 0
177 for pixel in line[column:column + 4]:
179 if pixel == "1":
180 byte = byte | (0x01 << shift)
181 elif pixel == "2":
182 byte = byte | (0x10 << shift)
183 elif pixel == "3":
184 byte = byte | (0x11 << shift)
186 shift -= 1
188 data += chr(byte)
190 return data
192 def make_scanline_bytes(lines):
194 data = []
195 for line in lines:
197 line_data = ""
198 # Read 4 columns at a time.
199 for column in range(0, len(lines[0]), 4):
201 shift = 3
202 byte = 0
203 for pixel in line[column:column + 4]:
205 if pixel == "1":
206 byte = byte | (0x01 << shift)
207 elif pixel == "2":
208 byte = byte | (0x10 << shift)
209 elif pixel == "3":
210 byte = byte | (0x11 << shift)
212 shift -= 1
214 line_data += chr(byte)
216 data.append(line_data)
218 return data
220 def compress(data):
222 output = []
223 current = None
224 length = 0
226 for byte in data:
228 if current != byte:
229 if length > 0:
230 output.append(current + chr(length))
231 current = byte
232 length = 1
233 else:
234 length += 1
235 if length == 255:
236 output.append(current + chr(length))
237 length = 0
239 if length > 0:
240 output.append(current + chr(length))
242 return "".join(output)
244 def read_sprites(sprites):
246 data = ""
248 for lines in sprites:
249 data += read_sprite(lines)
251 return data
253 def encode(data):
255 new_data = ""
256 for c in data:
258 i = ord(c)
259 new_data += chr(i & 0x0f) + chr((i & 0xf0) >> 4)
261 return new_data
263 def combine(encoded, overlay):
265 combined = ""
266 offset = 0
267 while offset < len(overlay):
268 combined += chr(ord(encoded[offset]) | ord(overlay[offset]))
269 offset += 1
271 combined += encoded[offset:]
272 return combined
