<s> import pygame <EOL> import sys <EOL> import math <EOL> import base <EOL> from pygame . constants import K_w , K_a , K_s , K_d <EOL> from primitives import Player , Creep <EOL> from utils . vec2d import vec2d <EOL> from utils import percent_round_int <EOL> class PuckCreep ( pygame . sprite . Sprite ) : <EOL> def __init__ ( self , pos_init , attr , SCREEN_WIDTH , SCREEN_HEIGHT ) : <EOL> pygame . sprite . Sprite . __init__ ( self ) <EOL> self . pos = vec2d ( pos_init ) <EOL> self . attr = attr <EOL> self . SCREEN_WIDTH = SCREEN_WIDTH <EOL> self . SCREEN_HEIGHT = SCREEN_HEIGHT <EOL> image = pygame . Surface ( ( self . attr [ "<STR_LIT>" ] * <NUM_LIT:2> , self . attr [ "<STR_LIT>" ] * <NUM_LIT:2> ) ) <EOL> image . fill ( ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> image . set_colorkey ( ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> pygame . draw . circle ( <EOL> image , <EOL> self . attr [ "<STR_LIT>" ] , <EOL> ( self . attr [ "<STR_LIT>" ] , self . attr [ "<STR_LIT>" ] ) , <EOL> self . attr [ "<STR_LIT>" ] , <EOL> <NUM_LIT:0> <EOL> ) <EOL> image . set_alpha ( int ( <NUM_LIT:255> * <NUM_LIT> ) ) <EOL> pygame . draw . circle ( <EOL> image , <EOL> self . attr [ "<STR_LIT>" ] , <EOL> ( self . attr [ "<STR_LIT>" ] , self . attr [ "<STR_LIT>" ] ) , <EOL> self . attr [ "<STR_LIT>" ] , <EOL> <NUM_LIT:0> <EOL> ) <EOL> self . image = image <EOL> self . rect = self . image . get_rect ( ) <EOL> self . rect . center = pos_init <EOL> def update ( self , ndx , ndy , dt ) : <EOL> self . pos . x += ndx * self . attr [ '<STR_LIT>' ] * dt <EOL> self . pos . y += ndy * self . attr [ '<STR_LIT>' ] * dt <EOL> self . rect . center = ( self . pos . x , self . pos . y ) <EOL> class PuckWorld ( base . Game ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , <EOL> width = <NUM_LIT:64> , <EOL> height = <NUM_LIT:64> ) : <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> self . CREEP_BAD = { <EOL> "<STR_LIT>" : percent_round_int ( width , <NUM_LIT> ) , <EOL> "<STR_LIT>" : percent_round_int ( width , <NUM_LIT> ) , <EOL> "<STR_LIT>" : ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> "<STR_LIT>" : ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> "<STR_LIT>" : <NUM_LIT> * width <EOL> } <EOL> self . CREEP_GOOD = { <EOL> "<STR_LIT>" : percent_round_int ( width , <NUM_LIT> ) , <EOL> "<STR_LIT>" : ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> } <EOL> self . AGENT_COLOR = ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> self . AGENT_SPEED = <NUM_LIT> * width <EOL> self . AGENT_RADIUS = percent_round_int ( width , <NUM_LIT> ) <EOL> self . AGENT_INIT_POS = ( self . AGENT_RADIUS * <NUM_LIT> , self . AGENT_RADIUS * <NUM_LIT> ) <EOL> self . BG_COLOR = ( <NUM_LIT:255> , <NUM_LIT:255> , <NUM_LIT:255> ) <EOL> self . dx = <NUM_LIT:0> <EOL> self . dy = <NUM_LIT:0> <EOL> self . ticks = <NUM_LIT:0> <EOL> def _handle_player_events ( self ) : <EOL> self . dx = <NUM_LIT:0.0> <EOL> self . dy = <NUM_LIT:0.0> <EOL> for event in pygame . event . get ( ) : <EOL> if event . type == pygame . QUIT : <EOL> pygame . quit ( ) <EOL> sys . exit ( ) <EOL> if event . type == pygame . KEYDOWN : <EOL> key = event . key <EOL> if key == self . actions [ "<STR_LIT:left>" ] : <EOL> self . dx -= self . AGENT_SPEED <EOL> if key == self . actions [ "<STR_LIT:right>" ] : <EOL> self . dx += self . AGENT_SPEED <EOL> if key == self . actions [ "<STR_LIT>" ] : <EOL> self . dy -= self . AGENT_SPEED <EOL> if key == self . actions [ "<STR_LIT>" ] : <EOL> self . dy += self . AGENT_SPEED <EOL> def getGameState ( self ) : <EOL> """<STR_LIT>""" <EOL> state = { <EOL> "<STR_LIT>" : self . player . pos . x , <EOL> "<STR_LIT>" : self . player . pos . y , <EOL> "<STR_LIT>" : self . player . vel . x , <EOL> "<STR_LIT>" : self . player . vel . y , <EOL> "<STR_LIT>" : self . good_creep . pos . x , <EOL> "<STR_LIT>" : self . good_creep . pos . y , <EOL> "<STR_LIT>" : self . bad_creep . pos . x , <EOL> "<STR_LIT>" : self . bad_creep . pos . y <EOL> } <EOL> return state <EOL> def getScore ( self ) : <EOL> return self . score <EOL> def game_over ( self ) : <EOL> """<STR_LIT>""" <EOL> return False <EOL> def _rngCreepPos ( self ) : <EOL> r = self . CREEP_GOOD [ '<STR_LIT>' ] <EOL> x = self . rng . uniform ( r * <NUM_LIT:3> , self . width - r * <NUM_LIT> ) <EOL> y = self . rng . uniform ( r * <NUM_LIT:3> , self . height - r * <NUM_LIT> ) <EOL> return ( x , y ) <EOL> def init ( self ) : <EOL> """<STR_LIT>""" <EOL> self . player = Player ( self . AGENT_RADIUS , self . AGENT_COLOR , self . AGENT_SPEED , self . AGENT_INIT_POS , self . width , self . height ) <EOL> self . good_creep = Creep ( <EOL> self . CREEP_GOOD [ '<STR_LIT>' ] , <EOL> self . CREEP_GOOD [ '<STR_LIT>' ] , <EOL> self . _rngCreepPos ( ) , <EOL> ( <NUM_LIT:1> , <NUM_LIT:1> ) , <EOL> <NUM_LIT:0.0> , <EOL> <NUM_LIT:1.0> , <EOL> "<STR_LIT>" , <EOL> self . width , <EOL> self . height , <EOL> <NUM_LIT:0.0> <EOL> ) <EOL> self . bad_creep = PuckCreep ( ( self . width , self . height ) , self . CREEP_BAD , self . screen_dim [ <NUM_LIT:0> ] * <NUM_LIT> , self . screen_dim [ <NUM_LIT:1> ] * <NUM_LIT> ) <EOL> self . creeps = pygame . sprite . Group ( ) <EOL> self . creeps . add ( self . good_creep ) <EOL> self . creeps . add ( self . bad_creep ) <EOL> self . score = <NUM_LIT:0> <EOL> self . ticks = <NUM_LIT:0> <EOL> self . lives = - <NUM_LIT:1> <EOL> def step ( self , dt ) : <EOL> """<STR_LIT>""" <EOL> dt /= <NUM_LIT> <EOL> self . ticks += <NUM_LIT:1> <EOL> self . screen . fill ( self . BG_COLOR ) <EOL> self . score += self . rewards [ "<STR_LIT>" ] <EOL> self . _handle_player_events ( ) <EOL> self . player . update ( self . dx , self . dy , dt ) <EOL> dx = self . player . pos . x - self . good_creep . pos . x <EOL> dy = self . player . pos . y - self . good_creep . pos . y <EOL> dist_to_good = math . sqrt ( dx * dx + dy * dy ) <EOL> dx = self . player . pos . x - self . bad_creep . pos . x <EOL> dy = self . player . pos . y - self . bad_creep . pos . y <EOL> dist_to_bad = math . sqrt ( dx * dx + dy * dy ) <EOL> reward = - dist_to_good <EOL> if dist_to_bad < self . CREEP_BAD [ '<STR_LIT>' ] : <EOL> reward += <NUM_LIT> * ( dist_to_bad - self . CREEP_BAD [ '<STR_LIT>' ] ) / float ( self . CREEP_BAD [ '<STR_LIT>' ] ) <EOL> self . score += reward <EOL> if self . ticks % <NUM_LIT> == <NUM_LIT:0> : <EOL> x , y = self . _rngCreepPos ( ) <EOL> self . good_creep . pos . x = x <EOL> self . good_creep . pos . y = y <EOL> ndx = <NUM_LIT:0.0> if dist_to_bad == <NUM_LIT:0.0> else dx / dist_to_bad <EOL> ndy = <NUM_LIT:0.0> if dist_to_bad == <NUM_LIT:0.0> else dy / dist_to_bad <EOL> self . bad_creep . update ( ndx , ndy , dt ) <EOL> self . good_creep . update ( dt ) <EOL> self . player . draw ( self . screen ) <EOL> self . creeps . draw ( self . screen ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import numpy as np <EOL> pygame . init ( ) <EOL> game = PuckWorld ( width = <NUM_LIT> , height = <NUM_LIT> ) <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 ( <NUM_LIT> ) <EOL> game . step ( dt ) <EOL> pygame . display . update ( ) </s>
