From 113e50708abc57fe17c456818944ba8678acc6b6 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sat, 19 Jul 2025 12:43:45 -0400 Subject: [PATCH] Had to fix how the subwindow was made and now player is refactored into a class. --- phase_four.py | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++ phase_one.py | 2 +- phase_three.py | 2 +- phase_two.py | 2 +- 4 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 phase_four.py diff --git a/phase_four.py b/phase_four.py new file mode 100644 index 0000000..848673c --- /dev/null +++ b/phase_four.py @@ -0,0 +1,98 @@ +import curses +import sys + + +class Map: + def __init__(self): + self.map = [ + "####################", + "###....#############", + "###....#############", + "###...........######", + "#############.######", + "#############....###", + "#############....###", + "####################"] + + def move_player(self, player, target_y, target_x): + if self.map[target_y - 1][target_x - 1] != '#': + player.y = target_y + player.x = target_x + + def draw(self, win): + map_line = 1 + for line in self.map: + win.addstr(map_line, 1, line) + map_line += 1 + +class UI: + def __init__(self, stdscr, the_map, 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-2, height-status_height, begin_x+1) + + # keep these for later by assigning to self + self.begin_x = 0 + self.begin_y = 0 + self.map = the_map + self.height = height + self.width = width + self.win = win + self.status = status + self.status_height = status_height + + def update(self, player): + self.win.clear() + self.win.box() + self.status.hline(0,0, curses.ACS_HLINE, self.width - 2) + self.map.draw(self.win) + self.draw_status() + self.draw_player(player) + self.win.refresh() + + def draw_status(self): + self.status.addstr(1, 1, "PLAYER STATS") + + def draw_player(self, player): + 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 + +class Player: + def __init__(self, x, y): + self.x = x + self.y = y + + +def main(stdscr): + width=80 + height=26 + the_map = Map() + ui = UI(stdscr, the_map, height, width, 5) + player = Player(4, 4) + + while True: + ui.update(player) + new_y, new_x = ui.handle_input(player.y, player.x) + the_map.move_player(player, new_y, new_x) + +curses.wrapper(main) diff --git a/phase_one.py b/phase_one.py index 8b15936..e029235 100644 --- a/phase_one.py +++ b/phase_one.py @@ -38,7 +38,7 @@ def create_ui(stdscr, height, width, status_height): begin_x = 0 begin_y = 0 win = curses.newwin(height, width, begin_y, begin_x) - status = win.subwin(status_height, height-2, height-6, begin_x+1) + status = win.subwin(status_height, width-2, height-status_height, begin_x+1) win.keypad(True) return win, status diff --git a/phase_three.py b/phase_three.py index 3814c67..fc71702 100644 --- a/phase_three.py +++ b/phase_three.py @@ -36,7 +36,7 @@ class UI: win = curses.newwin(height, width, begin_y, begin_x) win.keypad(True) - status = win.subwin(status_height, height-2, height-6, begin_x+1) + status = win.subwin(status_height, width-2, height-status_height, begin_x+1) # keep these for later by assigning to self self.begin_x = 0 diff --git a/phase_two.py b/phase_two.py index 44f9328..3c713e2 100644 --- a/phase_two.py +++ b/phase_two.py @@ -20,7 +20,7 @@ class UI: win = curses.newwin(height, width, begin_y, begin_x) win.keypad(True) - status = win.subwin(status_height, height-2, height-6, begin_x+1) + status = win.subwin(status_height, width-2, height-status_height, begin_x+1) # keep these for later by assigning to self self.begin_x = 0