- import libtcodpy as libtcod
- #actual size of the window
- SCREEN_WIDTH = 46
- SCREEN_HEIGHT = 20
- #size of the map
- MAP_WIDTH = 46
- MAP_HEIGHT = 20
- color_dark_wall = libtcod.Color(50, 50, 50)
- color_dark_ground = libtcod.Color(150, 150, 150)
- class Tile:
- #a tile of the map and its properties
- def __init__(self, blocked, block_sight = None):
- self.blocked = blocked
- self.clearance = 0
- #by default, if a tile is blocked, it also blocks sight
- if block_sight is None: block_sight = blocked
- self.block_sight = block_sight
- smap = ['##############################################',
- '####################### #################',
- '##################### # ###############',
- '###################### ### ###########',
- '################## ##### ####',
- '################ ######## ###### ####',
- '############### #################### ####',
- '################ ###### ##',
- '######## ####### ###### # # # ##',
- '######## ###### ### ##',
- '######## ##',
- '#### ###### ### # # # ##',
- '#### ### ########## #### ##',
- '#### ### ########## ###########=##########',
- '#### ################## ##### #####',
- '#### ### #### ##### #####',
- '#### # #### #####',
- '######## # #### ##### #####',
- '######## ##### ####################',
- '##############################################',
- ]
- #libtcod.console_put_char(0, self.x, self.y, self.char, libtcod.BKGND_NONE)
- def make_map():
- global map
- #fill map with "unblocked" tiles
- map = [[ Tile(False)
- for y in range(MAP_HEIGHT) ]
- for x in range(MAP_WIDTH) ]
- for y in range(MAP_HEIGHT):
- for x in range(MAP_WIDTH):
- if smap[y][x] == "#":
- map[x][y].blocked = True
- def render_all():
- global color_light_wall
- global color_light_ground
- #go through all tiles, and set their background color
- for y in range(MAP_HEIGHT):
- for x in range(MAP_WIDTH):
- wall = map[x][y].blocked
- if wall:
- libtcod.console_set_back(0, x, y, color_dark_wall, libtcod.BKGND_SET )
- else:
- libtcod.console_set_back(0, x, y, color_dark_ground, libtcod.BKGND_SET )
- if map[x][y].clearance >= 1:
- libtcod.console_put_char(0, x, y, str(map[x][y].clearance), libtcod.BKGND_NONE)
- libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
- libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/libtcod tutorial', False)
- def make_collision():
- global map
- incrementor = 0
- for y in range(MAP_HEIGHT):
- for x in range(MAP_WIDTH):
- if not map[x][y].blocked:
- map[x][y].clearance = 1
- incrementor += 1
- increase = True
- while increase == True:
- for iy in range(y, (y + incrementor + 1)):
- if map[x + incrementor][iy].blocked:
- increase = False
- for ix in range(x, (x + incrementor + 1)):
- if map[ix][y + incrementor].blocked:
- increase = False
- if increase == True:
- map[x][y].clearance += 1
- incrementor += 1
- else:
- increase = False
- incrementor = 0
- #generate map (at this point it's not drawn to the screen)
- make_map()
- while not libtcod.console_is_window_closed():
- make_collision()
- #render the screen
- render_all()
- libtcod.console_flush()
- key = libtcod.console_wait_for_keypress(True)