def step(self, dt): position = self.body.GetPosition() distance = math.sqrt(position.x ** 2 + position.y ** 2) if distance > self.max_lifeline_range: print "Game Over: Out of Range" sys.exit() angle = self.body.GetAngle() unit = box2d.b2Vec2(-math.sin(angle), math.cos(angle)) force = self.thrust * self.max_thrust * unit self.body.ApplyForce(force, position) self.body.SetAngularVelocity(self.turn * self.max_angular_velocity)
def __init__(self, world, ship=None, radius=None, position=None, linear_velocity=None): super(Asteroid, self).__init__(world) if radius is None: radius = 3.0 * (1.0 + random.random()) if position is None: angle = 2.0 * math.pi * random.random() unit = box2d.b2Vec2(-math.sin(angle), math.cos(angle)) distance = 50.0 * (1.0 + random.random()) position = unit * distance if ship is not None: position += ship.body.GetPosition() if linear_velocity is None: angle = 2.0 * math.pi * random.random() unit = box2d.b2Vec2(-math.sin(angle), math.cos(angle)) linear_velocity = unit * 4.0 * (1.0 + random.random()) self.radius = radius self.color = (0.5 * random.random(), 0.5 * random.random(), 0.5 * random.random() + 0.5) self.body = self.create_body(position, linear_velocity)
def draw_laser(self): if self.ship.firing: position = self.ship.body.GetPosition() angle = self.ship.body.GetAngle() unit = box2d.b2Vec2(-math.sin(angle), math.cos(angle)) endpoint = position + unit * 10.0 glBegin(GL_LINES) glColor4d(1.0, 0.0, 0.0, 1.0) glVertex2d(position.x, position.y) glColor4d(1.0, 0.0, 0.0, 0.0) glVertex2d(endpoint.x, endpoint.y) glEnd()
def __init__(self, world): super(Hub, self).__init__(world) self.color = (1.0, 1.0, 1.0) self.radius = 5.0 self.vertices = [] vertex_count = 90 for i in xrange(vertex_count): angle = i * 2.0 * math.pi / vertex_count vertex = self.radius * box2d.b2Vec2(-math.sin(angle), math.cos(angle)) self.vertices.append(vertex) self.body = self.create_body(world)
def step_laser(self, dt, maybe_dead): if self.ship.firing: angle = self.ship.body.GetAngle() unit = box2d.b2Vec2(-math.sin(angle), math.cos(angle)) segment = box2d.b2Segment() segment.p1 = self.ship.body.GetPosition() segment.p2 = segment.p1 + unit * 10.0 fraction, normal, shape = self.world.RaycastOne(segment, False, None) if shape is not None: agent = shape.GetBody().GetUserData() if type(agent) is Asteroid: maybe_dead.add(agent) agent.power -= self.ship.damage * dt * fraction
def split(self): if self.radius < 1.0: return [] fraction = (1.0 + random.random()) / 3.0 radius_1 = self.radius * math.sqrt(fraction) radius_2 = math.sqrt(self.radius ** 2 - radius_1 ** 2) position = self.body.GetPosition() angle = random.random() * 2.0 * math.pi unit = box2d.b2Vec2(-math.sin(angle), math.cos(angle)) position_1 = position + unit * radius_2 position_2 = position - unit * radius_1 linear_velocity = self.body.GetLinearVelocity() agent_1 = Asteroid(self.world, None, radius_1, position_1, linear_velocity) agent_2 = Asteroid(self.world, None, radius_2, position_2, linear_velocity) return agent_1, agent_2
def create_world(self): world_aabb = box2d.b2AABB() world_aabb.lowerBound.Set(-400.0, -400.0) world_aabb.upperBound.Set(400.0, 400.0) gravity = box2d.b2Vec2(0.0, 0.0) return box2d.b2World(world_aabb, gravity, False)