#!/usr/bin/env python3 from random import shuffle, randrange import curses import time import sys def main(): # janela principal stdscr = curses.initscr() stdscr.keypad(True) # iniciando cores curses.start_color() curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK) curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK) curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK) curses.init_pair(4, curses.COLOR_RED, curses.COLOR_BLACK) curses.init_pair(5, curses.COLOR_BLUE, curses.COLOR_BLACK) curses.init_pair(6, curses.COLOR_CYAN, curses.COLOR_BLACK) curses.init_pair(7, curses.COLOR_MAGENTA, curses.COLOR_BLACK) # Não retorna caracteres na tela curses.noecho() # esconde o cursor do mouse e do terminal curses.curs_set(0) # tamanho da janela principal stdscr_y, stdscr_x = stdscr.getmaxyx() # segunda janela second = curses.newwin(8, stdscr_x, 0, 0) second.box() # terceira janela third = curses.newwin(stdscr_y - 8, stdscr_x, 8, 0) third_y, third_x = third.getmaxyx() third.box() # limpar as telas second.clear() third.clear() # setando o tempo de espera curses.halfdelay(10) # produz o labirinto lab = make_maze((third_x // 3) - 1, (third_y // 2)) # mensagem de créditos message = 'Por ctw6av -- Créditos a @Tihup e @SamL' second.addstr(0, (stdscr_x // 2) - (len(message) // 2), message) # variáveis gerais text = ' ' * 19 + 'Tempo: {:02.0f}:{:02.0f}:{:02.0f}\tMovimentos: {:<5}' info = 'UP {} DOWN: {} LEFT: {} RIGHT: {} BACK_SPACE: Troca a cor do labirinto'\ .format('\u2191', '\u2193', '\u2190', '\u2192') movements = 0 start = time.time() color = 1 hour = 0 minu = 0 second.addstr(6, (stdscr_x // 2) - (len(info) // 2), info) # inicia a posição do personagem no mapa y, x = 1, 1 # loop principal while True: if curses.is_term_resized(stdscr_y, stdscr_x): curses.endwin() print('Mudança de resolução não é suportada') sys.exit(0) line = 0 for i in lab: try: third.addstr(line, 0, i, curses.color_pair(color)) line += 2 except curses.error: continue stdscr.refresh() # This is a fuck'ng mass second.addstr(2, (stdscr_x // 2) - (len(text)//2), text.format(hour, minu, int(time.time() - start), movements)) if int(time.time() - start) == 59: minu += 1 start = time.time() if int(minu) == 60: hour += 1 minu = 0 second.refresh() third.addstr(y, x, '()') third.refresh() key = stdscr.getch() if key == curses.KEY_UP: if third.instr(y - 1, x, 2).decode() == ' ': y -= 2 movements += 1 else: curses.beep() elif key == curses.KEY_DOWN: if third.instr(y + 1, x, 2).decode() == ' ': y += 2 movements += 1 else: curses.beep() elif key == curses.KEY_LEFT: if third.instr(y, x-1, 1).decode() == ' ': x -= 3 movements += 1 else: curses.beep() elif key == curses.KEY_RIGHT: if third.instr(y, x + 2, 1).decode() == ' ': x += 3 movements += 1 elif third.instr(y, x + 2, 1).decode() == '#': # setando o tempo de espera curses.halfdelay(60) stdscr.clear() stdscr.addstr(stdscr_y // 2, (stdscr_x // 2) - (len(text) // 2), text.format(hour, minu, int(time.time() - start), movements), curses.color_pair(1)) stdscr.refresh() stdscr.getch() curses.endwin() sys.exit(0) else: curses.beep() elif key == curses.KEY_BACKSPACE: if color < 7: color += 1 else: color = 1 def make_maze(w=32, h=11): vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)] ver = [["| "] * w + ['|'] for _ in range(h)] + [[]] hor = [["+--"] * w + ['+'] for _ in range(h + 1)] def walk(x, y): vis[y][x] = 1 d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)] shuffle(d) for (xx, yy) in d: if vis[yy][xx]: continue if xx == x: hor[max(y, yy)][x] = "+ " if yy == y: ver[y][max(x, xx)] = " " walk(xx, yy) ver[-2][-1] = '#' world = [] walk(randrange(w), randrange(h)) for (a, b) in zip(hor, ver): world.append(''.join(a + ['\n'] + b)) return world if __name__ == '__main__': try: curses.wrapper(main()) except KeyboardInterrupt: curses.endwin()