Add a map with tiles that can block player movement
This commit is contained in:
parent
4419eb360d
commit
367b284d31
4 changed files with 62 additions and 9 deletions
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
from . import object
|
from . import object
|
||||||
from . import main
|
from . import main
|
||||||
|
from . import tile
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import sys
|
import sys
|
||||||
|
|
|
@ -11,12 +11,15 @@ import os.path
|
||||||
import random
|
import random
|
||||||
import tcod
|
import tcod
|
||||||
from .object import Object
|
from .object import Object
|
||||||
|
from .tile import Tile
|
||||||
|
|
||||||
CONSOLE_WIDTH, CONSOLE_HEIGHT = 50, 50
|
CONSOLE_WIDTH, CONSOLE_HEIGHT = 80, 50
|
||||||
FONT = 'terminal16x16_gs_ro.png'
|
FONT = 'terminal16x16_gs_ro.png'
|
||||||
|
|
||||||
LOG = logging.getLogger('roguebasin')
|
LOG = logging.getLogger('roguebasin')
|
||||||
|
|
||||||
|
MAP_WIDTH, MAP_HEIGHT = 80, 45
|
||||||
|
|
||||||
PLAYER = Object('@', x=CONSOLE_WIDTH // 2, y=CONSOLE_HEIGHT // 2)
|
PLAYER = Object('@', x=CONSOLE_WIDTH // 2, y=CONSOLE_HEIGHT // 2)
|
||||||
NPC = Object('@', color=tcod.yellow, x=random.randint(0, CONSOLE_WIDTH), y=random.randint(0, CONSOLE_HEIGHT))
|
NPC = Object('@', color=tcod.yellow, x=random.randint(0, CONSOLE_WIDTH), y=random.randint(0, CONSOLE_HEIGHT))
|
||||||
|
|
||||||
|
@ -67,12 +70,26 @@ def main(argv):
|
||||||
tileset = tcod.tileset.load_tilesheet(font, 16, 16, tcod.tileset.CHARMAP_CP437)
|
tileset = tcod.tileset.load_tilesheet(font, 16, 16, tcod.tileset.CHARMAP_CP437)
|
||||||
console = tcod.Console(CONSOLE_WIDTH, CONSOLE_HEIGHT, order='F')
|
console = tcod.Console(CONSOLE_WIDTH, CONSOLE_HEIGHT, order='F')
|
||||||
|
|
||||||
|
level = [[Tile(False) for y in range(MAP_HEIGHT)] for x in range(MAP_WIDTH)]
|
||||||
|
level[30][22].blocks_movement = True
|
||||||
|
level[30][22].blocks_sight = True
|
||||||
|
level[45][22].blocks_movement = True
|
||||||
|
level[45][22].blocks_sight = True
|
||||||
|
|
||||||
objects = [PLAYER, NPC]
|
objects = [PLAYER, NPC]
|
||||||
|
|
||||||
with tcod.context.new(columns=console.width, rows=console.height, tileset=tileset) as context:
|
with tcod.context.new(columns=console.width, rows=console.height, tileset=tileset) as context:
|
||||||
while True:
|
while True:
|
||||||
console.clear()
|
console.clear()
|
||||||
|
|
||||||
|
for x in range(MAP_WIDTH):
|
||||||
|
for y in range(MAP_HEIGHT):
|
||||||
|
blocked = level[x][y].blocks_movement
|
||||||
|
if blocked:
|
||||||
|
console.print(x, y, '#', fg=Tile.Color.WALL)
|
||||||
|
else:
|
||||||
|
console.print(x, y, '.', fg=Tile.Color.GROUND)
|
||||||
|
|
||||||
for obj in objects:
|
for obj in objects:
|
||||||
obj.print(console)
|
obj.print(console)
|
||||||
|
|
||||||
|
@ -87,16 +104,24 @@ def main(argv):
|
||||||
raise SystemExit()
|
raise SystemExit()
|
||||||
elif isinstance(event, tcod.event.KeyDown):
|
elif isinstance(event, tcod.event.KeyDown):
|
||||||
sym = event.sym
|
sym = event.sym
|
||||||
|
new_position_x, new_position_y = PLAYER.x, PLAYER.y
|
||||||
if sym == tcod.event.KeySym.h:
|
if sym == tcod.event.KeySym.h:
|
||||||
PLAYER.x = max([0, PLAYER.x - 1])
|
new_position_x = max([0, PLAYER.x - 1])
|
||||||
elif sym == tcod.event.KeySym.j:
|
elif sym == tcod.event.KeySym.j:
|
||||||
PLAYER.y = min([CONSOLE_HEIGHT - 1, PLAYER.y + 1])
|
new_position_y = min([CONSOLE_HEIGHT - 1, PLAYER.y + 1])
|
||||||
elif sym == tcod.event.KeySym.k:
|
elif sym == tcod.event.KeySym.k:
|
||||||
PLAYER.y = max([0, PLAYER.y - 1])
|
new_position_y = max([0, PLAYER.y - 1])
|
||||||
elif sym == tcod.event.KeySym.l:
|
elif sym == tcod.event.KeySym.l:
|
||||||
PLAYER.x = min([CONSOLE_WIDTH - 1, PLAYER.x + 1])
|
new_position_x = min([CONSOLE_WIDTH - 1, PLAYER.x + 1])
|
||||||
else:
|
else:
|
||||||
handled = False
|
handled = False
|
||||||
|
|
||||||
|
if handled:
|
||||||
|
can_move_to_level_position = not level[new_position_x][new_position_y].blocks_movement
|
||||||
|
overlaps_an_object = any(new_position_x == obj.x and new_position_y == obj.y for obj in objects)
|
||||||
|
if can_move_to_level_position and not overlaps_an_object:
|
||||||
|
LOG.debug(f'Moving player to ({new_position_x}, {new_position_y}); can_move:{can_move_to_level_position} overlaps:{overlaps_an_object}')
|
||||||
|
PLAYER.move_to(new_position_x, new_position_y)
|
||||||
else:
|
else:
|
||||||
handled = False
|
handled = False
|
||||||
|
|
||||||
|
|
|
@ -4,11 +4,9 @@
|
||||||
import tcod
|
import tcod
|
||||||
|
|
||||||
class Object:
|
class Object:
|
||||||
'''
|
'''A drawable object with a symbol and (x, y) position.'''
|
||||||
A drawable object with a symbol and (x, y) position.
|
|
||||||
'''
|
|
||||||
|
|
||||||
def __init__(self, symbol, color=(255, 255, 255), x=0, y=0):
|
def __init__(self, symbol: str, color: tcod.Color = (255, 255, 255), x: int = 0, y: int = 0):
|
||||||
self.__x = int(x)
|
self.__x = int(x)
|
||||||
self.__y = int(y)
|
self.__y = int(y)
|
||||||
self.__color = color
|
self.__color = color
|
||||||
|
@ -30,5 +28,15 @@ class Object:
|
||||||
def y(self, value):
|
def y(self, value):
|
||||||
self.__y = int(value)
|
self.__y = int(value)
|
||||||
|
|
||||||
|
def move(self, dx: int, dy: int):
|
||||||
|
'''Move this object by (dx, dy).'''
|
||||||
|
self.__x += dx
|
||||||
|
self.__y += dy
|
||||||
|
|
||||||
|
def move_to(self, x: int, y: int) -> None:
|
||||||
|
'''Move this object directly to the given position.'''
|
||||||
|
self.__x = x
|
||||||
|
self.__y = y
|
||||||
|
|
||||||
def print(self, console: tcod.Console) -> None:
|
def print(self, console: tcod.Console) -> None:
|
||||||
console.print(x=self.__x, y=self.__y, string=self.__symbol, fg=self.__color)
|
console.print(x=self.__x, y=self.__y, string=self.__symbol, fg=self.__color)
|
||||||
|
|
19
roguebasin/tile.py
Normal file
19
roguebasin/tile.py
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# Eryn Wells <eryn@erynwells.me>
|
||||||
|
|
||||||
|
import tcod
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
class Tile:
|
||||||
|
class Color:
|
||||||
|
WALL = tcod.Color(255, 255, 255)
|
||||||
|
GROUND = tcod.Color(33, 33, 33)
|
||||||
|
|
||||||
|
def __init__(self, blocks_movement: bool, blocks_sight: Optional[bool] = None):
|
||||||
|
self.blocks_movement = blocks_movement
|
||||||
|
|
||||||
|
# If blocks_sight isn't explicitly given, tiles that block movement also block sight.
|
||||||
|
if blocks_sight is None:
|
||||||
|
self.blocks_sight = blocks_movement
|
||||||
|
else:
|
||||||
|
self.blocks_sight = blocks_sight
|
Loading…
Add table
Add a link
Reference in a new issue