parent
97e54300a4
commit
fce95dc61d
@ -1,67 +1,90 @@ |
||||
import curses |
||||
import sys |
||||
|
||||
MAP = [ |
||||
"####################", |
||||
"###....#############", |
||||
"###....#############", |
||||
"###...........######", |
||||
"#############.######", |
||||
"#############....###", |
||||
"#############....###", |
||||
"####################"] |
||||
|
||||
|
||||
def update(win, status, player_y, player_x): |
||||
win.erase() |
||||
win.box() |
||||
status.hline(0,0, curses.ACS_HLINE, 78) |
||||
map_line = 1 |
||||
|
||||
for line in MAP: |
||||
win.addstr(map_line, 1, line) |
||||
map_line += 1 |
||||
|
||||
win.addstr(player_y, player_x, '@', curses.A_BOLD) |
||||
status.addstr(1, 1, "PLAYER STATS") |
||||
win.refresh() |
||||
|
||||
|
||||
def main(stdscr): |
||||
class UI: |
||||
def __init__(self, stdscr, height, width, status_height): |
||||
curses.curs_set(0) |
||||
stdscr.clear() |
||||
begin_x = 0 |
||||
begin_y = 0 |
||||
height = 26 |
||||
width = 80 |
||||
win = curses.newwin(height, width, begin_y, begin_x) |
||||
status = win.subwin(5, 78, height-6, begin_x+1) |
||||
|
||||
win = curses.newwin(height, width, begin_y, begin_x) |
||||
win.keypad(True) |
||||
status = win.subwin(status_height, height-2, height-6, begin_x+1) |
||||
|
||||
# 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.win.box() |
||||
self.status.hline(0,0, curses.ACS_HLINE, self.width - 2) |
||||
self.draw_map() |
||||
self.draw_status() |
||||
self.draw_player(player_y, player_x) |
||||
self.win.refresh() |
||||
|
||||
def draw_map(self): |
||||
map_line = 1 |
||||
for line in MAP: |
||||
self.win.addstr(map_line, 1, line) |
||||
map_line += 1 |
||||
|
||||
def draw_status(self): |
||||
self.status.addstr(1, 1, "PLAYER STATS") |
||||
|
||||
player_x = 4 |
||||
player_y = 4 |
||||
|
||||
while True: |
||||
new_x = player_x |
||||
new_y = player_y |
||||
|
||||
update(win, status, player_y, player_x) |
||||
def draw_player(self, player_y, player_x): |
||||
self.win.addstr(player_y, player_x, '@', curses.A_BOLD) |
||||
|
||||
ch = win.getch() |
||||
def handle_input(self, y, x): |
||||
ch = self.win.getch() |
||||
|
||||
if ch == ord('q'): |
||||
break |
||||
sys.exit(0) |
||||
elif ch == curses.KEY_UP: |
||||
new_y = (player_y - 1) % 26 |
||||
y = (y - 1) % self.height |
||||
elif ch == curses.KEY_DOWN: |
||||
new_y = (player_y + 1) % 26 |
||||
y = (y + 1) % self.height |
||||
elif ch == curses.KEY_RIGHT: |
||||
new_x = (player_x + 1) % 80 |
||||
x = (x + 1) % self.width |
||||
elif ch == curses.KEY_LEFT: |
||||
new_x = (player_x - 1) % 80 |
||||
x = (x - 1) % self.width |
||||
|
||||
if MAP[new_y - 1][new_x - 1] != '#': |
||||
player_x, player_y = new_x, new_y |
||||
return y, x |
||||
|
||||
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 main(stdscr): |
||||
width=80 |
||||
height=26 |
||||
ui = UI(stdscr, height, width, 5) |
||||
|
||||
player_x = 4 |
||||
player_y = 4 |
||||
|
||||
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) |
||||
|
Loading…
Reference in new issue