Repository navigation
Expand file tree
/
Copy pathgensprites.py
More file actions
executable file
·175 lines (157 loc) · 4.69 KB
/
Copy pathgensprites.py
File metadata and controls
executable file
·175 lines (157 loc) · 4.69 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python3
"""
@file generate_sprites.py
@brief Generates all pixel-art sprites for the Space Invaders game.
@details
This script creates:
- Player sprite
- Bullet sprite
- Alien sprites (squid, crab, octopus) with 2-frame animations per color
- Alien bullets
- Mystery ship
Aliens have 6 colors, and each color has 2 frames using slightly different patterns
for simple animation. Sprites are saved as PNG files in the current directory.
"""
from PIL import Image, ImageDraw
import random
from pathlib import Path
ASSET_DIR = Path(__file__).parent / "assets"
"""@brief directory with external files."""
def draw_pixel_sprite(pattern, pixel_size, color, filename):
"""
@brief Draws a pixel-art sprite from a binary pattern and saves it as PNG.
@param pattern List of strings containing '1' (filled) or '0' (empty) pixels.
@param pixel_size Size of each pixel in the output image.
@param color Tuple (R,G,B) color for filled pixels.
@param filename Filename to save the PNG sprite.
"""
rows = len(pattern)
cols = len(pattern[0])
img = Image.new("RGBA", (cols * pixel_size, rows * pixel_size), (0, 0, 0, 0))
draw = ImageDraw.Draw(img)
for y, row in enumerate(pattern):
for x, c in enumerate(row):
if c == "1":
x0, y0 = x * pixel_size, y * pixel_size
x1, y1 = x0 + pixel_size, y0 + pixel_size
draw.rectangle([x0, y0, x1, y1], fill=color)
img.save(ASSET_DIR / filename)
# ---------------------------------------------------------------------
# Player and bullets
# ---------------------------------------------------------------------
player_pattern = [
"0001000",
"0011100",
"0010100",
"0110110",
"1111111",
"1110111",
"1101011",
]
draw_pixel_sprite(player_pattern, 6, (0, 255, 0), "player.png")
bullet_pattern = ["1", "1", "1"]
draw_pixel_sprite(bullet_pattern, 5, (255, 255, 0), "bullet.png")
alien_bullet_pattern = ["1", "0", "1", "0", "1"]
draw_pixel_sprite(alien_bullet_pattern, 5, (255, 0, 255), "alien_bullet.png")
# ---------------------------------------------------------------------
# Aliens (animated frames)
# ---------------------------------------------------------------------
# Animation comes from splicing these two patterns together
alien_patterns = {
"squid": [
"00111100",
"01111110",
"11111111",
"11011011",
"11111111",
"01100110",
"11000011",
],
"crab": [
"01100110",
"11111111",
"11011011",
"11111111",
"00111100",
"01111110",
"11000011",
],
"octopus": [
"00111100",
"01111110",
"11111111",
"10111101",
"11111111",
"01011010",
"10100101",
],
}
alien_patterns2 = {
"squid": [
"00111100",
"01111110",
"11111111",
"11011011",
"11111111",
"01100110",
"01100110",
],
"crab": [
"01100110",
"11111111",
"11011011",
"11111111",
"00111100",
"01111110",
"01100110",
],
"octopus": [
"00111100",
"01111110",
"11111111",
"10111101",
"11111111",
"01011010",
"00110110",
],
}
# 6-color palette for aliens
colors = [
(255, 0, 0), # Red
(0, 255, 0), # Green
(0, 0, 255), # Blue
(255, 255, 0), # Yellow
(255, 0, 255), # Magenta
(0, 255, 255), # Cyan
]
# Generate two frames per alien using both patterns
for alien_name in alien_patterns.keys():
pattern1 = alien_patterns[alien_name]
pattern2 = alien_patterns2[alien_name]
for i, base_color in enumerate(colors):
# Frame 0
draw_pixel_sprite(pattern1, 5, base_color,
f"alien_{alien_name}_{i}_0.png")
# Frame 1
draw_pixel_sprite(pattern2, 5, base_color,
f"alien_{alien_name}_{i}_1.png")
# ---------------------------------------------------------------------
# Mystery ship
# ---------------------------------------------------------------------
mystery_ship_pattern = [
"000111000",
"001111100",
"011111110",
"101101101",
"011101110",
"001000100",
]
mystery_color = tuple(random.randint(100, 255) for _ in range(3))
draw_pixel_sprite(mystery_ship_pattern, 5, mystery_color, "mystery_ship.png")
# ---------------------------------------------------------------------
# Done
# ---------------------------------------------------------------------
print("Sprites generated:")
print("player.png, bullet.png, alien_bullet.png")
print("alien_<type>_<color>_0.png, alien_<type>_<color>_1.png")
print("mystery_ship.png")