Last active
September 30, 2019 11:49
-
-
Save paperbenni/947ee377b99b13951a559217374267f6 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
import random | |
import curses | |
import time | |
from automaton import machines | |
# automat start | |
################################################ | |
################################################ | |
################################################ | |
player = machines.FiniteMachine() | |
player.add_state('up') | |
player.add_state('down') | |
player.add_state('left') | |
player.add_state('right') | |
player.add_transition('right', 'up', 'wall') | |
upstates = ['up'] | |
downstates = ['down'] | |
leftstates = ['left'] | |
rightstates = ['right'] | |
player.default_start_state = 'right' | |
player.initialize() | |
################################################ | |
################################################ | |
################################################ | |
floortile = 0 | |
walltile = 1 | |
playerchar = '#' | |
sc = curses.initscr() | |
h, w = (15, 50) | |
playground = [[0 for _ in range(w)] for _ in range(h)] | |
for i in playground: | |
for _ in range(2): | |
index = random.randint(0, w - 1) | |
i[index] = walltile | |
playground[0] = [1 for _ in range(w - 1)] | |
playground[h - 2] = [1 for _ in range(w - 1)] | |
for i in playground: | |
i[0] = 1 | |
i[len(i) - 2] = 1 | |
win = curses.newwin(h, w, 0, 0) | |
win.keypad(1) | |
curses.curs_set(0) | |
up = (-1, 0) | |
down = (1, 0) | |
left = (0, -1) | |
right = (0, 1) | |
player_facing = down | |
player_pos = [random.randint(5, h - 5), random.randint(5, w - 5)] | |
for y in range(h - 1): | |
for i in range(w - 1): | |
if playground[y][i] == walltile: | |
win.addch(y, i, '█') | |
def gettile(x, y): | |
return playground[x][y] | |
def getplayer(): | |
return (player_pos[0], player_pos[1]) | |
while not playground[player_pos[0]][player_pos[1]] == 1: | |
win.addch(*getplayer(), ' ') | |
facedtile = gettile(*tuple(map(sum, zip(getplayer(), player_facing)))) | |
facedtile2 = gettile( | |
*tuple(map(sum, zip(getplayer(), (player_facing[0] * 2, player_facing[1] * 2))))) | |
event = False | |
if facedtile2 == 1: | |
if player.is_actionable_event('wall2'): | |
player.process_event('wall2') | |
event = True | |
elif facedtile == 1: | |
if player.is_actionable_event('wall'): | |
player.process_event('wall') | |
event = True | |
else: | |
if player.is_actionable_event('floor'): | |
player.process_event('floor') | |
if player.current_state in upstates: | |
player_facing = up | |
playerchar = '^' | |
if player.current_state in downstates: | |
player_facing = down | |
playerchar = 'v' | |
if player.current_state in leftstates: | |
player_facing = left | |
playerchar = '<' | |
if player.current_state in rightstates: | |
player_facing = right | |
playerchar = '>' | |
if not event: | |
playery, playerx = getplayer() | |
playground[playery][playery] = 1 | |
win.addch(playery, playerx, '█') | |
if player_facing == up: | |
player_pos[0] -= 1 | |
elif player_facing == down: | |
player_pos[0] += 1 | |
elif player_facing == left: | |
player_pos[1] -= 1 | |
elif player_facing == right: | |
player_pos[1] += 1 | |
win.addch(player_pos[0], player_pos[1], playerchar) | |
win.refresh() | |
time.sleep(1) | |
time.sleep(0.5) | |
curses.nocbreak() | |
sc.keypad(False) | |
curses.echo() | |
curses.endwin() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment