Finished Part 2
This commit is contained in:
49
engine.py
49
engine.py
@@ -1,29 +1,41 @@
|
||||
import tcod
|
||||
import tcod as tc
|
||||
from entity import Entity
|
||||
from render_functions import *
|
||||
from game_map import *
|
||||
from input_handlers import handle_keys
|
||||
|
||||
|
||||
def main():
|
||||
screen_width = 80
|
||||
screen_height = 50
|
||||
map_width = 80
|
||||
map_height = 50
|
||||
|
||||
player_x = int(screen_width/2)
|
||||
player_y = int(screen_height/2)
|
||||
colors = {
|
||||
'dark_wall': tc.Color(0, 0, 100),
|
||||
'dark_ground': tc.Color(50, 50, 150)
|
||||
}
|
||||
|
||||
tcod.console_set_custom_font('arial10x10.png', tcod.FONT_TYPE_GREYSCALE | tcod.FONT_LAYOUT_TCOD)
|
||||
player = Entity(int(screen_width/2), int(screen_height/2), '@', tc.white)
|
||||
npc = Entity(int(screen_width/2), int(screen_height/2), '@', tc.yellow)
|
||||
entities = [npc, player]
|
||||
|
||||
tcod.console_init_root(screen_width, screen_height, 'tutorial revised', False)
|
||||
con = tcod.console_new(screen_width, screen_height)
|
||||
tc.console_set_custom_font('arial10x10.png', tc.FONT_TYPE_GREYSCALE | tc.FONT_LAYOUT_TCOD)
|
||||
|
||||
key = tcod.Key()
|
||||
mouse = tcod.Mouse()
|
||||
tc.console_init_root(screen_width, screen_height, 'tutorial revised', False)
|
||||
con = tc.console_new(screen_width, screen_height)
|
||||
|
||||
while not tcod.console_is_window_closed():
|
||||
tcod.sys_check_for_event(tcod.EVENT_KEY_PRESS, key, mouse)
|
||||
tcod.console_set_default_foreground(con, tcod.white)
|
||||
tcod.console_put_char(con, player_x, player_y, '@', tcod.BKGND_NONE)
|
||||
tcod.console_blit(con, 0,0, screen_width, screen_height, 0,0,0)
|
||||
tcod.console_flush()
|
||||
game_map = GameMap(map_width, map_height)
|
||||
|
||||
tcod.console_put_char(con, player_x, player_y, ' ', tcod.BKGND_NONE)
|
||||
key = tc.Key()
|
||||
mouse = tc.Mouse()
|
||||
|
||||
while not tc.console_is_window_closed():
|
||||
tc.sys_check_for_event(tc.EVENT_KEY_PRESS, key, mouse)
|
||||
render_all(con, entities, game_map, screen_width, screen_height, colors)
|
||||
tc.console_flush()
|
||||
|
||||
clear_all(con, entities)
|
||||
|
||||
action = handle_keys(key)
|
||||
|
||||
@@ -33,14 +45,15 @@ def main():
|
||||
|
||||
if move:
|
||||
dx, dy = move
|
||||
player_x += dx
|
||||
player_y += dy
|
||||
if not game_map.is_blocked(player.x + dx, player.y + dy):
|
||||
player.move(dx, dy)
|
||||
|
||||
if exit:
|
||||
return True
|
||||
|
||||
if fullscreen:
|
||||
tcod.console_set_fullscreen(not tcod.console_is_fullscreen())
|
||||
tc.console_set_fullscreen(not tc.console_is_fullscreen())
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
14
entity.py
Normal file
14
entity.py
Normal file
@@ -0,0 +1,14 @@
|
||||
class Entity:
|
||||
"""
|
||||
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):
|
||||
self.x += dx
|
||||
self.y += dy
|
||||
@@ -1,18 +1,19 @@
|
||||
import tcod
|
||||
import tcod as tc
|
||||
|
||||
|
||||
def handle_keys(key):
|
||||
if key.vk == tcod.KEY_UP:
|
||||
if key.vk == tc.KEY_UP:
|
||||
return {'move': (0,-1)}
|
||||
elif key.vk == tcod.KEY_DOWN:
|
||||
elif key.vk == tc.KEY_DOWN:
|
||||
return {'move': (0,1)}
|
||||
elif key.vk == tcod.KEY_LEFT:
|
||||
elif key.vk == tc.KEY_LEFT:
|
||||
return {'move':(-1,0)}
|
||||
elif key.vk == tcod.KEY_RIGHT:
|
||||
elif key.vk == tc.KEY_RIGHT:
|
||||
return {'move':(1,0)}
|
||||
|
||||
if key.vk == tcod.KEY_ENTER and key.lalt:
|
||||
if key.vk == tc.KEY_ENTER and key.lalt:
|
||||
return {'fullscreen':True}
|
||||
elif key.vk == tcod.KEY_ESCAPE:
|
||||
elif key.vk == tc.KEY_ESCAPE:
|
||||
return {'exit': True}
|
||||
|
||||
return {}
|
||||
13
map_objects.py
Normal file
13
map_objects.py
Normal file
@@ -0,0 +1,13 @@
|
||||
class Tile:
|
||||
"""
|
||||
A tile on a map. 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
|
||||
24
venv/game_map.py
Normal file
24
venv/game_map.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from map_objects import *
|
||||
|
||||
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
|
||||
33
venv/render_functions.py
Normal file
33
venv/render_functions.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import tcod as tc
|
||||
|
||||
def render_all(con, entities, game_map, screen_width, screen_height, colors):
|
||||
# Draw all 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:
|
||||
tc.console_set_char_background(con, x, y, colors.get('dark_wall'), tc.BKGND_SET)
|
||||
else:
|
||||
tc.console_set_char_background(con, x, y, colors.get('dark_ground'), tc.BKGND_SET)
|
||||
|
||||
# Draw all entities in the list
|
||||
for entity in entities:
|
||||
draw_entity(con, entity)
|
||||
|
||||
tc. 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):
|
||||
tc.console_set_default_foreground(con, entity.color)
|
||||
tc.console_put_char(con, entity.x, entity.y, entity.char, tc.BKGND_NONE)
|
||||
|
||||
|
||||
def clear_entity(con, entity):
|
||||
# Erase the charect that represents this object
|
||||
tc.console_put_char(con, entity.x, entity.y, ' ', tc.BKGND_NONE)
|
||||
Reference in New Issue
Block a user