<s> import sys <EOL> import os <EOL> sys . path . append ( os . path . abspath ( '<STR_LIT:..>' ) ) <EOL> import pygcurse , pygame , random , time <EOL> from pygame . locals import * <EOL> BLUE = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT> ) <EOL> YELLOW = ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:0> ) <EOL> GREEN = ( <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:0> ) <EOL> BLACK = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> RED = ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> MAZE_WIDTH = <NUM_LIT> <EOL> MAZE_HEIGHT = <NUM_LIT> <EOL> FPS = <NUM_LIT> <EOL> win = pygcurse . PygcurseWindow ( MAZE_WIDTH , MAZE_HEIGHT , fullscreen = False ) <EOL> pygame . display . set_caption ( '<STR_LIT>' ) <EOL> win . autowindowupdate = False <EOL> win . autoupdate = False <EOL> class JoeWingMaze ( ) : <EOL> def __init__ ( self , width = <NUM_LIT> , height = <NUM_LIT> ) : <EOL> if width % <NUM_LIT:2> == <NUM_LIT:0> : <EOL> width += <NUM_LIT:1> <EOL> if height % <NUM_LIT:2> == <NUM_LIT:0> : <EOL> height += <NUM_LIT:1> <EOL> self . width = width <EOL> self . height = height <EOL> self . maze = dict ( ) <EOL> self . init_maze ( ) <EOL> self . generate_maze ( ) <EOL> def display_maze ( self ) : <EOL> for y in range ( <NUM_LIT:0> , self . height ) : <EOL> for x in range ( <NUM_LIT:0> , self . width ) : <EOL> if self . maze [ x ] [ y ] == <NUM_LIT:0> : <EOL> sys . stdout . write ( "<STR_LIT:U+0020>" ) <EOL> else : <EOL> sys . stdout . write ( "<STR_LIT:#>" ) <EOL> sys . stdout . write ( "<STR_LIT:\n>" ) <EOL> def init_maze ( self ) : <EOL> for x in range ( <NUM_LIT:0> , self . width ) : <EOL> self . maze [ x ] = dict ( ) <EOL> for y in range ( <NUM_LIT:0> , self . height ) : <EOL> self . maze [ x ] [ y ] = <NUM_LIT:1> <EOL> def carve_maze ( self , x , y ) : <EOL> dir = random . randint ( <NUM_LIT:0> , <NUM_LIT:3> ) <EOL> count = <NUM_LIT:0> <EOL> while count < <NUM_LIT:4> : <EOL> dx = <NUM_LIT:0> <EOL> dy = <NUM_LIT:0> <EOL> if dir == <NUM_LIT:0> : <EOL> dx = <NUM_LIT:1> <EOL> elif dir == <NUM_LIT:1> : <EOL> dy = <NUM_LIT:1> <EOL> elif dir == <NUM_LIT:2> : <EOL> dx = - <NUM_LIT:1> <EOL> else : <EOL> dy = - <NUM_LIT:1> <EOL> x1 = x + dx <EOL> y1 = y + dy <EOL> x2 = x1 + dx <EOL> y2 = y1 + dy <EOL> if x2 > <NUM_LIT:0> and x2 < self . width and y2 > <NUM_LIT:0> and y2 < self . height : <EOL> if self . maze [ x1 ] [ y1 ] == <NUM_LIT:1> and self . maze [ x2 ] [ y2 ] == <NUM_LIT:1> : <EOL> self . maze [ x1 ] [ y1 ] = <NUM_LIT:0> <EOL> self . maze [ x2 ] [ y2 ] = <NUM_LIT:0> <EOL> self . carve_maze ( x2 , y2 ) <EOL> count = count + <NUM_LIT:1> <EOL> dir = ( dir + <NUM_LIT:1> ) % <NUM_LIT:4> <EOL> def generate_maze ( self ) : <EOL> random . seed ( ) <EOL> self . carve_maze ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> startx = starty = endx = endy = <NUM_LIT:0> <EOL> while self . maze [ startx ] [ starty ] : <EOL> startx = random . randint ( <NUM_LIT:1> , self . width - <NUM_LIT:2> ) <EOL> starty = random . randint ( <NUM_LIT:1> , self . height - <NUM_LIT:2> ) <EOL> while self . maze [ endx ] [ endy ] or endx == <NUM_LIT:0> or abs ( startx - endx ) < int ( self . width / <NUM_LIT:3> ) or abs ( starty - endy ) < int ( self . height / <NUM_LIT:3> ) : <EOL> endx = random . randint ( <NUM_LIT:1> , self . width - <NUM_LIT:2> ) <EOL> endy = random . randint ( <NUM_LIT:1> , self . height - <NUM_LIT:2> ) <EOL> self . maze [ startx ] [ starty ] = <NUM_LIT:0> <EOL> self . maze [ endx ] [ endy ] = <NUM_LIT:0> <EOL> self . startx = startx <EOL> self . starty = starty <EOL> self . endx = endx <EOL> self . endy = endy <EOL> def main ( ) : <EOL> newGame = True <EOL> solved = False <EOL> moveLeft = moveRight = moveUp = moveDown = False <EOL> lastmovetime = sys . maxsize <EOL> mainClock = pygame . time . Clock ( ) <EOL> while True : <EOL> if newGame : <EOL> newGame = False <EOL> jwmaze = JoeWingMaze ( MAZE_WIDTH , MAZE_HEIGHT ) <EOL> maze = jwmaze . maze <EOL> solved = False <EOL> playerx , playery = jwmaze . startx , jwmaze . starty <EOL> endx , endy = jwmaze . endx , jwmaze . endy <EOL> breadcrumbs = { } <EOL> if ( playerx , playery ) not in breadcrumbs : <EOL> breadcrumbs [ ( playerx , playery ) ] = True <EOL> for event in pygame . event . get ( ) : <EOL> if event . type == QUIT : <EOL> pygame . quit ( ) <EOL> sys . exit ( ) <EOL> elif event . type == KEYDOWN : <EOL> if solved or event . key == K_BACKSPACE : <EOL> newGame = True <EOL> elif event . key == K_ESCAPE : <EOL> pygame . quit ( ) <EOL> sys . exit ( ) <EOL> elif event . key == K_UP : <EOL> moveUp = True <EOL> moveDown = False <EOL> elif event . key == K_DOWN : <EOL> moveDown = True <EOL> moveUp = False <EOL> elif event . key == K_LEFT : <EOL> moveLeft = True <EOL> moveRight = False <EOL> elif event . key == K_RIGHT : <EOL> moveRight = True <EOL> moveLeft = False <EOL> lastmovetime = time . time ( ) - <NUM_LIT:1> <EOL> elif event . type == KEYUP : <EOL> if event . key == K_UP : <EOL> moveUp = False <EOL> elif event . key == K_DOWN : <EOL> moveDown = False <EOL> elif event . key == K_LEFT : <EOL> moveLeft = False <EOL> elif event . key == K_RIGHT : <EOL> moveRight = False <EOL> if time . time ( ) - <NUM_LIT> > lastmovetime : <EOL> if moveUp and isOnBoard ( playerx , playery - <NUM_LIT:1> ) and maze [ playerx ] [ playery - <NUM_LIT:1> ] == <NUM_LIT:0> : <EOL> playery -= <NUM_LIT:1> <EOL> elif moveDown and isOnBoard ( playerx , playery + <NUM_LIT:1> ) and maze [ playerx ] [ playery + <NUM_LIT:1> ] == <NUM_LIT:0> : <EOL> playery += <NUM_LIT:1> <EOL> elif moveLeft and isOnBoard ( playerx - <NUM_LIT:1> , playery ) and maze [ playerx - <NUM_LIT:1> ] [ playery ] == <NUM_LIT:0> : <EOL> playerx -= <NUM_LIT:1> <EOL> elif moveRight and isOnBoard ( playerx + <NUM_LIT:1> , playery ) and maze [ playerx + <NUM_LIT:1> ] [ playery ] == <NUM_LIT:0> : <EOL> playerx += <NUM_LIT:1> <EOL> lastmovetime = time . time ( ) <EOL> if playerx == endx and playery == endy : <EOL> solved = True <EOL> drawMaze ( win , maze , breadcrumbs ) <EOL> if solved : <EOL> win . cursor = ( win . centerx - <NUM_LIT:4> , win . centery ) <EOL> win . write ( '<STR_LIT>' , fgcolor = YELLOW , bgcolor = RED ) <EOL> moveLeft = moveRight = moveUp = moveDown = False <EOL> win . putchar ( '<STR_LIT:@>' , playerx , playery , RED , BLACK ) <EOL> win . putchar ( '<STR_LIT:O>' , jwmaze . endx , jwmaze . endy , GREEN , BLACK ) <EOL> win . update ( ) <EOL> pygame . display . update ( ) <EOL> mainClock . tick ( FPS ) <EOL> def isOnBoard ( x , y ) : <EOL> return x >= <NUM_LIT:0> and y >= <NUM_LIT:0> and x < MAZE_WIDTH and y < MAZE_HEIGHT <EOL> def drawMaze ( win , maze , breadcrumbs ) : <EOL> for x in range ( MAZE_WIDTH ) : <EOL> for y in range ( MAZE_HEIGHT ) : <EOL> if maze [ x ] [ y ] != <NUM_LIT:0> : <EOL> win . paint ( x , y , BLUE ) <EOL> else : <EOL> win . paint ( x , y , BLACK ) <EOL> if ( x , y ) in breadcrumbs : <EOL> win . putchar ( '<STR_LIT:.>' , x , y , RED , BLACK ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
