Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.11 KB | None | 0 0
  1. ##########
  2. #engine.py
  3. ##########
  4.  
  5. import libtcodpy as libtcod
  6.  
  7.  
  8. from entity import Entity
  9. from input_handlers import handle_keys
  10. from map_objects.game_map import GameMap
  11. from render_functions import clear_all, render_all
  12. from map_objects.rectangle import Rect
  13. from map_objects.tile import Tile
  14.  
  15.  
  16. def main():
  17.     screen_width = 80
  18.     screen_height = 50
  19.     map_width = 80
  20.     map_height = 45
  21.  
  22.     room_max_size = 10
  23.     room_min_size = 6
  24.     max_rooms = 20
  25.     colors = {
  26.         'dark_wall':libtcod.Color(0,0,100),
  27.         'dark_ground':libtcod.Color(50,50,150)
  28.     }
  29.  
  30.     player = Entity(int(screen_width / 2), int(screen_height / 2), '@', libtcod.white)
  31.     npc = Entity(int(screen_width / 2 -5), int(screen_height / 2), '@', libtcod.yellow)
  32.                  
  33.     entities = [npc, player]
  34.  
  35.     libtcod.console_set_custom_font(
  36.         'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
  37.  
  38.     libtcod.console_init_root(
  39.         screen_width, screen_height, 'libtcod tutorial revised', False)
  40.  
  41.     con = libtcod.console_new(screen_width, screen_height)
  42.  
  43.     game_map = GameMap(map_width, map_height)
  44.     game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player)
  45.  
  46.     key = libtcod.Key()
  47.     mouse = libtcod.Mouse()
  48.  
  49.     while not libtcod.console_is_window_closed():
  50.         libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse)
  51.  
  52.         render_all(con, entities, game_map,
  53.                    screen_width, screen_height, colors)
  54.  
  55.         libtcod.console_flush()
  56.  
  57.         clear_all(con, entities)
  58.  
  59.         action = handle_keys(key)
  60.  
  61.         if exit():
  62.             return True
  63.  
  64.         if not game_map.is_blocked(player.x + dx, player.y + dy):
  65.             player.move(dx,dy)
  66.    
  67.  
  68. if __name__ == '__main__':
  69.     main()
  70.  
  71. ###########################
  72. #tile.py
  73. ###########################
  74.  
  75. class Tile:
  76.     """
  77.     A tile on a map. It may or may not be blocked, and may or may not block sight.block
  78.     """
  79.  
  80.     def __init__(self, blocked, block_sight=None):
  81.         self.blocked = blocked
  82.  
  83.         # By default, if a tile is blocked, it also blocks sight
  84.         if block_sight is None:
  85.             block_sight = blocked
  86.  
  87.         self.block_sight = block_sight
  88.  
  89. ###############################
  90. #game_map.py
  91. ###############################
  92.  
  93. import libtcodpy as libtcod
  94. from map_objects.tile import Tile
  95. from map_objects.rectangles import Rect
  96. from random import randint
  97.  
  98.  
  99. class GameMap:
  100.     def __init__(self, width, height):
  101.         self.width = width
  102.         self.height = height
  103.         self.tiles = self.initialize_tiles()
  104.  
  105.     def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player):
  106.         rooms = []
  107.         num_rooms = 0
  108.  
  109.         for r in range(max_rooms):
  110.             # randome width and height
  111.             w = randint(room_min_size, room_max_size)
  112.             h = randint(room_min_size, room_max_size)
  113.             #random position without going out of bounds
  114.             x = randint(0, map_width, - w - 1)
  115.             y = randint(0, map_height, - h - 1)
  116.  
  117.     def create_room(self, room):
  118.         # go through the times in the rectangle and make them passable
  119.         for x in range(room.x1+1,room.x2):
  120.             for y in range(room.y1+1, room.y2):
  121.                 self.tiles[x][y].blocked = False
  122.                 self.tiles[x][y].block_sight = False
  123.  
  124.     def create_h_tunnel(self, x1, x2, y):
  125.         for x in range(min(x1, x2), max(x1, x2)+1):
  126.             self.tiles[x][y].blocked = False
  127.             self.tiles[x][y].block_sight = False
  128.  
  129.     def create_v_tunnel(self, y1, y2, x):
  130.         for y in range(min(y1, y2), max(y1, y2)+1):
  131.             self.tiles[x][y].blocked = False
  132.             self.tiles[x][y].block_sight = False       
  133.            
  134.     def initialize_tiles(self):
  135.         tiles = [[Tile(True)for y in range(self.height)] for x in range(self.width)]
  136.  
  137.         return tiles   
  138.    
  139.  
  140.     def create_room(self, room):
  141.         # go through the times in the rectangle and make them passable
  142.         for x in range(room.x1+1,room.x2):
  143.             for y in range(room.y1+1, room.y2):
  144.                 self.tiles[x][y].blocked = False
  145.                 self.tiles[x][y].block_sight = False
  146.  
  147.     def is_blocked(self, x, y):
  148.         if self.tiles[x][y].blocked:
  149.             return True
  150.  
  151.     self.create_room(room)
  152.     self.create_h_tunnel(25, 40, 23)
  153.  
  154.     return False
  155.  
  156. ##########################
  157. #rectangle.py
  158. ##########################
  159. import libtcodpy as libtcod
  160. from map_objects.tile import Tile
  161. from map_objects.rectangles import Rect
  162. from random import randint
  163.  
  164.  
  165. class GameMap:
  166.     def __init__(self, width, height):
  167.         self.width = width
  168.         self.height = height
  169.         self.tiles = self.initialize_tiles()
  170.  
  171.     def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player):
  172.         rooms = []
  173.         num_rooms = 0
  174.  
  175.         for r in range(max_rooms):
  176.             # randome width and height
  177.             w = randint(room_min_size, room_max_size)
  178.             h = randint(room_min_size, room_max_size)
  179.             #random position without going out of bounds
  180.             x = randint(0, map_width, - w - 1)
  181.             y = randint(0, map_height, - h - 1)
  182.  
  183.     def create_room(self, room):
  184.         # go through the times in the rectangle and make them passable
  185.         for x in range(room.x1+1,room.x2):
  186.             for y in range(room.y1+1, room.y2):
  187.                 self.tiles[x][y].blocked = False
  188.                 self.tiles[x][y].block_sight = False
  189.  
  190.     def create_h_tunnel(self, x1, x2, y):
  191.         for x in range(min(x1, x2), max(x1, x2)+1):
  192.             self.tiles[x][y].blocked = False
  193.             self.tiles[x][y].block_sight = False
  194.  
  195.     def create_v_tunnel(self, y1, y2, x):
  196.         for y in range(min(y1, y2), max(y1, y2)+1):
  197.             self.tiles[x][y].blocked = False
  198.             self.tiles[x][y].block_sight = False       
  199.            
  200.     def initialize_tiles(self):
  201.         tiles = [[Tile(True)for y in range(self.height)] for x in range(self.width)]
  202.  
  203.         return tiles   
  204.    
  205.  
  206.     def create_room(self, room):
  207.         # go through the times in the rectangle and make them passable
  208.         for x in range(room.x1+1,room.x2):
  209.             for y in range(room.y1+1, room.y2):
  210.                 self.tiles[x][y].blocked = False
  211.                 self.tiles[x][y].block_sight = False
  212.  
  213.     def is_blocked(self, x, y):
  214.         if self.tiles[x][y].blocked:
  215.             return True
  216.  
  217.     self.create_room(room)
  218.     self.create_h_tunnel(25, 40, 23)
  219.  
  220.     return False
  221.  
  222. #######################
  223. #input_handlers.py
  224. #######################
  225.  
  226. def handle_keys(key):
  227.     key_char = chr(key.c)
  228.  
  229.     # Movement keys
  230.     if key.vk == libtcod.KEY_UP:
  231.         return {'move': (0, -1)}
  232.     elif key.vk == libtcod.KEY_DOWN:
  233.         return {'move': (0, 1)}
  234.     elif key.vk == libtcod.KEY_LEFT:
  235.         return {'move': (-1, 0)}
  236.     elif key.vk == libtcod.KEY_RIGHT:
  237.         return {'move': (1, 0)}
  238.  
  239.     if key.vk == libtcod.KEY_ENTER and key.lalt:
  240.         # Alt+Enter: toggle fullscreen
  241.         libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
  242.  
  243.     elif key.vk == libtcod.KEY_ESCAPE:
  244.         # Exits the game
  245.         return {'exit': True}
  246.  
  247.     # No key was pressed
  248.     return {}
  249.  
  250. #######################
  251. #render_functions.py
  252. #######################
  253.  
  254. import libtcodpy as libtcod
  255.  
  256. def render_all(con, entities, game_map, screen_width, screen_height, colors):
  257.     #Draw all the tiles in the game map
  258.     for y in range(game_map.height):
  259.         for x in range(game_map.width):
  260.             wall = game_map.tiles[x][y].block_sight
  261.  
  262.             if wall:
  263.                 libtcod.console_set_char_background(con, x, y, colors.get('dark_wall'), libtcod.BKGND_SET)
  264.             else:
  265.                 libtcod.console_set_char_background(con, x, y, colors.get('dark_ground'), libtcod.BKGND_SET)   
  266.  
  267.     #Draw Enemies
  268.     for entity in entities:
  269.         draw_entity(con, entity)
  270.        
  271.     libtcod.console_blit(con, 0, 0, screen_width, screen_height, 0, 0, 0)
  272.  
  273. def clear_all(con, entities):
  274.     for entity in entities:
  275.         clear_entity(con, entity)
  276.  
  277. def draw_entity(con, entity):
  278.     libtcod.console_set_default_foreground(con, entity.color)
  279.     libtcod.console_put_char(con, entity.x, entity.y, entity.char, libtcod.BKGND_NONE)
  280.  
  281. def clear_entity(con, entity):
  282.     # Erase the character the represents this object
  283.     libtcod.console_put_char(con, entity.x, entity.y, entity.char, libtcod.BKGND_NONE)
  284.  
  285. ################
  286. #Entity.py
  287. ################
  288.  
  289. class Entity:
  290.     """
  291.     A generic object to represent players, enemies, items, etc.
  292.     """
  293.  
  294.     def __init__(self, x, y, char, color):
  295.         self.x = x
  296.         self.y = y
  297.         self.char = char
  298.         self.color = color
  299.  
  300.     def move(self, dx, dy):
  301.         #move the entity by a given amount
  302.         self.x += dx
  303.         self.y += dy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement