Phase 04 is mostly just more refactoring not any numpy stuff.

master
Zed A. Shaw 5 days ago
parent e6c70a00a3
commit bb39d525ab
  1. 95
      npy_style/phase_04.py

@ -0,0 +1,95 @@
import curses
import sys
import numpy as np
class Map:
def __init__(self):
self.map = [
list("####################"),
list("###....#############"),
list("###....#############"),
list("###...........######"),
list("#############.######"),
list("#############....###"),
list("#############....###"),
list("####################")]
def move_player(self, player, target_y, target_x):
if self.map[target_y][target_x] != '#':
player.y = target_y
player.x = target_x
def draw(self, win):
for y, row in enumerate(self.map):
win.addstr(y, 0, "".join(row))
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, height-status_height, begin_x)
# 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.status.box()
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=27
height=16
the_map = Map()
ui = UI(stdscr, the_map, height, width, 5)
player = Player(3, 3)
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)
Loading…
Cancel
Save