parent
410ea999bf
commit
d964034ab6
@ -0,0 +1,84 @@ |
||||
import curses |
||||
import sys |
||||
import numpy as np |
||||
|
||||
MAP = np.array([ |
||||
list("####################"), |
||||
list("###....#############"), |
||||
list("###....#############"), |
||||
list("###...........######"), |
||||
list("#############.######"), |
||||
list("#############....###"), |
||||
list("#############....###"), |
||||
list("####################")], dtype=str) |
||||
|
||||
|
||||
def draw_map(win): |
||||
map_line = 1 |
||||
for line in MAP: |
||||
win.addstr(map_line, 1, "".join(line)) |
||||
map_line += 1 |
||||
|
||||
def draw_status(status): |
||||
status.addstr(1, 1, "PLAYER STATS") |
||||
|
||||
def draw_player(win, player_y, player_x): |
||||
win.addstr(player_y, player_x, '@', curses.A_BOLD) |
||||
|
||||
def update(win, status, player_y, player_x): |
||||
win.clear() |
||||
status.box() |
||||
draw_map(win) |
||||
draw_status(status) |
||||
draw_player(win, player_y, player_x) |
||||
win.refresh() |
||||
|
||||
def create_ui(stdscr, width, height, status_height): |
||||
curses.curs_set(0) |
||||
stdscr.clear() |
||||
begin_x = 0 |
||||
begin_y = 0 |
||||
win = curses.newwin(height, width, begin_y, begin_x) |
||||
status = win.subwin(status_height, width-2, height-status_height, begin_x+1) |
||||
win.keypad(True) |
||||
return win, status |
||||
|
||||
def move_player(player_y, player_x, target_y, target_x): |
||||
if MAP[target_y - 1][target_x - 1] != '#': |
||||
return target_y, target_x |
||||
else: |
||||
return player_y, player_x |
||||
|
||||
def handle_input(win, y, x, width, height): |
||||
ch = win.getch() |
||||
|
||||
if ch == ord('q'): |
||||
sys.exit(0) |
||||
elif ch == curses.KEY_UP: |
||||
y = (y - 1) % height |
||||
elif ch == curses.KEY_DOWN: |
||||
y = (y + 1) % height |
||||
elif ch == curses.KEY_RIGHT: |
||||
x = (x + 1) % width |
||||
elif ch == curses.KEY_LEFT: |
||||
x = (x - 1) % width |
||||
|
||||
return y, x |
||||
|
||||
def main(stdscr): |
||||
width=27 |
||||
height=17 |
||||
win, status = create_ui(stdscr, width, height, 5) |
||||
|
||||
player_x = 4 |
||||
player_y = 4 |
||||
|
||||
while True: |
||||
update(win, status, player_y, player_x) |
||||
new_y, new_x = handle_input( |
||||
win, player_y, player_x, width, height) |
||||
|
||||
player_y, player_x = move_player( |
||||
player_y, player_x, new_y, new_x) |
||||
|
||||
curses.wrapper(main) |
Loading…
Reference in new issue