def update(self, stage): self.velocity.add(self.acceleration) self.position.add(self.velocity) if (((self.position.z + self.radius) > stage.paddle0.z - 25 and (self.velocity.z > 0))): if (self.position.x > stage.paddle0.x - stage.paddle0.WIDTH / 2 and self.position.x < stage.paddle0.x + stage.paddle0.WIDTH / 2 and self.position.y > stage.paddle0.y - stage.paddle0.HEIGHT / 2 and self.position.x < stage.paddle0.y + stage.paddle0.HEIGHT / 2): self.velocity.z *= -1.1 if math.fabs(stage.paddle0.vx) > 10: self.acceleration.x = -1 * stage.paddle0.vx / 35 if math.fabs(stage.paddle0.vy) > 10: self.acceleration.y = stage.paddle0.vy / 35 else: self.velocity = Vector(0, 0, 50) self.position = Vector(0, 0, -1000) if (self.position.z - self.radius) < stage.paddle1.z + 20 and ( self.velocity.z < 0): self.velocity.z *= -1.1 self.acceleration = Vector(0, 0, 0) if (((self.position.x - self.radius) < -640 and self.velocity.x < 0) or (self.position.x + self.radius) > 640 and self.velocity.x > 0): self.velocity.x *= -1 if (((self.position.y - self.radius) < -360 and self.velocity.y < 0) or (self.position.y + self.radius) > 360 and self.velocity.y > 0): self.velocity.y *= -1
def fire(self, delta_time): if not self.can_shoot: return player_v = self.body.velocity / 300 if self.pressed_keys["up"]: velocity = Vector(0, -1) + (player_v.x, 0) elif self.pressed_keys["down"]: velocity = Vector(0, 1) + (player_v.x, 0) elif self.pressed_keys["left"]: velocity = Vector(-1, 0) + (0, player_v.y) elif self.pressed_keys["right"]: velocity = Vector(1, 0) + (0, player_v.y) else: return velocity.normalize() velocity *= self.shot_speed tear = PlayerTear(position=self.body.collider.center(), velocity=velocity, range=self.range, damage=self.shot_damage) self.game.add(tear) self.can_shoot = False def reset_shoot(): self.can_shoot = True self.game.add_timer(self.tears / 10, reset_shoot) self.on_fire.dispatch(self, tear)
class Door(Barrier): positions = [ Vector(ROOM_WIDTH * TILE_SIZE / 2, ROOM_HEIGHT * TILE_SIZE + WALL_SIZE / 2), Vector(ROOM_WIDTH * TILE_SIZE / 2, -WALL_SIZE / 2), Vector(ROOM_WIDTH * TILE_SIZE + WALL_SIZE / 2, ROOM_HEIGHT * TILE_SIZE / 2), Vector(-WALL_SIZE / 2, ROOM_HEIGHT * TILE_SIZE / 2) ] size = (2 * TILE_SIZE, WALL_SIZE + 5) def __init__(self, direction): position = Door.positions[direction] size = Door.size if direction in (DOWN, UP) else Door.size[::-1] self.body = RigidBody(collider=RectCollider, position=position, size=size, mass=0) self.sprite = RectSprite(colors.WHITE, size) self.layer = layers.OBSTACLES self.direction = direction self.enabled = True self.on_collide += enter_door self.on_mount += door_mount
def _loop(self): particle = Particle(Vector(WIDTH / 2, 0, 0), Vector(0, 0, 0), Vector(0, 10e-4, 0), 0.99, 10) self.clock.tick() while True: tick = self.clock.tick() particle.integrate(tick) self._draw(particle)
def create_given_vector(self, x_var, y_var, value): vector = None if x_var == "angle": if y_var != "horizontal_component": vector = Vector(x=16, theta=math.radians(value)) else: vector = Vector(y=16, theta=math.radians(value)) return vector
def get_unitless_state_vectors(self): ec = 0.1 # need to use non-zero eccentricity i = math.radians(self.i) o0 = math.radians(self.o) w0 = math.radians(self.w) eca = ec / 2.0 diff = 1000.0 eps = 0.000001 e1 = 0.0 while diff > eps: e1 = eca - (eca - ec * math.sin(eca)) / (1 - ec * math.cos(eca)) diff = math.fabs(e1 - eca) eca = e1 ceca = math.cos(eca) seca = math.sin(eca) e1 = math.sqrt(math.fabs(1.0 - ec * ec)) xw = ceca - ec yw = e1 * seca edot = 1.0 / (1.0 - ec * ceca) xdw = -edot * seca ydw = e1 * edot * ceca Cw = math.cos(w0) Sw = math.sin(w0) co = math.cos(o0) so = math.sin(o0) ci = math.cos(i) si = math.sin(i) swci = Sw * ci cwci = Cw * ci pX = Cw * co - so * swci pY = Cw * so + co * swci pZ = Sw * si qx = -Sw * co - so * cwci qy = -Sw * so + co * cwci qz = Cw * si x = xw * pX + yw * qx y = xw * pY + yw * qy z = xw * pZ + yw * qz xd = xdw * pX + ydw * qx yd = xdw * pY + ydw * qy zd = xdw * pZ + ydw * qz return (Vector(x, y, z), Vector(xd, yd, zd))
def __init__(self, ship, config): self.ship = ship self.model = "ball" self.hp = 1 self.mass = 1000 self.radius = 1 self.position = Vector() self.orientation = Vector(1, 0, 0) self.energy = 0.0 self.idle = True self.__dict__.update(config) self.orientation = Vector(*self.orientation) self.position = Vector(*self.position) print(self.hp)
def main(): car = Body(17500 / G, (30.5, 30.5)) driver = Body(850 / G, (31.5, 31)) fuel = Body(993 / G, (28, 30.5)) total_mass = sum([car.mass, driver.mass, fuel.mass]) total_weight = sum([car.weight, driver.weight, fuel.weight]) print('총 질량 (무게):\t{:.1f} kg ({:.1f} N)'.format(total_mass, total_weight)) center_gravity = sum( [car.first_moment, driver.first_moment, fuel.first_moment], Vector()) center_gravity /= total_mass print('질량 중심 (x, y):\t{}'.format(center_gravity)) model = {} model['car'] = RectCylinder(car, 1.80, 4.70) model['driver'] = RectCylinder(driver, 0.5, 0.9) model['fuel'] = RectCylinder(fuel, 0.9, 0.5) total_moment_inertia = Decimal(0) for name, model in model.items(): mass_element_inertia = model.moment_inertia(center_gravity) total_moment_inertia += mass_element_inertia print('\t{}: {:.1f} '.format(name, mass_element_inertia)) print('관성 모멘트:\t{:.2f}'.format(total_moment_inertia))
def isContact(self, position): xx = self.coords[0] yy = self.coords[1] zz = self.coords[2] projectile = Vector(*position) xin = True if xx[0] <= projectile.x <= xx[1] else False yin = True if yy[0] <= projectile.y <= yy[1] else False zin = True if zz[0] <= projectile.z <= zz[1] else False return xin and yin and zin
def main(): elements = [Body(1, (1, 2, 3)), Body(2, (2, -3, 4))] for point_mass in elements: print(point_mass) print('weight: {}'.format(point_mass.weight)) total_mass = Decimal(0) for point_mass in elements: total_mass += point_mass.mass first_moment = sum([body.first_moment for body in elements], Vector()) combined_center = first_moment / total_mass print('Combined Center of Gravity: {}'.format(combined_center))
def solve(self, question, element): if self.given_object == "vector": vectors_data = self.find_given_values(question) vectors = [] for _, values in vectors_data.iteritems(): vectors.append(Vector(dict=values)) if self.requested_value == "angle": return "%d degrees" % int(round(math.degrees(vectors[0].theta))) elif self.requested_value == "r": return "%d units" % int(round(vectors[0].r)) elif self.requested_value == "x": return "%d units" % int(round(vectors[0].x)) elif self.requested_value == "y": return "%d units" % int(round(vectors[0].y))
def __init__(self, *, position, velocity, range, damage, **kwargs): radius = kwargs.get("radius", damage_to_radius(damage)) self.body = KinematicBody(collider=CircleCollider, radius=radius, position=position, velocity=velocity, disable_collide=True) self.sprite = CircleSprite(colors.BLUE, radius) self.layer = layers.PLAYER_TEARS self.speed = kwargs.get("speed", Vector(*velocity).magnitude()) self.remaining_range = range self.damage = damage self.on_update += check_range self.on_collide += kill_on_obstacle self.on_collide += damage_enemies
def populate(self, neighbors): template = choice(templates[self.type]) if "neighbors" in template and not all( available or not actual for available, actual in zip( template["neighbors"], neighbors)): return False for obj in template["objects"]: self.objects.append(types[obj["type"]]( position=Vector(*obj["position"]) * TILE_SIZE + (TILE_SIZE / 2, TILE_SIZE / 2), **obj.get("params", {}))) self.door_directions = [] for direction in MapGenerator.directions: self.objects.append(Wall(direction)) if neighbors[direction]: self.door_directions.append(direction) self.objects.append(Door(direction)) return True
def move(self, delta_time): movement_axes = 0 move_force = Vector(0, 0) if self.pressed_keys['w']: movement_axes += 1 move_force += (0, -self.speed) elif self.pressed_keys['s']: movement_axes += 1 move_force += (0, self.speed) if self.pressed_keys['a']: movement_axes += 1 move_force += (-self.speed, 0) elif self.pressed_keys['d']: movement_axes += 1 move_force += (self.speed, 0) if movement_axes == 2: move_force /= SQRT_2 if movement_axes: self.body.add_force(move_force) self.on_move.dispatch(self, delta_time)
def __init__(self): self.paddle0 = Paddle(0) self.paddle1 = Paddle(1) self.ball = Ball(Vector(0, 0, -1000), Vector(-2, 1, 35), Vector(0, 0, 0), 50)
def test_neg(self, vectors): v1, *_ = vectors assert -v1 == Vector(-1,-2,-3)
def test_pow(self, vectors): v, *_ = vectors assert v**2 == Vector(v.x**2, v.y**2, v.z**2) assert (-v)**2 == Vector(v.x**2, v.y**2, v.z**2)
def test_add(self, vectors): v1, v2 = vectors assert v1 + v2 == Vector(3, 5, 7)
def __init__(self, v0, angle, position): self.v0 = v0 self.angle = Angle(*angle) self.position = Vector(*position)
def __init__(self, ship, config): self.throttle = 0.0 self.thrustVector = Vector() super().__init__(ship, config)
def vectors(self): v1 = Vector(1,2,3) v2 = Vector(2,3,4) return v1, v2
def test_mul(self, vectors): v1, v2 = vectors assert v1 * v2 == Vector(2,6,12) assert v1 * 2 == Vector(2,4,6) assert 2 * v1 == Vector(2,4,6)
def teset_sub(self, vectors): v1, v2 = vectors assert v1 - v1 == Vector()
def room_center(self): return Vector(self.width / 2, self.height / 2)
def test_div(self, vectors): v, *_ = vectors assert Vector(2,4,6) / 2 == v with pytest.raises(TypeError): 2 / Vector(2,4,6)
def test_mul(self): v1 = Vector(3, 4, 5) self.assertEqual(Vector(6, 8, 10), v1 * 2)
def __init__(self, name, frm, to, *, color): self._frm = Vector(*frm) self._to = Vector(*to) self._color = color super().__init__(name, position=None, image=self.get_image())
def __init__(self, measurement, position): self.measurement = Measurement(*measurement) self.position = Vector(*position) self._get_coordinates()
def test_sub(self): v1 = Vector(3, 3, 3) v2 = Vector(1, 1, 1) self.assertEqual(Vector(2, 2, 2), v1 - v2)
while not populated: populated = room.populate(self.get_surrounding(room.position)) def generate_map(self, amount, deadends): self.add_rooms(amount - deadends) self.add_deadends(deadends) self.populate_rooms() if __name__ == "__main__": mg = MapGenerator() mg.generate_map(20, 5) img = np.full((25, 25, 3), 0) for r in mg.rooms: index = (r.position[1] + 13, r.position[0] + 13) if type(r) is Room: img[index] = (255, 255, 255) if type(r) is DeadendRoom: img[index] = (255, 255, 0) plt.imshow(img) plt.show() player_room_positions = [ Door.positions[direction] + Vector(*MapGenerator.neighbor_offsets[mirror[direction]]) * int(TILE_SIZE * 1.5) for direction in MapGenerator.directions ]