/
renderer.py
51 lines (43 loc) · 1.91 KB
/
renderer.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
import tcod
from fov import FOV
class Renderer:
def __init__(self, console, game_map):
self.console = console
self.game_map = game_map
self.fov = FOV(game_map)
def render_all(self, entities, screen_width, screen_height, x, y):
"""Recomputes FOV and draws all tiles/entities."""
self.fov.compute(x, y)
self.draw_tiles(self.fov)
self.draw_entities(entities, self.fov)
tcod.console_blit(self.console, 0, 0, screen_width,
screen_height, 0, 0, 0)
tcod.console_flush()
def draw_tiles(self, fov):
"""Draws all tiles in list."""
for y in range(self.game_map.height):
for x in range(self.game_map.width):
if tcod.map_is_in_fov(fov.map, x, y):
self.draw_light_tile(
self.console, x, y, self.game_map.tiles[x][y].img
)
self.game_map.tiles[x][y].explored = True
elif self.game_map.tiles[x][y].explored:
self.draw_dark_tile(
self.console, x, y, self.game_map.tiles[x][y].img
)
def draw_entities(self, entities, fov):
"""Draws all entities in list."""
for entity in entities:
self.draw_entity(self.console, entity, fov)
def draw_entity(self, con, entity, fov):
"""Draws a single entity."""
if tcod.map_is_in_fov(fov.map, entity.x, entity.y):
tcod.console_put_char(con, entity.x, entity.y,
entity.tile, tcod.BKGND_NONE)
def draw_light_tile(self, con, x, y, tile):
"""Draws tile with a light tint."""
tcod.console_put_char_ex(con, x, y, tile, tcod.white, tcod.black)
def draw_dark_tile(self, con, x, y, tile):
"""Draws tile with a dark tint."""
tcod.console_put_char_ex(con, x, y, tile, tcod.grey, tcod.black)