示例#1
0
 def __init__(self, config):
     self.rand = config['RAND']
     self.accel_max = config['ACCEL_MAX']
     self.speed_max = config['SPEED_MAX']
     self.power_rate = config['POWER_RATE']
     self.decay = config['DECAY']
     self.size = config['SIZE']
     self.position = vector(0, 0)
     self.velocity = vector(0, 0)
示例#2
0
 def control_update(self):
     ''' Uses input from the keyboard to control the player. '''
     keys_pressed = pygame.key.get_pressed()
     mousex, mousey = pygame.mouse.get_pos()
     mousex = mousex / SCALE_FACTOR
     mousey = mousey / SCALE_FACTOR - PITCH_WIDTH/2
     position = vector(mousex, mousey)
     theta = angle_between(self.simulator.player.position, position)
     action_map = {
         pygame.K_SPACE: ('kick', (100, theta)),
         pygame.K_w: ('dash', (10,)),
         pygame.K_s: ('dash', (-10,)),
         pygame.K_a: ('turn', (np.pi/32,)),
         pygame.K_d: ('turn', (-np.pi/32,)),
         pygame.K_b: ('toball', None),
         pygame.K_g: ('shootgoal', (mousey,)),
         pygame.K_t: ('turnball', (theta,)),
         pygame.K_p: ('dribble', position),
         pygame.K_k: ('kickto', position)
     }
     action = None
     for key in action_map:
         if keys_pressed[key]:
             action = action_map[key]
             break
     reward, end_episode = self.simulator.update(action)
     if end_episode:
         if reward == 50:
             print "GOAL"
         else:
             print "OUT"
         self.simulator = Simulator()
示例#3
0
    def __init__(self):
        ''' The entities are set up and added to a space. '''
        initial_player = vector(0, uniform(-PITCH_WIDTH/2, PITCH_WIDTH/2))
        angle = angle_between(initial_player, vector(PITCH_LENGTH/2, 0))
        self.player = Player(initial_player, angle)

        initial_ball = initial_player + KICKABLE * angle_position(angle)
        self.ball = Ball(initial_ball)

        initial_goalie = keeper_target(initial_ball)
        angle2 = angle_between(initial_goalie, initial_ball)
        self.goalie = Goalie(initial_goalie, angle2)

        self.entities = [self.player, self.goalie, self.ball]
        self.states = []

        self.time = 0
示例#4
0
 def goal_distance(self):
     ''' Returns the distance from the goal box. '''
     if self.position[0] < PITCH_LENGTH/2:
         if self.position[1] < -GOAL_WIDTH/2:
             bot_corner = vector(PITCH_LENGTH/2, -GOAL_WIDTH/2)
             return norm(self.position - bot_corner)
         elif self.position[1] > GOAL_WIDTH/2:
             top_corner = vector(PITCH_LENGTH/2, GOAL_WIDTH/2)
             return norm(self.position - top_corner)
         else:
             return PITCH_LENGTH/2 - self.position[0]
     else:
         if self.position[1] < -GOAL_WIDTH/2:
             return GOAL_WIDTH/2 - self.position[1]
         elif self.position[1] > GOAL_WIDTH/2:
             return self.position[1] - GOAL_WIDTH/2
         else:
             return 0
示例#5
0
文件: 4.py 项目: pgoultiaev/aoc2018
def calculate_sleep(data):
    all_guards = {}
    start_sleep, end_sleep, current_guard_id = 0, 0, 0

    for line in data:
        v = vector(line, sep=' ')

        if v[2] == 'Guard':
            current_guard_id = v[3].lstrip('#')
            if current_guard_id not in all_guards:
                all_guards[current_guard_id] = [0 for t in range(60)]
        elif v[2] == 'falls':
            start_sleep = int(vector(v[1], sep=':')[1].rstrip(']'))
        else:
            end_sleep = int(vector(v[1], sep=':')[1].rstrip(']'))
            for t in range(start_sleep, end_sleep):
                all_guards[current_guard_id][t] += 1

    return all_guards
