-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdraw.py
executable file
·98 lines (86 loc) · 3.24 KB
/
draw.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
93
94
95
96
97
98
from PIL import Image, ImageDraw
GROUND_TYPES = {
"MUD" : "sprites/mud.png",
"GRASS" : "sprites/grass.png",
"STONE" : "sprites/stone.jpg",
"EARTH" : "sprites/grass.png"
}
HERO_TYPES = {
"DISABLED" : "sprites/hero2.png",
"HIPSTER" : "sprites/hero3.png",
"ISLAMIC_WARRIOR" : "sprites/hero4.png"
}
BATTLE_TYPES = [
# this
{
"DISABLED" : "sprites/hero_back2.png",
"HIPSTER" : "sprites/hero_back3.png",
"ISLAMIC_WARRIOR" : "sprites/hero_back4.png"
},
# that
{
"DISABLED" : "sprites/enemy_back2.png",
"HIPSTER" : "sprites/enemy_back3.png",
"ISLAMIC_WARRIOR" : "sprites/enemy_back4.png"
}
]
MARKUPS = {
3 : "sprites/3markup.png",
4 : "sprites/4markup.png",
9 : "sprites/9markup.png"
}
class draw:
def __init__(self, size, cell_size):
transparent_background = Image.new('RGBA',
(size * cell_size, size * cell_size),
(0, 0, 0, 0))
self.back = transparent_background
self.size = size
self.cell_sz = cell_size
def getsize(self):
return self.size ** 2
def set_cell(self, pos, type):
if type not in GROUND_TYPES.keys() or pos >= self.getsize():
return False
x, y = pos % self.size, pos // self.size
texture = Image.open(GROUND_TYPES[type])
if texture.size[0] != self.cell_sz:
texture = texture.resize((self.cell_sz, self.cell_sz))
self.back.paste(texture, (x * self.cell_sz, y * self.cell_sz))
texture.close()
return True
def draw_transparent(self, pos, tr_type):
if tr_type not in HERO_TYPES.keys() or pos >= self.getsize():
return False
x, y = pos % self.size, pos // self.size
transparent = Image.new("RGBA", self.back.size, (0, 0, 0, 0))
hero = Image.open(HERO_TYPES[tr_type]).convert("RGBA")
if hero.size[0] != self.cell_sz:
hero = hero.resize((self.cell_sz, self.cell_sz))
transparent.paste(hero, (x * self.cell_sz, y * self.cell_sz))
self.back = Image.alpha_composite(self.back, transparent)
hero.close()
transparent.close()
return True
def add_markup(self, mode):
if mode not in MARKUPS.keys():
markup = Image.new('RGBA', self.back.size, (0, 0, 0, 0))
drawer = ImageDraw.Draw(markup)
for c in range(1, mode):
x0 = ((self.back.size[0] - 2 * (mode - 1)) // mode + 2) * c
y0 = ((self.back.size[1] - 2 * (mode - 1)) // mode + 2) * c
drawer.line([(x0, 0), (x0, self.back.size[1])], (0, 0, 0, 255), 2)
drawer.line([(0, y0), (self.back.size[0], y0)], (0, 0, 0, 255), 2)
fname = 'sprites/{}markup.png'.format(mode)
markup.save(fname, "PNG")
MARKUPS[mode] = fname
self.back = Image.alpha_composite(self.back, markup)
markup.close()
else:
markup = Image.open(MARKUPS[mode])
self.back = Image.alpha_composite(self.back, markup)
markup.close()
def save_image(self, name):
self.back.save(name, 'JPEG', optimize=True)
self.back.close()
return name