diff --git a/npy_style/phase_01.py b/npy_style/phase_01.py index c046f5e..ec0656b 100644 --- a/npy_style/phase_01.py +++ b/npy_style/phase_01.py @@ -14,10 +14,8 @@ list("####################")], dtype=str) def draw_map(win): - map_line = 1 - for line in MAP: - win.addstr(map_line, 1, "".join(line)) - map_line += 1 + for y, row in enumerate(MAP): + win.addstr(y, 0, "".join(row)) def draw_status(status): status.addstr(1, 1, "PLAYER STATS") @@ -39,12 +37,12 @@ def create_ui(stdscr, width, height, status_height): 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) + status = win.subwin(status_height, width, height-status_height, begin_x) 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] != '#': + if MAP[target_y][target_x] != '#': return target_y, target_x else: return player_y, player_x @@ -70,8 +68,8 @@ def main(stdscr): height=17 win, status = create_ui(stdscr, width, height, 5) - player_x = 4 - player_y = 4 + player_x = 3 + player_y = 3 while True: update(win, status, player_y, player_x) diff --git a/npy_style/phase_02.py b/npy_style/phase_02.py new file mode 100644 index 0000000..813c1c5 --- /dev/null +++ b/npy_style/phase_02.py @@ -0,0 +1,88 @@ +import curses +import sys +import numpy as np + +MAP = np.array([ +list("####################"), +list("###....#############"), +list("###....#############"), +list("###...........######"), +list("#############.######"), +list("#############....###"), +list("#############....###"), +list("####################")], dtype=str) + +class UI: + def __init__(self, stdscr, height, width, status_height): + curses.curs_set(0) + stdscr.clear() + begin_x = 0 + begin_y = 0 + + win = curses.newwin(height, width, begin_y, begin_x) + win.keypad(True) + status = win.subwin(status_height, width, height-status_height, begin_x) + + # keep these for later by assigning to self + self.begin_x = 0 + self.begin_y = 0 + self.height = height + self.width = width + self.win = win + self.status = status + self.status_height = status_height + + def update(self, player_y, player_x): + self.win.clear() + self.status.box() + self.draw_map() + self.draw_status() + self.draw_player(player_y, player_x) + self.win.refresh() + + def draw_map(self): + for y, row in enumerate(MAP): + self.win.addstr(y, 0, "".join(row)) + + def draw_status(self): + self.status.addstr(1, 1, "PLAYER STATS") + + def draw_player(self, player_y, player_x): + self.win.addstr(player_y, player_x, '@', curses.A_BOLD) + + def handle_input(self, y, x): + ch = self.win.getch() + + if ch == ord('q'): + sys.exit(0) + elif ch == curses.KEY_UP: + y = (y - 1) % self.height + elif ch == curses.KEY_DOWN: + y = (y + 1) % self.height + elif ch == curses.KEY_RIGHT: + x = (x + 1) % self.width + elif ch == curses.KEY_LEFT: + x = (x - 1) % self.width + + return y, x + +def move_player(player_y, player_x, target_y, target_x): + if MAP[target_y][target_x] != '#': + return target_y, target_x + else: + return player_y, player_x + +def main(stdscr): + width=27 + height=16 + ui = UI(stdscr, height, width, 5) + + player_x = 3 + player_y = 3 + + while True: + ui.update(player_y, player_x) + new_y, new_x = ui.handle_input(player_y, player_x) + player_y, player_x = move_player(player_y, player_x, new_y, new_x) + +curses.wrapper(main)