forked from PyLadiesCZ/roboprojekt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frontend.py
88 lines (71 loc) · 2.33 KB
/
frontend.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
"""
The frontend module
- deal with graphic output
- define a Pyglet window for drawing and create sprites from images
"""
import pyglet
# Constatnts for size of tile image in px
TILE_WIDTH = 64
TILE_HEIGHT = 64
def init_window(state):
"""
Return a pyglet window for graphic outputself.
data: a dict created from decoded Tiled 1.2 JSON file
"""
window = pyglet.window.Window(state.sizes[0] * TILE_WIDTH,
state.sizes[1] * TILE_HEIGHT)
return window
def load_tiles(state):
"""
Return list of sprites of tiles.
state: State object containing game board and robots
data: a dict created from decoded Tiled 1.2 JSON file
"""
tile_sprites = []
for coordinate, tiles in state.board.items():
sprites = sprite(coordinate, tiles)
tile_sprites.extend(sprites)
return tile_sprites
def load_robots(state):
"""
Return list of sprites of robots.
state: State object containing game board and robots
data: a dict created from decoded Tiled 1.2 JSON file
"""
robot_sprites = []
for robot in state.robots:
robot_sprite = sprite(robot.coordinates, [robot])
robot_sprites.extend(robot_sprite)
return robot_sprites
def sprite(coordinate, items):
"""
Return list of sprites of items.
coordinate: coordinate of tiles or robot
items: a list of Tile or Robot objects
data: a dict created from decoded Tiled 1.2 JSON file
"""
items_sprites = []
for item in items:
rotation = item.direction.value
path = item.path
x, y = coordinate
img = pyglet.image.load(path)
img.anchor_x = img.width//2
img.anchor_y = img.height//2
item_x = x*TILE_WIDTH
item_y = y*TILE_HEIGHT
img_sprite = pyglet.sprite.Sprite(img, x=img.anchor_x + item_x,
y=img.anchor_y + item_y)
img_sprite.rotation = rotation
items_sprites.append(img_sprite)
return items_sprites
def draw_board(state):
"""
Draw the images of tiles and robots into map.
state: State object containing game board and robots
"""
tile_sprites = load_tiles(state)
robot_sprites = load_robots(state)
tile_sprites.extend(robot_sprites)
for tile_sprite in tile_sprites:
tile_sprite.draw()