Share Pastebin
Guest
Public paste!

Darkone

By: a guest | Feb 9th, 2010 | Syntax: None | Size: 4.20 KB | Hits: 70 | Expires: Never
Copy text to clipboard
  1. import libtcodpy as libtcod
  2.  
  3. #actual size of the window
  4. SCREEN_WIDTH = 46
  5. SCREEN_HEIGHT = 20
  6.  
  7. #size of the map
  8. MAP_WIDTH = 46
  9. MAP_HEIGHT = 20
  10.  
  11. color_dark_wall = libtcod.Color(50, 50, 50)
  12. color_dark_ground = libtcod.Color(150, 150, 150)
  13.  
  14. class Tile:
  15.     #a tile of the map and its properties
  16.     def __init__(self, blocked, block_sight = None):
  17.         self.blocked = blocked
  18.         self.clearance = 0
  19.        
  20.         #by default, if a tile is blocked, it also blocks sight
  21.         if block_sight is None: block_sight = blocked
  22.         self.block_sight = block_sight
  23.    
  24.  
  25. smap = ['##############################################',
  26.         '#######################      #################',
  27.         '#####################    #     ###############',
  28.         '######################  ###        ###########',
  29.         '##################      #####             ####',
  30.         '################       ########    ###### ####',
  31.         '###############      #################### ####',
  32.         '################    ######                  ##',
  33.         '########   #######  ######   #     #     #  ##',
  34.         '########   ######      ###                  ##',
  35.         '########                                    ##',
  36.         '####       ######      ###   #     #     #  ##',
  37.         '#### ###   ########## ####                  ##',
  38.         '#### ###   ##########   ###########=##########',
  39.         '#### ##################   #####          #####',
  40.         '#### ###             #### #####          #####',
  41.         '####           #     ####                #####',
  42.         '########       #     #### #####          #####',
  43.         '########       #####      ####################',
  44.         '##############################################',
  45.         ]
  46.  
  47.         #libtcod.console_put_char(0, self.x, self.y, self.char, libtcod.BKGND_NONE)
  48.  
  49. def make_map():
  50.     global map
  51.    
  52.     #fill map with "unblocked" tiles
  53.     map = [[ Tile(False)
  54.         for y in range(MAP_HEIGHT) ]
  55.             for x in range(MAP_WIDTH) ]
  56.     for y in range(MAP_HEIGHT):
  57.         for x in range(MAP_WIDTH):
  58.             if smap[y][x] == "#":
  59.                 map[x][y].blocked = True
  60.                
  61. def render_all():
  62.     global color_light_wall
  63.     global color_light_ground
  64.    
  65.     #go through all tiles, and set their background color
  66.     for y in range(MAP_HEIGHT):
  67.         for x in range(MAP_WIDTH):
  68.             wall = map[x][y].blocked
  69.             if wall:
  70.                 libtcod.console_set_back(0, x, y, color_dark_wall, libtcod.BKGND_SET )
  71.             else:
  72.                 libtcod.console_set_back(0, x, y, color_dark_ground, libtcod.BKGND_SET )            
  73.             if map[x][y].clearance >= 1:
  74.                 libtcod.console_put_char(0, x, y, str(map[x][y].clearance), libtcod.BKGND_NONE)
  75.                
  76. libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
  77. libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/libtcod tutorial', False)
  78.  
  79. def make_collision():
  80.     global map
  81.     incrementor = 0
  82.     for y in range(MAP_HEIGHT):
  83.         for x in range(MAP_WIDTH):
  84.             if not map[x][y].blocked:
  85.                 map[x][y].clearance = 1
  86.                 incrementor += 1
  87.                 increase = True
  88.                 while increase == True:
  89.                     for iy in range(y, (y + incrementor + 1)):
  90.                         if map[x + incrementor][iy].blocked:
  91.                             increase = False
  92.                     for ix in range(x, (x + incrementor + 1)):
  93.                         if map[ix][y + incrementor].blocked:
  94.                             increase = False
  95.                     if increase == True:
  96.                         map[x][y].clearance += 1
  97.                         incrementor += 1
  98.                     else:
  99.                         increase = False
  100.                         incrementor = 0
  101.  
  102.                        
  103.                
  104.                
  105.  
  106. #generate map (at this point it's not drawn to the screen)
  107. make_map()
  108.  
  109. while not libtcod.console_is_window_closed():
  110.     make_collision()
  111.     #render the screen
  112.     render_all()
  113.     libtcod.console_flush()
  114.     key = libtcod.console_wait_for_keypress(True)