/
test.py
executable file
·387 lines (336 loc) · 12.4 KB
/
test.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/usr/bin/python
# -*-coding:Utf-8 -*
"""
This is a test of wargame
"""
import math
import sfml as sf
from hexagon import Hexagon, HexagonLine
import time
from unit import Cavalry
import operator
MARGIN = 70
GRAY_127 = sf.Color(127, 127, 127)
EAU = sf.Color(150, 196, 217)
RED = sf.Color(232, 174, 173)
GREEN = sf.Color(188, 222, 186)
BORDER = sf.Color(127, 127, 127, 127)
OVERLOAD = sf.Color(127, 127, 127, 100)
SIZE = 50
class Ground(object):
"""Enumerator for ground type"""
plain = 0
forest = 1
mountain = 2
water = 3
def get_plain(self):
"""docstring for get_plain"""
return self.plain
def get_forest(self):
"""docstring for get_forest"""
return self.forest
def get_mountain(self):
"""docstring for get_mountain"""
return self.mountain
def get_water(self):
"""docstring for get_water"""
return self.water
class TextureManager(object):
"""Texture manager."""
class InternTextureManager(object):
"""Intern singleton class for texture manager."""
def __init__(self):
"""docstring for __init__"""
self.ground_textures = {
Ground.plain: None,
Ground.forest: None,
Ground.mountain: None,
Ground.water: None
}
self.unit_textures = {
"Infantry": None,
"Cavalery": None
}
self.background = None
self.load_all_texture()
def load_all_texture(self):
"""load all texture used in game"""
# self.background = sf.Texture.from_file('data/bois.jpg')
self.background = sf.Texture.from_file('final.png')
self.ground_textures[Ground.forest] = sf.Texture.from_file(
'data/arbre.jpg')
self.ground_textures[Ground.water] = sf.Texture.from_file(
'data/eau.jpg')
self.ground_textures[Ground.mountain] = sf.Texture.from_file(
'data/montagne.jpg')
self.ground_textures[Ground.plain] = sf.Texture.from_file(
'data/paper.jpg')
# self.unit_textures["Infantry"] = sf.Texture.from_file()
self.unit_textures["Cavalry"] = sf.Texture.from_file(
'data/cheval.png')
def convert_ground_to_texture(self, ground):
"""Search the texture associate to ground type."""
return self.ground_textures[ground]
def convert_texture_to_ground(self, texture):
"""docstring for convert_texture_to_ground"""
# TODO
pass
def get_unit_texture(self, unit):
"""Search the texture for each type of unit."""
if unit is not None:
return self.unit_textures[unit.get_name()]
else:
return None
def get_background(self):
"""Return the texture of background"""
return self.background
__instance = None
def __init__(self):
"""docstring for __init__"""
if TextureManager.__instance is None:
TextureManager.__instance = TextureManager.InternTextureManager()
def __getattr__(self, name):
"""docstring for __getattr__"""
return getattr(self.__instance, name)
def __setattr__(self, name):
"""docstring for __setattr__"""
return setattr(self.__instance, name)
class Node(object):
"""
Représente une case sur le plateau
"""
def __init__(self, pos=(0, 0), ground=Ground.plain, color=sf.Color.WHITE):
"""Constructor"""
self.color = color
self.position = pos
self.ground = ground
def get_position(self):
"""Return the position of node in graph."""
return self.position
def get_color(self):
"""Return the color of node."""
return self.color
def set_color(self, color):
"""Set a new color for this node."""
self.color = color
def get_ground(self):
"""Return a ground type for this node"""
return self.ground
def set_ground(self, ground):
"""Set a new ground type for this node"""
self.ground = ground
def convert_pixel_to_coord(position):
"""docstring for convert_pixel_to_coord"""
hexa = Hexagon((0, 0), SIZE, RED, BORDER)
x_coord = (position[0] - MARGIN) / (hexa.get_radius() * 1.5)
y_coord = (position[1] - MARGIN + x_coord + hexa.get_apothem()) / \
(hexa.get_radius() * 2)
return (x_coord, y_coord)
def convert_coord_to_pixel(coord):
"""docstring for convert_coord_to_pixel"""
hexa = Hexagon((0, 0), SIZE, RED, BORDER)
y_pos = MARGIN + coord[1] * hexa.get_apothem() * 2 - coord[0] * \
hexa.get_apothem()
x_pos = MARGIN + coord[0] * hexa.get_radius() * 1.5
return (x_pos, y_pos)
def display_units(window, units):
"""
Display all unit on the map
:param window:window used to display
:param units:list of all units
"""
hexa = Hexagon((100, 100), SIZE, GRAY_127, BORDER)
texture_mgr = TextureManager()
for unit in units:
hexa.set_texture(texture_mgr.get_unit_texture(unit))
hexa.position = convert_coord_to_pixel(unit.get_position())
window.draw(hexa)
if unit.get_selected():
hexa.set_texture(None)
hexa.set_color(sf.Color(250, 50, 250, 50))
window.draw(hexa)
neighbors = unit.get_neighbors()
for i in range(len(neighbors)):
pos = unit.get_position()
neighbor = (neighbors[i][0] + pos[0], neighbors[i][1] + pos[1])
hexa.position = convert_coord_to_pixel(neighbor)
hexa.set_color(sf.Color(0, 0, 250, 50))
window.draw(hexa)
def display_grid(window, graph):
"""
Display all elements contains in graph (grid on map)
:param window:window used to display
:param graph:list of all elements to displaying
"""
hexa = HexagonLine((100, 100), SIZE, BORDER)
for node in graph:
pos = node.get_position()
y_pos = MARGIN + pos[1] * hexa.get_apothem() * 2 - \
pos[0] * hexa.get_apothem()
x_pos = MARGIN + pos[0] * hexa.get_radius() * 1.5
hexa.position = (x_pos, y_pos)
hexa.set_color(node.get_color())
window.draw(hexa)
def search_node_by_coord_in_list(list_node, coord):
"""
Search if a node exist in list with the coordinate passed in arguments.
"""
for node in list_node:
if node.get_position() == coord:
return node
return None
def search_unit_by_coord_in_list(units, coord):
"""docstring for search_unit_by_coord_in_list"""
for unit in units:
if unit.get_position() == coord:
return unit
return None
def generate_grid(height, width):
"""docstring for generate_grid"""
liste = []
base = 0
for i in range(width):
for j in range(base, height):
liste.append(Node((i, j)))
if i != 0 and i % 2 == 1:
height += 1
else:
base += 1
return liste
def search_node_in_map(event, hexa):
"""docstring for searchNode"""
neighbors = [
(+1, +1), (+1, 0), (0, -1),
(-1, -1), (-1, 0), (0, +1),
(0, 0)
]
pos = (-1, -1)
old_dist = -1
coord = (-1, -1)
# search element in diagonal (search column)
for i in range(9):
y_i = MARGIN + i * hexa.get_apothem() * 2 - \
i * hexa.get_apothem()
x_i = MARGIN + i * hexa.get_radius() * 1.5
dist = math.fabs(event.position.x - x_i)
if pos == (-1, -1) or dist < old_dist:
coord = (i, i)
pos = (x_i, y_i)
old_dist = dist
# create a list of element on same column
if event.position.y > pos[1]:
liste = range(coord[0] - 1, 15)
else:
liste = range(0, coord[0] + 1)
old_dist = -1
# search the row
for i in liste:
y_pos = (MARGIN + i * hexa.get_apothem() * 2 - coord[0] *
hexa.get_apothem())
dist = math.fabs(event.position.y - y_pos)
if old_dist == -1 or dist < old_dist:
coord = (coord[0], i)
pos = (pos[0], y_pos)
old_dist = dist
old_dist = -1
# improve the position of element selected with their neighbors
for i in neighbors:
act_coord = tuple(map(operator.add, coord, i))
y_i = MARGIN + act_coord[1] * hexa.get_apothem() * 2 - \
act_coord[0] * hexa.get_apothem()
x_i = MARGIN + act_coord[0] * hexa.get_radius() * 1.5
dist = math.sqrt((event.position.x - x_i)**2 +
(event.position.y - y_i)**2)
if old_dist == -1 or dist < old_dist:
pos = (x_i, y_i)
final_coord = act_coord
old_dist = dist
coord = final_coord
return pos, coord
def event_key_dispatcher(event, window, last_node, units):
"""docstring for event_key_dispatcher"""
if event.pressed and event.code is sf.Keyboard.ESCAPE:
window.close()
if event.pressed and event.code is sf.Keyboard.C and last_node is not None:
cavalry = Cavalry()
cavalry.set_position(last_node.get_position())
units.append(cavalry)
if event.pressed and event.code is sf.Keyboard.A and last_node is not None:
last_node.set_ground(Ground.water)
if event.pressed and event.code is sf.Keyboard.Z and last_node is not None:
last_node.set_ground(Ground.forest)
if event.pressed and event.code is sf.Keyboard.E and last_node is not None:
last_node.set_ground(Ground.mountain)
if event.pressed and event.code is sf.Keyboard.R and last_node is not None:
last_node.set_ground(Ground.plain)
def event_dispatcher(event, window, last_node, units, survol, survol_on, grid):
"""docstring for event_dispatcher"""
hexa = Hexagon((100, 100), SIZE, GRAY_127, BORDER)
if type(event) is sf.CloseEvent:
window.close()
if type(event) is sf.ResizeEvent:
window.view = sf.View(sf.Rectangle((0, 0), window.size))
if type(event) is sf.KeyEvent:
event_key_dispatcher(event, window, last_node, units)
if type(event) is sf.window.MouseMoveEvent:
pos, coord = search_node_in_map(event, hexa)
survol.position = pos
node = search_node_by_coord_in_list(grid, coord)
if node is not None:
last_node = node
survol_on = True
else:
last_node = None
survol_on = False
if type(event) is sf.window.FocusEvent:
survol_on = False
if type(event) is sf.window.MouseButtonEvent and event.pressed:
if last_node is not None and \
search_node_by_coord_in_list(
grid, last_node.get_position()) is not None:
unit = search_unit_by_coord_in_list(units,
last_node.get_position())
if unit is not None:
unit.set_selected(not unit.get_selected())
return survol_on, last_node
def main():
"""docstring for main"""
width, height = sf.Vector2(800, 600)
settings = sf.window.ContextSettings()
settings.antialiasing_level = 8
window = sf.RenderWindow(sf.VideoMode(width, height), "PySFML test",
sf.window.Style.DEFAULT, settings)
grid = generate_grid(6, 10)
survol_on = False
last_node = None
texture_mgr = TextureManager()
survol = Hexagon((100, 100), SIZE, OVERLOAD, sf.Color.BLACK)
sprite = sf.Sprite(texture_mgr.get_background())
font = sf.Font.from_file('data/Ubuntu-L.ttf')
text = sf.Text('test', font, 30)
text.color = sf.Color.RED
text.position = (30, 0)
fpstimer = time.time()
fpscounter = 0
units = []
# On démarre la boucle de jeu
while window.is_open:
window.clear(sf.Color(50, 200, 50))
window.draw(sprite)
display_grid(window, grid)
display_units(window, units)
for event in window.events:
survol_on, last_node = event_dispatcher(event, window, last_node,
units, survol, survol_on,
grid)
if time.time() - fpstimer >= 1:
text.string = str(fpscounter) + 'fps'
fpstimer = time.time()
fpscounter = 0
if survol_on is True:
window.draw(survol)
window.draw(text)
window.display()
fpscounter += 1
if __name__ == '__main__':
main()