Cleaner proof of concept

This commit is contained in:
Yohann D'ANELLO 2020-10-16 15:23:58 +02:00 committed by Nicolas Margulies
parent 4175238297
commit 12413746fd
4 changed files with 42 additions and 45 deletions

View File

@ -1,44 +0,0 @@
import curses
from term_manager import TermManager
import time
filename = "map.txt"
A = open(filename)
M = []
i = 0
for lines in A :
B = list(lines)[:-1]
M.append(B)
i+=1
print(M)
def main(stdscr):
for y in range(len(M)): #len(M)
for x in range(len(M[0])): #len(M[0])
stdscr.addstr(y,x,M[y][x])
stdscr.refresh()
cur = [1,6] #(y,x)
stdscr.addstr(1,6,'@')
stdscr.refresh()
for i in range(10) :
key = stdscr.getkey()
stdscr.addstr(cur[0],cur[1],'.')
if key == 'z' :
if cur[0]>0 and M[cur[0]-1][cur[1]] != '#' :
cur[0] = cur[0]-1
if key == 's' :
if cur[0]<len(M)-1 and M[cur[0]+1][cur[1]] != '#' :
cur[0] = cur[0]+1
if key == 'q' :
if cur[1]>0 and M[cur[0]][cur[1]-1] != '#' :
cur[1] = cur[1]-1
if key == 'd' :
if cur[1]<len(M[0]) and M[cur[0]][cur[1]+1] != '#' :
cur[1] = cur[1]+1
stdscr.addstr(cur[0],cur[1],'@')
with TermManager() as T :
main(T.screen)

View File

@ -0,0 +1,40 @@
#!/usr/bin/env python
from .term_manager import TermManager
def proof_of_concept() -> None:
matrix = []
with open("example_map.txt") as f:
for line in f.readlines():
matrix.append(line[:-1])
with TermManager() as term_manager:
stdscr = term_manager.screen
for y in range(len(matrix)):
for x in range(len(matrix[0])):
stdscr.addstr(y, x, matrix[y][x])
stdscr.refresh()
cur = [1, 6] # (y,x)
stdscr.addstr(1, 6, '@')
stdscr.refresh()
while True:
key = stdscr.getkey()
stdscr.addstr(cur[0], cur[1], '.')
if key == 'z':
if cur[0] > 0 and matrix[cur[0] - 1][cur[1]] != '#':
cur[0] = cur[0] - 1
if key == 's':
if cur[0] < len(matrix) - 1 and \
matrix[cur[0] + 1][cur[1]] != '#':
cur[0] = cur[0] + 1
if key == 'q':
if cur[1] > 0 and matrix[cur[0]][cur[1] - 1] != '#':
cur[1] = cur[1] - 1
if key == 'd':
if cur[1] < len(matrix[0]) and \
matrix[cur[0]][cur[1] + 1] != '#':
cur[1] = cur[1] + 1
stdscr.addstr(cur[0], cur[1], '@')

3
main.py Normal file → Executable file
View File

@ -1,4 +1,5 @@
#!/usr/bin/env python
from dungeonbattle.proof_of_concept import proof_of_concept
if __name__ == "__main__":
print("Hello world!")
proof_of_concept()