示例#6
0
 def tangent_points(self, htc):
     ''' Finds the tangent points on the player wrt to homothetic centre. '''
     diff = htc - self.position
     square = sum(diff**2)
     if square <= self.size**2:
         delta = 0.0
     else:
         delta = np.sqrt(square - self.size**2)
     xt1 = (diff[0]*self.size**2 + self.size*diff[1]*delta) / square
     xt2 = (diff[0]*self.size**2 - self.size*diff[1]*delta) / square
     yt1 = (diff[1]*self.size**2 + self.size*diff[0]*delta) / square
     yt2 = (diff[1]*self.size**2 - self.size*diff[0]*delta) / square
     tangent1 = vector(xt1, yt1) + self.position
     tangent2 = vector(xt1, yt2) + self.position
     tangent3 = vector(xt2, yt1) + self.position
     tangent4 = vector(xt2, yt2) + self.position
     if norm(tangent1 - self.position) == self.size:
         return tangent1, tangent4
     else:
         return tangent2, tangent3
 def jump_to(self, diffx, dy0, dev):
     ''' Jump to a specific position. '''
     time = 2.0 * dy0 / GRAVITY + 1.0
     dx0 = diffx / time - self.velocity[0]
     dx0 = bound(dx0, -MAX_DDX, MAX_DY - dy0)
     if dev > 0:
         noise = -abs(np.random.normal(0.0, dev, 2))
     else:
         noise = np.zeros((2,))
     accel = vector(dx0, dy0) + noise
     self.accelerate(accel / DT)
示例#8
0
 def jump_to(self, diffx, dy0, dev):
     ''' Jump to a specific position. '''
     time = 2.0 * dy0 / GRAVITY + 1.0
     dx0 = diffx / time - self.velocity[0]
     dx0 = bound(dx0, -MAX_DDX, MAX_DY - dy0)
     if dev > 0:
         noise = -abs(np.random.normal(0.0, dev, 2))
     else:
         noise = np.zeros((2, ))
     accel = vector(dx0, dy0) + noise
     self.accelerate(accel / DT)
示例#9
0
 def accelerate(self, power, theta):
     ''' Applies a power to the entity in direction theta. '''
     rrand = uniform(-self.rand, self.rand)
     theta = (1 + rrand) * theta
     rmax = self.rand*norm(self.velocity)
     noise = vector(uniform(-rmax, rmax), uniform(-rmax, rmax))
     rate = float(power) * self.power_rate
     acceleration = rate * angle_position(theta) + noise
     acceleration = bound_vector(acceleration, self.accel_max)
     self.velocity += acceleration
     self.velocity = bound_vector(self.velocity, self.speed_max)
示例#10
0
def keeper_target(ball):
    ''' Target the keeper wants to move towards. '''
    grad, yint = keeper_line(ball)
    if ball[0] < PITCH_LENGTH/2 - GOAL_AREA_LENGTH:
        xval = ball[0]
    else:
        if ball[1] < -GOAL_AREA_WIDTH/2:
            xval = (-GOAL_AREA_WIDTH/2 - yint)/grad
        else:
            xval = (GOAL_AREA_WIDTH/2 - yint)/grad
    xval = bound(xval, PITCH_LENGTH/2 - GOAL_AREA_LENGTH, PITCH_LENGTH/2)
    yval = bound(grad * xval + yint, -GOAL_AREA_WIDTH/2, GOAL_AREA_WIDTH/2)
    return vector(xval, yval)
示例#11
0
 def __init__(self, sim=Simulator()):
     ''' Sets up the colors, pygame, and screen. '''
     pygame.init()
     self.size = (LENGTH, WIDTH)
     self.window = pygame.display.set_mode(self.size)
     self.clock = pygame.time.Clock()
     self.simulator = sim
     self.background = pygame.image.load('./sprites/background.png').convert_alpha()
     self.platform = pygame.image.load('./sprites/platform.png').convert_alpha()
     self.enemy = pygame.image.load('./sprites/enemy.png').convert_alpha()
     self.player = pygame.image.load('./sprites/player.png').convert_alpha()
     self.centre = vector(0, 100)/2
     self.total = 0.0
     self.draw_surf = pygame.Surface(self.size)
     self.draw_background()
