<s> import base <EOL> import pygame <EOL> import numpy as np <EOL> from raycast import RayCastPlayer <EOL> from pygame . constants import K_w , K_a , K_d , K_s <EOL> class RaycastMaze ( base . Game , RayCastPlayer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> init_pos = ( <NUM_LIT:1> , <NUM_LIT:1> ) , resolution = <NUM_LIT:1> , <EOL> move_speed = <NUM_LIT:20> , turn_speed = <NUM_LIT> , <EOL> map_size = <NUM_LIT:10> , height = <NUM_LIT> , width = <NUM_LIT> ) : <EOL> assert map_size > <NUM_LIT:5> , "<STR_LIT>" <EOL> init_dir = ( <NUM_LIT:1.0> , <NUM_LIT:0.0> ) <EOL> init_plane = ( <NUM_LIT:0.0> , <NUM_LIT> ) <EOL> block_types = { <EOL> <NUM_LIT:0> : { <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : None <EOL> } , <EOL> <NUM_LIT:1> : { <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT>" : ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) <EOL> } , <EOL> <NUM_LIT:2> : { <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT>" : ( <NUM_LIT:255> , <NUM_LIT:100> , <NUM_LIT:100> ) <EOL> } <EOL> } <EOL> actions = { <EOL> "<STR_LIT>" : K_w , <EOL> "<STR_LIT:left>" : K_a , <EOL> "<STR_LIT:right>" : K_d , <EOL> "<STR_LIT>" : K_s <EOL> } <EOL> base . Game . __init__ ( self , width , height , actions = actions ) <EOL> RayCastPlayer . __init__ ( self , None , <EOL> init_pos , init_dir , width , height , resolution , <EOL> move_speed , turn_speed , init_plane , actions , block_types ) <EOL> self . init_pos = np . array ( [ init_pos ] , dtype = np . float32 ) <EOL> self . init_dir = np . array ( [ init_dir ] , dtype = np . float32 ) <EOL> self . init_plane = np . array ( [ init_plane ] , dtype = np . float32 ) <EOL> self . obj_loc = None <EOL> self . map_size = map_size <EOL> def _make_maze ( self , complexity = <NUM_LIT> , density = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> dim = np . floor ( self . map_size / <NUM_LIT:2> ) * <NUM_LIT:2> + <NUM_LIT:1> <EOL> shape = ( dim , dim ) <EOL> complexity = int ( complexity * ( <NUM_LIT:5> * ( shape [ <NUM_LIT:0> ] + shape [ <NUM_LIT:1> ] ) ) ) <EOL> density = int ( density * ( shape [ <NUM_LIT:0> ] // <NUM_LIT:2> * shape [ <NUM_LIT:1> ] // <NUM_LIT:2> ) ) <EOL> Z = np . zeros ( shape , dtype = bool ) <EOL> Z [ <NUM_LIT:0> , : ] = Z [ - <NUM_LIT:1> , : ] = <NUM_LIT:1> <EOL> Z [ : , <NUM_LIT:0> ] = Z [ : , - <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> for i in range ( density ) : <EOL> x = self . rng . random_integers ( <NUM_LIT:0> , shape [ <NUM_LIT:1> ] // <NUM_LIT:2> ) * <NUM_LIT:2> <EOL> y = self . rng . random_integers ( <NUM_LIT:0> , shape [ <NUM_LIT:0> ] // <NUM_LIT:2> ) * <NUM_LIT:2> <EOL> Z [ y , x ] = <NUM_LIT:1> <EOL> for j in range ( complexity ) : <EOL> neighbours = [ ] <EOL> if x > <NUM_LIT:1> : neighbours . append ( ( y , x - <NUM_LIT:2> ) ) <EOL> if x < shape [ <NUM_LIT:1> ] - <NUM_LIT:2> : neighbours . append ( ( y , x + <NUM_LIT:2> ) ) <EOL> if y > <NUM_LIT:1> : neighbours . append ( ( y - <NUM_LIT:2> , x ) ) <EOL> if y < shape [ <NUM_LIT:0> ] - <NUM_LIT:2> : neighbours . append ( ( y + <NUM_LIT:2> , x ) ) <EOL> if len ( neighbours ) : <EOL> y_ , x_ = neighbours [ self . rng . random_integers ( <NUM_LIT:0> , len ( neighbours ) - <NUM_LIT:1> ) ] <EOL> if Z [ y_ , x_ ] == <NUM_LIT:0> : <EOL> Z [ y_ , x_ ] = <NUM_LIT:1> <EOL> Z [ y_ + ( y - y_ ) // <NUM_LIT:2> , x_ + ( x - x_ ) // <NUM_LIT:2> ] = <NUM_LIT:1> <EOL> x , y = x_ , y_ <EOL> return Z . astype ( int ) <EOL> def getGameState ( self ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> def getScore ( self ) : <EOL> return self . score <EOL> def game_over ( self ) : <EOL> obj_loc = self . obj_loc + <NUM_LIT:0.5> <EOL> dist = np . sqrt ( np . sum ( ( self . pos - obj_loc ) ** <NUM_LIT> ) ) <EOL> if dist < <NUM_LIT:1.0> : <EOL> self . score += self . rewards [ "<STR_LIT>" ] <EOL> return True <EOL> else : <EOL> return False <EOL> def init ( self ) : <EOL> self . pos = np . copy ( self . init_pos ) <EOL> self . dir = np . copy ( self . init_dir ) <EOL> self . plane = np . copy ( self . init_plane ) <EOL> self . map_ = self . _make_maze ( ) <EOL> self . obj_loc = self . rng . randint ( <NUM_LIT:3> , high = self . map_size - <NUM_LIT:1> , size = ( <NUM_LIT:2> ) ) <EOL> self . map_ [ self . obj_loc [ <NUM_LIT:0> ] , self . obj_loc [ <NUM_LIT:1> ] ] = <NUM_LIT:2> <EOL> def reset ( self ) : <EOL> self . init ( ) <EOL> def step ( self , dt ) : <EOL> self . screen . fill ( ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> pygame . draw . rect ( self . screen , ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , ( <NUM_LIT:0> , self . height / <NUM_LIT:2> , self . width , self . height ) ) <EOL> self . score += self . rewards [ "<STR_LIT>" ] <EOL> self . _handle_player_events ( dt ) <EOL> c , t , b , col = self . draw ( ) <EOL> for i in range ( len ( c ) ) : <EOL> color = ( col [ i ] [ <NUM_LIT:0> ] , col [ i ] [ <NUM_LIT:1> ] , col [ i ] [ <NUM_LIT:2> ] ) <EOL> p0 = ( c [ i ] , t [ i ] ) <EOL> p1 = ( c [ i ] , b [ i ] ) <EOL> pygame . draw . line ( self . screen , color , p0 , p1 , self . resolution ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import numpy as np <EOL> fps = <NUM_LIT> <EOL> pygame . init ( ) <EOL> game = RaycastMaze ( <EOL> height = <NUM_LIT> , <EOL> width = <NUM_LIT> , <EOL> map_size = <NUM_LIT:10> <EOL> ) <EOL> game . screen = pygame . display . set_mode ( game . getScreenDims ( ) , <NUM_LIT:0> , <NUM_LIT:32> ) <EOL> game . clock = pygame . time . Clock ( ) <EOL> game . rng = np . random . RandomState ( <NUM_LIT> ) <EOL> game . init ( ) <EOL> while True : <EOL> dt = game . clock . tick_busy_loop ( fps ) <EOL> if game . game_over ( ) : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> game . reset ( ) <EOL> game . step ( dt ) <EOL> pygame . display . update ( ) </s>
