def _spawn_bullet(self): fire_pos = cfg.Vec2(*self.rect.center) + \ cfg.Vec2(self.hit_rect.height, 0).rotate(-self.rot) Bullet(*fire_pos, self.rot, self._type, self._color, self.id, self.groups) MuzzleFlash(*fire_pos, self.rot, self.groups) self._ammo_count -= 1
def handle_mouse(self, mouse_state, mouse_x, mouse_y): aim_vec = cfg.Vec2(mouse_x + self.camera.rect.x, mouse_y + self.camera.rect.y) pointing = aim_vec - self._tank.pos dir = pointing.angle_to(cfg.Vec2(1, 0)) self._tank.rotate_barrel(dir) self._tank.attempt_reload() if mouse_state == InputState.JUST_PRESSED: self.fire()
def __init__(self, x, y, groups): Tank.__init__(self, x, y, LargeTank._IMAGE, groups) self.id = id(self) self.equip_barrel( Barrel("Dark", "standard", self, cfg.Vec2(0, -10), "specialBarrel4.png", groups)) barrel = Barrel("Dark", "standard", self, cfg.Vec2(0, 10), "specialBarrel4.png", groups) barrel.flip(True, False) # barrel.orig_image = barrel.image self.equip_barrel(barrel) self.max_ammo = self._barrels[0].get_ammo_count() self.color = "Dark" self.MAX_ACCELERATION *= LargeTank.ACC_MULTIPLIER
def handle_keys(self, active_bindings): # Reset acceleration if no press self._tank.rot_speed = 0 self._tank.acc = cfg.Vec2(0, 0) for name in active_bindings: # i.e. self._actions["fire"] if self._actions.get(name, None): self._actions[name]()
def __init__(self, x, y, type, style, groups): Barricade.__init__(self, x, y, groups) Damageable.__init__(self, self.hit_rect) groups['damageable'].add(self) self._barrel = SpecialBarrel(self, cfg.Vec2(0, 0), type, style, groups) self.pos = self.rect.center # Bullets spawned by turret won't hurt turret, but hurts other sprites. self.id = id(self) self._barrel.id = self.id
def __init__(self, x, y, dir, type, color, id, groups): self._layer = cfg.ITEM_LAYER SpriteW.__init__(self, x, y, _IMAGES[type][color], (groups['all'], groups['bullets'])) Movable.__init__(self, x, y) self.rotate_image(dir - Bullet.IMAGE_ROT) # Rotatable.rotate_image(self, self.image, dir - Bullet.IMAGE_ROT) self.hit_rect = self.rect self._damage = _STATS[type]["damage"] self.vel = cfg.Vec2(_STATS[type]["speed"], 0).rotate(-dir) self._lifetime = _STATS[type]["lifetime"] self._spawn_timer = Timer() self.id = id
def __init__(self, x, y, color, type, groups): img = f"tankBody_{color}_outline.png" Tank.__init__(self, x, y, img, groups) self.id = id(self) # Create, position, and assign id to barrel. offset = cfg.Vec2(self.hit_rect.height // 3, 0) self.color = color.capitalize() barrel = Barrel(self.color, type, self, offset, _BARREL_IMAGES[self.color][type], groups) # barrel.rect.midtop = self.rect.center barrel.id = self.id self._barrels.append(barrel) self.max_ammo = self._barrels[0].get_ammo_count()
def move(self, collider_groups, dt): # Simulate friction. self.acc -= 4 * self.vel # Effect kinematic equations. self.vel += self.acc * dt if self.vel.length_squared() < 1: self.vel.x = 0 self.vel.y = 0 displacement = cfg.Vec2(0, 0) else: displacement = (self.vel * dt) + (0.5 * self.acc * dt**2) self._handle_colliders(displacement, collider_groups)
def reverse(self): self._tank.acc = cfg.Vec2(-self._tank.MAX_ACCELERATION, 0).rotate(-self._tank.rot)
def forward(self): self._tank.acc = cfg.Vec2(self._tank.MAX_ACCELERATION, 0).rotate(-self._tank.rot)
def update(self, dt): self.rect.center = cfg.Vec2(*self._parent.rect.center) + \ self._offset.rotate(-self.rot)
def __init__(self, x, y): self.pos = cfg.Vec2(x, y) self.vel = cfg.Vec2(0, 0)
def __init__(self, x, y): Movable.__init__(self, x, y) self.acc = cfg.Vec2(0, 0) self._hit_wall = False
def move(self, pct=1): self._sprite.acc = cfg.Vec2(self._sprite.MAX_ACCELERATION * pct, 0).rotate(-self._sprite.rot)
def __init__(self, tank, path_data, target): AIMob.__init__(self, target) self._sprite = tank self._path_points = cycle([cfg.Vec2(p.x, p.y) for p in path_data]) self.set_state(AIPatrolState)