def display_acceleration(self): """Display the exhaust if accelerating.""" x1, y1 = rotate_around_origin( (0, constants.SHIP_ACCELERATION_POINTS[0]), self.direction) x2, y2 = rotate_around_origin( (0, constants.SHIP_ACCELERATION_POINTS[1]), self.direction) pyxel.line( x1=x1 + self.x, y1=y1 + self.y, x2=x2 + self.x, y2=y2 + self.y, col=constants.SHIP_ACCELERATION_COLOUR, )
def __init__( self, size=constants.ASTERPOD_INITIAL_SIZE, radius=constants.ASTEROID_RADIUS, position=None, ): """Initialise the asteroid, including the position, size, and points. By default, the asteroid is the largest size and randomly placed, but this is overriden for smaller asteroids.""" self.colour = constants.ASTEROID_COLOUR self.size = size self.radius = radius self.init_position(position) self.direction = random.random() * math.pi * 2 self.velocity = rotate_around_origin((0, -constants.ASTEROID_VELOCITY), self.direction) self.spin_direction = random.choice((-1, 1)) asteroid_points = random.choice(constants.ASTEROID_SHAPES) scale = radius / constants.ASTEROID_RADIUS self.points = [] for x, y in asteroid_points: point_new = Point(x * scale, y * scale) point_new.rotate_point(self.direction) self.points.append(point_new) Asteroid.asteroids.append(self)
def random_velocity(): """Helper function to determine a random velocity.""" direction = random.random() * math.pi * 2 velocity = rotate_around_origin( (0, -constants.SHIP_DRIFT_VELOCITY), direction ) return velocity
def shoot(self): """Create a bullet based on the ship's direction and position.""" vel_x, vel_y = rotate_around_origin((0, -constants.BULLET_VELOCITY), self.direction) ship_tip = self.points[0] Bullet( self.points[0].x + self.x, self.points[0].y + self.y, vel_x, vel_y, constants.BULLET_COLOUR, )
def accelerate(self): """Increase the velocity (to a maximum) of the ship based on user input.""" self.accelerating = True acc_x, acc_y = rotate_around_origin((0, -constants.ACCELERATION), self.direction) self.momentum_x += acc_x self.momentum_y += acc_y acceleration = math.hypot(self.momentum_x, self.momentum_y) if acceleration > constants.MAX_ACCELERATION: scale = constants.MAX_ACCELERATION / acceleration self.momentum_x *= scale self.momentum_y *= scale assert (round(math.hypot(self.momentum_x, self.momentum_y), 0) == constants.MAX_ACCELERATION)