Advertisement
ctw6avq

Labirinto.py

Nov 6th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.17 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. from random import shuffle, randrange
  4. import curses
  5. import time
  6. import sys
  7.  
  8.  
  9. def main():
  10.     # janela principal
  11.     stdscr = curses.initscr()
  12.     stdscr.keypad(True)
  13.  
  14.     # iniciando cores
  15.     curses.start_color()
  16.     curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
  17.     curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK)
  18.     curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK)
  19.     curses.init_pair(4, curses.COLOR_RED, curses.COLOR_BLACK)
  20.     curses.init_pair(5, curses.COLOR_BLUE, curses.COLOR_BLACK)
  21.     curses.init_pair(6, curses.COLOR_CYAN, curses.COLOR_BLACK)
  22.     curses.init_pair(7, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
  23.  
  24.     # Não retorna caracteres na tela
  25.     curses.noecho()
  26.  
  27.     # esconde o cursor do mouse e do terminal
  28.     curses.curs_set(0)
  29.  
  30.     # tamanho da janela principal
  31.     stdscr_y, stdscr_x = stdscr.getmaxyx()
  32.  
  33.     # segunda janela
  34.     second = curses.newwin(8, stdscr_x, 0, 0)
  35.     second.box()
  36.  
  37.     # terceira janela
  38.     third = curses.newwin(stdscr_y - 8, stdscr_x, 8, 0)
  39.     third_y, third_x = third.getmaxyx()
  40.     third.box()
  41.  
  42.     # limpar as telas
  43.     second.clear()
  44.     third.clear()
  45.  
  46.     # setando o tempo de espera
  47.     curses.halfdelay(10)
  48.  
  49.     # produz o labirinto
  50.     lab = make_maze((third_x // 3) - 1, (third_y // 2))
  51.  
  52.     # mensagem de créditos
  53.     message = 'Por ctw6av -- Créditos a @Tihup e @SamL'
  54.     second.addstr(0, (stdscr_x // 2) - (len(message) // 2), message)
  55.  
  56.     # variáveis gerais
  57.     text = ' ' * 19 + 'Tempo: {:02.0f}:{:02.0f}:{:02.0f}\tMovimentos: {:<5}'
  58.     info = 'UP {}   DOWN: {}   LEFT: {}   RIGHT: {}   BACK_SPACE: Troca a cor do labirinto'\
  59.            .format('\u2191', '\u2193', '\u2190', '\u2192')
  60.     movements = 0
  61.     start = time.time()
  62.     color = 1
  63.     hour = 0
  64.     minu = 0
  65.  
  66.     second.addstr(6, (stdscr_x // 2) - (len(info) // 2), info)
  67.  
  68.     # inicia a posição do personagem no mapa
  69.     y, x = 1, 1
  70.  
  71.     # loop principal
  72.     while True:
  73.  
  74.         if curses.is_term_resized(stdscr_y, stdscr_x):
  75.             curses.endwin()
  76.             print('Mudança de resolução não é suportada')
  77.             sys.exit(0)
  78.  
  79.         line = 0
  80.         for i in lab:
  81.             try:
  82.                 third.addstr(line, 0, i, curses.color_pair(color))
  83.                 line += 2
  84.             except curses.error:
  85.                 continue
  86.  
  87.         stdscr.refresh()
  88.  
  89.         # This is a fuck'ng mass
  90.         second.addstr(2, (stdscr_x // 2) - (len(text)//2), text.format(hour, minu, int(time.time() - start), movements))
  91.         if int(time.time() - start) == 59:
  92.             minu += 1
  93.             start = time.time()
  94.             if int(minu) == 60:
  95.                 hour += 1
  96.                 minu = 0
  97.  
  98.         second.refresh()
  99.  
  100.         third.addstr(y, x, '()')
  101.         third.refresh()
  102.  
  103.         key = stdscr.getch()
  104.  
  105.         if key == curses.KEY_UP:
  106.             if third.instr(y - 1, x, 2).decode() == '  ':
  107.                 y -= 2
  108.                 movements += 1
  109.             else:
  110.                 curses.beep()
  111.  
  112.         elif key == curses.KEY_DOWN:
  113.             if third.instr(y + 1, x, 2).decode() == '  ':
  114.                 y += 2
  115.                 movements += 1
  116.             else:
  117.                 curses.beep()
  118.  
  119.         elif key == curses.KEY_LEFT:
  120.             if third.instr(y, x-1, 1).decode() == ' ':
  121.                 x -= 3
  122.                 movements += 1
  123.             else:
  124.                 curses.beep()
  125.  
  126.         elif key == curses.KEY_RIGHT:
  127.             if third.instr(y, x + 2, 1).decode() == ' ':
  128.                 x += 3
  129.                 movements += 1
  130.             elif third.instr(y, x + 2, 1).decode() == '#':
  131.                 # setando o tempo de espera
  132.                 curses.halfdelay(60)
  133.                 stdscr.clear()
  134.                 stdscr.addstr(stdscr_y // 2, (stdscr_x // 2) - (len(text) // 2),
  135.                               text.format(hour, minu, int(time.time() - start), movements), curses.color_pair(1))
  136.                 stdscr.refresh()
  137.                 stdscr.getch()
  138.                 curses.endwin()
  139.                 sys.exit(0)
  140.             else:
  141.                 curses.beep()
  142.  
  143.         elif key == curses.KEY_BACKSPACE:
  144.             if color < 7:
  145.                 color += 1
  146.             else:
  147.                 color = 1
  148.  
  149.  
  150. def make_maze(w=32, h=11):
  151.     vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
  152.     ver = [["|  "] * w + ['|'] for _ in range(h)] + [[]]
  153.     hor = [["+--"] * w + ['+'] for _ in range(h + 1)]
  154.  
  155.     def walk(x, y):
  156.         vis[y][x] = 1
  157.  
  158.         d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
  159.         shuffle(d)
  160.  
  161.         for (xx, yy) in d:
  162.             if vis[yy][xx]:
  163.                 continue
  164.             if xx == x:
  165.                 hor[max(y, yy)][x] = "+  "
  166.             if yy == y:
  167.                 ver[y][max(x, xx)] = "   "
  168.             walk(xx, yy)
  169.  
  170.         ver[-2][-1] = '#'
  171.  
  172.     world = []
  173.  
  174.     walk(randrange(w), randrange(h))
  175.     for (a, b) in zip(hor, ver):
  176.         world.append(''.join(a + ['\n'] + b))
  177.  
  178.     return world
  179.  
  180.  
  181. if __name__ == '__main__':
  182.     try:
  183.         curses.wrapper(main())
  184.     except KeyboardInterrupt:
  185.         curses.endwin()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement