squirrel-battle/dungeonbattle/proof_of_concept.py

46 lines
1.5 KiB
Python
Raw Normal View History

2020-10-16 14:46:40 +00:00
from .interfaces import Map
2020-10-16 13:23:58 +00:00
from .term_manager import TermManager
def proof_of_concept() -> None:
2020-10-16 16:05:49 +00:00
"""
Read an example map, parse it, then the (squirrel) player can move freely.
"""
# Load the example map
2020-10-16 14:46:40 +00:00
m = Map.load("example_map.txt")
2020-10-16 13:23:58 +00:00
2020-10-16 16:05:49 +00:00
# Create the context manager to manipulate the screen
2020-10-16 13:23:58 +00:00
with TermManager() as term_manager:
stdscr = term_manager.screen
2020-10-16 16:05:49 +00:00
# Draw the full map
2020-10-16 14:46:40 +00:00
stdscr.addstr(0, 0, m.draw_string())
2020-10-16 13:23:58 +00:00
stdscr.refresh()
cur = [1, 6] # (y,x)
2020-10-16 16:05:49 +00:00
# We are a squirrel
stdscr.addstr(1, 6, '🐿️')
2020-10-16 13:23:58 +00:00
stdscr.refresh()
while True:
2020-10-16 16:05:49 +00:00
# Get movements of the user
2020-10-16 13:23:58 +00:00
key = stdscr.getkey()
stdscr.addstr(cur[0], cur[1], '.')
next_pos = cur[:]
2020-10-16 13:32:15 +00:00
if key == 'z' or key == 'KEY_UP':
next_pos[0] = cur[0] - 1
2020-10-16 13:32:15 +00:00
if key == 's' or key == 'KEY_DOWN':
next_pos[0] = cur[0] + 1
2020-10-16 13:32:15 +00:00
if key == 'q' or key == 'KEY_LEFT':
next_pos[1] = cur[1] - 1
2020-10-16 13:32:15 +00:00
if key == 'd' or key == 'KEY_RIGHT':
next_pos[1] = cur[1] + 1
2020-10-16 16:05:49 +00:00
# Check if we stay in the bounds
if 0 <= next_pos[0] < m.height and 0 <= next_pos[0] < m.width:
next_tile = m.tiles[next_pos[0]][next_pos[1]]
2020-10-16 16:05:49 +00:00
# Check if the new position is valid
if next_tile.can_walk():
cur = next_pos
2020-10-16 16:05:49 +00:00
# Draw the squirrel
stdscr.addstr(cur[0], cur[1], '🐿️')