Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Python by serbandr ( 6 years ago )
ENGINE.PY
import tcod 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
def main():
screen_width = 80
screen_height = 50
map_width = 100
map_height = 100
camera_width = 80
camera_height = 43
camera_x = 0
camera_y = 0
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)
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, player, camera_width, camera_height, camera_x, camera_y, colors)
libtcod.console_flush()
clear_all(con, entities, camera_width, camera_height, camera_x, camera_y)
action = handle_keys(key)
move = action.get('move')
exit = action.get('exit')
fullscreen = action.get('fullscreen')
if move:
dx, dy = move
if not game_map.is_blocked(player.x + dx, player.y + dy):
player.move(dx, dy)
if exit:
return True
if fullscreen:
libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
if __name__ == '__main__':
main()
RENDER_FUNCTIONS.PY
import tcod as libtcod
from camera import to_camera_coordinates, move_camera
def render_all(con, entities, game_map, screen_width, screen_height, player, camera_width, camera_height, camera_x, camera_y, colors):
move_camera(player.x, player.y, camera_width, camera_height, camera_y, camera_x, game_map)
# Draw all the tiles in the game map
for y in range(camera_height):
for x in range(camera_width):
wall = game_map.tiles[camera_x + x][camera_y + 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 all entities in the list
for entity in entities:
draw_entity(con, entity, camera_width, camera_height, camera_x, camera_y)
libtcod.console_blit(con, 0, 0, screen_width, screen_height, 0, 0, 0)
def clear_all(con, entities, camera_width, camera_height, camera_x, camera_y):
for entity in entities:
clear_entity(con, entity, camera_width, camera_height, camera_x, camera_y)
def draw_entity(con, entity, camera_width, camera_height, camera_x, camera_y):
(x, y) = to_camera_coordinates(entity.x, entity.y, camera_width, camera_height, camera_x, camera_y)
if x is not None:
libtcod.console_set_default_foreground(con, entity.color)
libtcod.console_put_char(con, x, y, entity.char, libtcod.BKGND_NONE)
def clear_entity(con, entity, camera_width, camera_height, camera_x, camera_y):
# erase the character that represents this object
(x, y) = to_camera_coordinates(entity.x, entity.y, camera_width, camera_height, camera_x, camera_y)
if x is not None:
libtcod.console_put_char(con, x, y, ' ', libtcod.BKGND_NONE)
CAMERA.PY
def to_camera_coordinates(x, y, camera_width, camera_height, camera_x, camera_y):
#convert coordinates on the map to coordinates on the screen
(x, y) = (x - camera_x, y - camera_y)
if (x < 0 or y < 0 or x >= camera_width or y >= camera_height):
return (None, None) #if it's outside the view, return nothing
return (x, y)
def move_camera(target_x, target_y, camera_width, camera_height, camera_y, camera_x, game_map):
#new camera coordinates (top-left corner of the screen relative to the map)
x = target_x - camera_width / 2 #coordinates so that the target is at the center of the screen
y = target_y - camera_height / 2
#make sure the camera doesn't see outside the map
if x < 0: x = 0
if y < 0: y = 0
if x > game_map.width - camera_width - 1: x = game_map.width - camera_width - 1
if y > game_map.height - camera_height - 1: y = game_map.height - camera_height - 1
(camera_x, camera_y) = (x, y)
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
GAME_MAP.PY
from map_objects.tile import Tile
class GameMap:
def __init__(self, width, height):
self.width = width
self.height = height
self.tiles = self.initialize_tiles()
def initialize_tiles(self):
tiles = [[Tile(False) for y in range(self.height)] for x in range(self.width)]
tiles[30][22].blocked = True
tiles[30][22].block_sight = True
tiles[31][22].blocked = True
tiles[31][22].block_sight = True
tiles[32][22].blocked = True
tiles[32][22].block_sight = True
return tiles
def is_blocked(self, x, y):
if self.tiles[x][y].blocked:
return True
return False
TILE.PY
class Tile:
"""
A tile on a map. It may or may not be blocked, and may or may not block sight.
"""
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
Revise this Paste
Children: 103274