示例#12
0
 def __init__(self, sim=Simulator()):
     ''' Sets up the colors, pygame, and screen. '''
     pygame.init()
     self.size = (LENGTH, WIDTH)
     self.window = pygame.display.set_mode(self.size)
     self.clock = pygame.time.Clock()
     self.simulator = sim
     self.background = pygame.image.load(
         './sprites/background.png').convert_alpha()
     self.platform = pygame.image.load(
         './sprites/platform.png').convert_alpha()
     self.enemy = pygame.image.load('./sprites/enemy.png').convert_alpha()
     self.player = pygame.image.load('./sprites/player.png').convert_alpha()
     self.centre = vector(0, 100) / 2
     self.total = 0.0
     self.draw_surf = pygame.Surface(self.size)
     self.draw_background()
示例#13
0
    def move(self, ball, player):
        ''' This moves the goalie. '''
        ball_end = ball.position + ball.velocity / (1 - ball.decay)
        diff = ball_end - ball.position
        grad = diff[1] / diff[0]
        yint = ball.position[1] - grad * ball.position[0]
        goal_y = grad * PITCH_LENGTH/2 + yint
        if ball_end[0] > PITCH_LENGTH/2 and -GOAL_WIDTH/2 - CATCHABLE <= goal_y <= GOAL_WIDTH/2 + CATCHABLE and grad != 0:
	    grad2 = -1/grad
	    yint2 = self.position[1] - grad2 * self.position[0]
	    ballx = (yint2 - yint) / (grad - grad2)
	    bally = grad * ballx + yint
	    target = vector(ballx, bally)
	    self.move_towards(20, target)
            self.orientation = angle_between(self.position, target)
        else:
            self.orientation = angle_between(self.position, ball_end)
            self.move_towards(8, ball_end)
示例#14
0
def startf(x, y, i=0):
    global startT
    global snake, walls, pwalls, thorns, lasers, teledict, foods, aim, cnt
    if x < 25 and x > -25 and y < -80 and y > -100 and i < len(stages.stage):
        clear()
        up(), goto(-40, -10), down(), color("black")
        write("STAGE %d"%(i+1), font=("Arial", 16, "normal"))
        time.sleep(2)
        clear()
        cnt = 0
        aim = vector(0, 0)
        snake = deepcopy(stages.stage[i])[0]
        walls = deepcopy(stages.stage[i])[1]
        pwalls = deepcopy(stages.stage[i])[2]
        thorns = deepcopy(stages.stage[i])[3]
        lasers = deepcopy(stages.stage[i])[4]
        teledict = deepcopy(stages.stage[i])[5]
        foods = deepcopy(stages.stage[i])[6]
        startT = time.time()
        move()
        done()
示例#15
0
class Enemy:
    ''' Defines the enemy. '''

    size = vector(20.0, 30.0)

    def __init__(self, platform):
        ''' Initializes the enemy on the platform. '''
        self.dx = -ENEMY_SPEED
        self.platform = platform
        self.position = self.platform.size + self.platform.position
        self.position[0] -= self.size[0]

    def update(self, dt):
        ''' Shift the enemy along the platform. '''
        right = self.platform.position[0] + self.platform.size[0] - self.size[0]
        if not self.platform.position[0] < self.position[0] < right:
            self.dx *= -1
        self.dx += np.random.normal(0.0, ENEMY_NOISE * dt)
        self.dx = bound(self.dx, -ENEMY_SPEED, ENEMY_SPEED)
        self.position[0] += self.dx * dt
        self.position[0] = bound(self.position[0], self.platform.position[0],
                                 right)
示例#16
0
 def run(self, power, dt):
     ''' Run for a given power and time. '''
     if dt > 0:
         self.accelerate(vector(power / dt, 0.0), dt)
示例#17
0
 def __init__(self):
     ''' Initialize the position to the starting platform. '''
     self.position = vector(0, PLATHEIGHT)
     self.velocity = vector(0.0, 0.0)
示例#18
0
def bound_vector(vect, xmax, ymax):
    ''' Bounds a vector between a negative and positive maximum range. '''
    xval = bound(vect[0], -xmax, xmax)
    yval = bound(vect[1], -ymax, ymax)
    return vector(xval, yval)
示例#19
0
from util import square, vector, triangle, circle, line, squarenum
from turtle import *

setup(600, 600, 370, 0)
hideturtle()
tracer(False)
listen()

#stage setting
snake = [vector(20, 20)]
walls = {vector(100, 0), vector(-100, 0)}
pwalls = {vector(150, 0): 5}
thorns = {vector(10, 10)}
lasers = [[vector(-100, 0), vector(100, 0)]]
teledict = {vector(0, 100): vector(0, -100)}
foods = {vector(0, 0), vector(50, 50)}

for body in snake:
    square(body.x, body.y, 9, 'black')
for telep in teledict:
    square(telep.x, telep.y, 9, 'magenta')
    square(teledict[telep].x, teledict[telep].y, 9, 'purple')
for food in foods:
    square(food.x, food.y, 9, 'green')
for i in range(len(lasers)):
    line(lasers[i][0].x, lasers[i][0].y + 4.5, lasers[i][1].x,
         lasers[i][1].y + 4.5)
for wall in walls:
    square(wall.x, wall.y, 9, 'gray')
for pwall in pwalls:
    squarenum(pwall.x, pwall.y, 9, 'blue', pwalls[pwall])
示例#20
0
文件: 3.py 项目: pgoultiaev/aoc2018
def parse(line):
    v = vector(line, sep=' ')
    claim_id = v[0]
    l, t = vector(v[2].rstrip(':'), sep=',')
    w, h = vector(v[3], sep='x')
    return claim_id, l, t, w, h
示例#21
0
 def run(self, power, dt):
     ''' Run for a given power and time. '''
     if dt > 0:
         self.accelerate(vector(power / dt, 0.0), dt)
示例#22
0
 def fall(self):
     ''' Apply gravity. '''
     self.accelerate(vector(0.0, -GRAVITY))
示例#23
0
def ball_features(state):
    ''' Returns ball-based position features. '''
    ball = vector(state[10], state[11])
    keeper = vector(state[5], state[6])
    diff = (ball - keeper) / norm(ball - keeper)
    return np.array([1, state[10], state[11], diff[0], diff[1]])
示例#24
0
 def jump(self, power):
     ''' Jump up for a single step. '''
     self.accelerate(vector(0.0, power / DT))
示例#25
0
 def __init__(self):
     ''' Initialize the position to the starting platform. '''
     self.position = vector(0, PLATHEIGHT)
     self.velocity = vector(0.0, 0.0)
示例#26
0
 def fall(self):
     ''' Apply gravity. '''
     self.accelerate(vector(0.0, -GRAVITY))
示例#27
0
def bound_vector(vect, maximum):
    ''' Bounds a vector between a negative and positive maximum range. '''
    xval = bound(vect[0], -maximum, maximum)
    yval = bound(vect[1], -maximum, maximum)
    return vector(xval, yval)
示例#28
0
def bound_vector(vect, xmax, ymax):
    ''' Bounds a vector between a negative and positive maximum range. '''
    xval = bound(vect[0], -xmax, xmax)
    yval = bound(vect[1], -ymax, ymax)
    return vector(xval, yval)
示例#29
0
 def jump(self, power):
     ''' Jump up for a single step. '''
     self.accelerate(vector(0.0, power / DT))
示例#30
0
def ball_features(state):
    ''' Returns ball-based position features. '''
    ball = vector(state[10], state[11])
    keeper = vector(state[5], state[6])
    diff = (ball - keeper) / norm(ball - keeper)
    return np.array([1, state[10], state[11], diff[0], diff[1]])
示例#31
0
 def __init__(self, xpos, ypos, width):
     self.position = vector(xpos, ypos)
     self.size = vector(width, PLATHEIGHT)
示例#32
0
 def __init__(self, xpos, ypos, width):
     self.position = vector(xpos, ypos)
     self.size = vector(width, PLATHEIGHT)
示例#33
0
 def shoot_goal(self, ball, ypos):
     ''' Shoot the goal at a targeted position on the goal line. '''
     ypos = bound(ypos, -GOAL_WIDTH/2, GOAL_WIDTH/2)
     target = vector(PITCH_LENGTH/2 + ball.size, ypos)
     self.kick_to(ball, target)