Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##########
- #engine.py
- ##########
- import libtcodpy as libtcod
- from entity import Entity
- from input_handlers import handle_keys
- from map_objects.game_map import GameMap
- from render_functions import clear_all, render_all
- from map_objects.rectangle import Rect
- from map_objects.tile import Tile
- def main():
- screen_width = 80
- screen_height = 50
- map_width = 80
- map_height = 45
- room_max_size = 10
- room_min_size = 6
- max_rooms = 20
- colors = {
- 'dark_wall':libtcod.Color(0,0,100),
- 'dark_ground':libtcod.Color(50,50,150)
- }
- player = Entity(int(screen_width / 2), int(screen_height / 2), '@', libtcod.white)
- npc = Entity(int(screen_width / 2 -5), int(screen_height / 2), '@', libtcod.yellow)
- entities = [npc, player]
- libtcod.console_set_custom_font(
- 'arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
- libtcod.console_init_root(
- screen_width, screen_height, 'libtcod tutorial revised', False)
- con = libtcod.console_new(screen_width, screen_height)
- game_map = GameMap(map_width, map_height)
- game_map.make_map(max_rooms, room_min_size, room_max_size, map_width, map_height, player)
- key = libtcod.Key()
- mouse = libtcod.Mouse()
- while not libtcod.console_is_window_closed():
- libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS, key, mouse)
- render_all(con, entities, game_map,
- screen_width, screen_height, colors)
- libtcod.console_flush()
- clear_all(con, entities)
- action = handle_keys(key)
- if exit():
- return True
- if not game_map.is_blocked(player.x + dx, player.y + dy):
- player.move(dx,dy)
- if __name__ == '__main__':
- main()
- ###########################
- #tile.py
- ###########################
- class Tile:
- """
- A tile on a map. It may or may not be blocked, and may or may not block sight.block
- """
- def __init__(self, blocked, block_sight=None):
- self.blocked = blocked
- # By default, if a tile is blocked, it also blocks sight
- if block_sight is None:
- block_sight = blocked
- self.block_sight = block_sight
- ###############################
- #game_map.py
- ###############################
- import libtcodpy as libtcod
- from map_objects.tile import Tile
- from map_objects.rectangles import Rect
- from random import randint
- class GameMap:
- def __init__(self, width, height):
- self.width = width
- self.height = height
- self.tiles = self.initialize_tiles()
- def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player):
- rooms = []
- num_rooms = 0
- for r in range(max_rooms):
- # randome width and height
- w = randint(room_min_size, room_max_size)
- h = randint(room_min_size, room_max_size)
- #random position without going out of bounds
- x = randint(0, map_width, - w - 1)
- y = randint(0, map_height, - h - 1)
- def create_room(self, room):
- # go through the times in the rectangle and make them passable
- for x in range(room.x1+1,room.x2):
- for y in range(room.y1+1, room.y2):
- self.tiles[x][y].blocked = False
- self.tiles[x][y].block_sight = False
- def create_h_tunnel(self, x1, x2, y):
- for x in range(min(x1, x2), max(x1, x2)+1):
- self.tiles[x][y].blocked = False
- self.tiles[x][y].block_sight = False
- def create_v_tunnel(self, y1, y2, x):
- for y in range(min(y1, y2), max(y1, y2)+1):
- self.tiles[x][y].blocked = False
- self.tiles[x][y].block_sight = False
- def initialize_tiles(self):
- tiles = [[Tile(True)for y in range(self.height)] for x in range(self.width)]
- return tiles
- def create_room(self, room):
- # go through the times in the rectangle and make them passable
- for x in range(room.x1+1,room.x2):
- for y in range(room.y1+1, room.y2):
- self.tiles[x][y].blocked = False
- self.tiles[x][y].block_sight = False
- def is_blocked(self, x, y):
- if self.tiles[x][y].blocked:
- return True
- self.create_room(room)
- self.create_h_tunnel(25, 40, 23)
- return False
- ##########################
- #rectangle.py
- ##########################
- import libtcodpy as libtcod
- from map_objects.tile import Tile
- from map_objects.rectangles import Rect
- from random import randint
- class GameMap:
- def __init__(self, width, height):
- self.width = width
- self.height = height
- self.tiles = self.initialize_tiles()
- def make_map(self, max_rooms, room_min_size, room_max_size, map_width, map_height, player):
- rooms = []
- num_rooms = 0
- for r in range(max_rooms):
- # randome width and height
- w = randint(room_min_size, room_max_size)
- h = randint(room_min_size, room_max_size)
- #random position without going out of bounds
- x = randint(0, map_width, - w - 1)
- y = randint(0, map_height, - h - 1)
- def create_room(self, room):
- # go through the times in the rectangle and make them passable
- for x in range(room.x1+1,room.x2):
- for y in range(room.y1+1, room.y2):
- self.tiles[x][y].blocked = False
- self.tiles[x][y].block_sight = False
- def create_h_tunnel(self, x1, x2, y):
- for x in range(min(x1, x2), max(x1, x2)+1):
- self.tiles[x][y].blocked = False
- self.tiles[x][y].block_sight = False
- def create_v_tunnel(self, y1, y2, x):
- for y in range(min(y1, y2), max(y1, y2)+1):
- self.tiles[x][y].blocked = False
- self.tiles[x][y].block_sight = False
- def initialize_tiles(self):
- tiles = [[Tile(True)for y in range(self.height)] for x in range(self.width)]
- return tiles
- def create_room(self, room):
- # go through the times in the rectangle and make them passable
- for x in range(room.x1+1,room.x2):
- for y in range(room.y1+1, room.y2):
- self.tiles[x][y].blocked = False
- self.tiles[x][y].block_sight = False
- def is_blocked(self, x, y):
- if self.tiles[x][y].blocked:
- return True
- self.create_room(room)
- self.create_h_tunnel(25, 40, 23)
- return False
- #######################
- #input_handlers.py
- #######################
- def handle_keys(key):
- key_char = chr(key.c)
- # Movement keys
- if key.vk == libtcod.KEY_UP:
- return {'move': (0, -1)}
- elif key.vk == libtcod.KEY_DOWN:
- return {'move': (0, 1)}
- elif key.vk == libtcod.KEY_LEFT:
- return {'move': (-1, 0)}
- elif key.vk == libtcod.KEY_RIGHT:
- return {'move': (1, 0)}
- if key.vk == libtcod.KEY_ENTER and key.lalt:
- # Alt+Enter: toggle fullscreen
- libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
- elif key.vk == libtcod.KEY_ESCAPE:
- # Exits the game
- return {'exit': True}
- # No key was pressed
- return {}
- #######################
- #render_functions.py
- #######################
- import libtcodpy as libtcod
- def render_all(con, entities, game_map, screen_width, screen_height, colors):
- #Draw all the tiles in the game map
- for y in range(game_map.height):
- for x in range(game_map.width):
- wall = game_map.tiles[x][y].block_sight
- if wall:
- libtcod.console_set_char_background(con, x, y, colors.get('dark_wall'), libtcod.BKGND_SET)
- else:
- libtcod.console_set_char_background(con, x, y, colors.get('dark_ground'), libtcod.BKGND_SET)
- #Draw Enemies
- for entity in entities:
- draw_entity(con, entity)
- libtcod.console_blit(con, 0, 0, screen_width, screen_height, 0, 0, 0)
- def clear_all(con, entities):
- for entity in entities:
- clear_entity(con, entity)
- def draw_entity(con, entity):
- libtcod.console_set_default_foreground(con, entity.color)
- libtcod.console_put_char(con, entity.x, entity.y, entity.char, libtcod.BKGND_NONE)
- def clear_entity(con, entity):
- # Erase the character the represents this object
- libtcod.console_put_char(con, entity.x, entity.y, entity.char, libtcod.BKGND_NONE)
- ################
- #Entity.py
- ################
- class Entity:
- """
- A generic object to represent players, enemies, items, etc.
- """
- def __init__(self, x, y, char, color):
- self.x = x
- self.y = y
- self.char = char
- self.color = color
- def move(self, dx, dy):
- #move the entity by a given amount
- self.x += dx
- self.y += dy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement