Example #1
0
 def shot_handler(self, board: PoolBoard) -> Shot:
     time.sleep(1)
     shot = Shot(random_float(0, 360), random_float(100, 150))
     if board.cue_ball.pocketed:
         x = -1.0
         y = -1.0
         while True:
             x = random_float(Constants.BALL_RADIUS + 0.5, Constants.TABLE_WIDTH - Constants.BALL_RADIUS - 0.5)
             y = random_float(Constants.BALL_RADIUS + 0.5, Constants.TABLE_HEIGHT - Constants.BALL_RADIUS - 0.5)
             if Shot.test_cue_ball_position(b2Vec2(x, y), board.balls):
                 break
         shot.cue_ball_position = b2Vec2(x, y)
     return shot
Example #2
0
 def LaunchRandomBomb(self):
     """Creƫer een nieuwe bom en lanceer deze op het testbed.
     
     """
     p = b2Vec2(b2Random(-15.0, 15.0), 30.0)
     v = -5.0 * p
     self.LaunchBomb(p, v)
Example #3
0
 def __createTile(self):
     tl = b2Vec2(-self.TILE_SIZE/2, -self.TILE_SIZE/2)
     tr = b2Vec2(self.TILE_SIZE/2, -self.TILE_SIZE/2)
     br = b2Vec2(self.TILE_SIZE/2, self.TILE_SIZE/2)
     bl = b2Vec2(-self.TILE_SIZE/2, self.TILE_SIZE/2)
     
     shape = b2ChainShape()
     shape.vertices = (bl, br, tr, tl)
     fd = b2FixtureDef()
     fd.categoryBits = Filter.CATEGORY_WALLS
     fd.maskBits = Filter.MASK_WALLS
     fd.bullet = False
     fd.shape = shape
     
     self.mBody = self.mWorld.CreateStaticBody(position = self.mPosition)
     
     self.mBody.CreateFixture(fd)
     self.mBody.userData = self
Example #4
0
 def reset(self, initState=None):
     self.time = 0
     self.episode += 1
     if initState is None:
         for a in self.bodies:
             a.resetState(self.world, b2Vec2(0.1, 0))
         for _ in range(
                 4
         ):  # make a few steps to bring joints to valid states (apply constraints)
             self.world.Step(self.timestep * 2, 3, 6)
     else:
         for a in self.bodies:
             a.resetToState(initState)
     return self._getStates()
Example #5
0
def launchProjectile(level, position, range, missile=False):
    player = level.player
    if player is None:
        return
    
    position = b2Vec2(position[0], position[1])
    distance = (player.body.transform.position - position).Normalize()
    dir = (player.body.transform.position + player.body.linearVelocity*0.5) - position
    if dir.Normalize() < range:
        position = position + dir * 3
        
        if missile:
            obj = Missile.launch(world=level.world, target=player, position=position, velocity=dir * 50, agility=random.uniform(0.2, 2))
            level.objects.append(obj)
        else:
            obj = Missile.launch(world=level.world, target=None, position=position, velocity=dir * 50)
            level.objects.append(obj)
Example #6
0
 def compute_best_shots(self, board : PoolBoard, magnitudes=[75.0, 100.0, 125.0], angles=range(0, 360), length=10) -> List[ComparableShot]:
     position = None
     if board.cue_ball.pocketed:
         while True:
             x = random_float(Constants.BALL_RADIUS + 0.5, Constants.TABLE_WIDTH - Constants.BALL_RADIUS - 0.5)
             y = random_float(Constants.BALL_RADIUS + 0.5, Constants.TABLE_HEIGHT - Constants.BALL_RADIUS - 0.5)
             position = b2Vec2(x, y)
             if Shot.test_cue_ball_position(position, board.balls):
                 break
     queue : List[ComparableShot] = []
     for angle in angles:
         for magnitude in magnitudes:
             if len(queue) % 50 == 0:
                 print(f"Shots generated: {len(queue)}")
             shot = Shot(angle, magnitude, position)
             heapq.heappush(queue, self.compute_shot_heuristic(shot, board))
     shots = []
     for _ in range(length):
         shots.append(heapq.heappop(queue))
     return shots
Example #7
0
 def __init__(self, world, position, tiletype):
     self.mWorld = world
     self.mPosition = b2Vec2(position.x + self.TILE_SIZE/2, position.y + self.TILE_SIZE/2)
     self.mTiletype = tiletype