Created
July 31, 2015 11:39
-
-
Save justinfay/9d77b56149c651af6352 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Super simple construction free roam RPG. | |
| """ | |
| from itertools import product | |
| import os | |
| import sys | |
| from getch import getch | |
| class BaseTile(object): | |
| _repr = None | |
| def __init__(self): | |
| self.object_ = None | |
| def __repr__(self): | |
| if self.object_: | |
| return repr(self.object_) | |
| return repr(self._repr) | |
| class EarthTile(BaseTile): | |
| _repr = ' ' | |
| class Trail(object): | |
| def __init__(self, vertical=True): | |
| if vertical: | |
| self.trail = '|' | |
| else: | |
| self.trail = '-' | |
| def __repr__(self): | |
| return self.trail | |
| class World(object): | |
| """ | |
| A self expanding world. | |
| """ | |
| def __init__(self): | |
| self._tiles = {(0, 0): EarthTile()} | |
| def move_player(self, player, direction): | |
| # Find where player currently is. | |
| for coord, tile in self._tiles.iteritems(): | |
| if tile.object_ == player: | |
| cur = coord | |
| if direction == RIGHT: | |
| new = (cur[0] + 1, cur[1]) | |
| trail = Trail(vertical=False) | |
| elif direction == LEFT: | |
| new = (cur[0] - 1, cur[1]) | |
| trail = Trail(vertical=False) | |
| elif direction == UP: | |
| new = (cur[0], cur[1] - 1) | |
| trail = Trail() | |
| else: | |
| new = (cur[0], cur[1] + 1) | |
| trail = Trail() | |
| if new not in self._tiles: | |
| self._tiles[new] = EarthTile() | |
| self._tiles[new].object_ = player | |
| self._tiles[cur].object_ = trail | |
| def render(self): | |
| os.system('clear') | |
| minx = min(c[0] for c in self._tiles) | |
| maxx = max(c[0] for c in self._tiles) | |
| miny = min(c[1] for c in self._tiles) | |
| maxy = max(c[1] for c in self._tiles) | |
| line = miny | |
| for coord in sorted(list(product( | |
| range(minx, maxx + 1), | |
| range(miny, maxy + 1))), key=lambda x: x[1]): | |
| if coord[1] != line: | |
| print '\n' | |
| line += 1 | |
| if coord not in self._tiles: | |
| print '#', | |
| else: | |
| print self._tiles[coord], | |
| class Player(object): | |
| """ | |
| The in game character. | |
| """ | |
| def __repr__(self): | |
| return 'o' | |
| UP = 'UP' | |
| DOWN = 'DOWN' | |
| LEFT = 'LEFT' | |
| RIGHT = 'RIGHT' | |
| DIRECTION_MAP = { | |
| 65: UP, | |
| 66: DOWN, | |
| 67: RIGHT, | |
| 68: LEFT, | |
| } | |
| def get_direction(): | |
| key = ord(getch()) | |
| if key == 27: | |
| key = ord(getch()) | |
| if key == 91: | |
| return DIRECTION_MAP[ord(getch())] | |
| def run(): | |
| player = Player() | |
| world = World() | |
| world._tiles[(0, 0)].object_ = player | |
| while True: | |
| try: | |
| cmd = get_direction() | |
| if cmd is None: | |
| sys.stderr.write('Bad keypress\n') | |
| sys.stderr.flush() | |
| world.move_player(player, cmd) | |
| world.render() | |
| except KeyboardInterrupt: | |
| print world._tiles | |
| break | |
| if __name__ == "__main__": | |
| sys.exit(run()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment