#!/usr/bin/env python # -*- coding: utf-8 -*- import random import urwid level_map = """ .. ..... .. ........... .......... .....○.......... ................. ..............................................%..┏━━━.━┓..... ......┏━━━━━┓......................................┃✚...☣┃...... .......┃☢...✚┃.... .............☻...█⚑.┃....... .......┃.⚐█....⛨............ ....┃.....┃....... ......┃☣...✚┃....☺.................................┗━━━━━┛...... .....┗━.━━━┛.................................................. .................. ..........●...... ........... ........ .. .... .. """ global offset_x offset_x = random.randint(0, 20) global offset_y offset_y = random.randint(0, 10) def pad_line(line, maxcol): line_bytes = (u'→' * (-offset_x)) + line.decode('utf-8') if offset_x >= 0: line_bytes_visible = line_bytes[offset_x:maxcol+offset_x] else: line_bytes_visible = line_bytes[:maxcol] result = line_bytes_visible.encode('utf-8') \ + ('←' * (maxcol-len(line_bytes_visible))) return result class Map(urwid.Widget): _sizing = frozenset(['box']) def render(self, size, focus=False): (maxcol, maxrow) = size visible_map = [] if offset_y < 0: visible_map = [ '↓' * maxcol ] * -offset_y # top padding # transform map to lines visible_level_map = [ pad_line(line, maxcol) for line in level_map.split('\n') ] visible_map.extend(visible_level_map) # cut off invisible lines from top: if offset_y > 0: visible_map = visible_map[offset_y:] # cut off invisible lines from bottom visible_map = visible_map[:maxrow] # bottom padding missing_rows = maxrow - len(visible_map) if missing_rows > 0: for i in range(missing_rows): visible_map.append('↑' * (maxcol)) return urwid.TextCanvas( visible_map, maxcol=maxcol ) global map map = Map() def handle_input(key): if key == 'q': raise urwid.ExitMainLoop() global offset_x if key == 'left': offset_x -= 1 elif key == 'right': offset_x += 1 global offset_y if key == 'up': offset_y -= 1 elif key == 'down': offset_y += 1 map._invalidate() global loop loop = urwid.MainLoop( map, [], unhandled_input=handle_input ) loop.run()