Python Asteroids on GitHub

Just for fun, I add a cheat code. Might not be my last one.

I need help in this game. And since it’s my game, I’m putting in a cheat code. If you type “s”, player zero gets a free ship. (Because there’s no reason why I would ever play as player one.)

class Ship(Flyer):
    def control_motion(self, delta_time, fleets):
        if not pygame.get_init():
            return
        keys = pygame.key.get_pressed()
        if keys[pygame.K_s]:
            if self._allow_freebie and self._shipmaker:
                self._shipmaker.add_ship(u.PLAYER_ZERO)
                self._allow_freebie = False
        else:
            self._allow_freebie = True
        if keys[pygame.K_f]:
            self.turn_left(delta_time)
        if keys[pygame.K_d]:
            self.turn_right(delta_time)
        if keys[pygame.K_j]:
            self.power_on(delta_time)
        else:
            self.power_off()
        if keys[pygame.K_SPACE]:
            self._hyperspace_generator.press_button(self._asteroid_tally, fleets)
        else:
            self._hyperspace_generator.lift_button()

The toggle _allow_freebie keeps it from adding multiple ships as long as you hold your finger down. I’ll leave it to you to guess how I figured out that I needed that. The Ship now captures the ShipMaker as usual:

    def interact_with_shipmaker(self, shipmaker, fleets):
        self._shipmaker = shipmaker

I first patched this in by adding a score of u.FREE_SHIP_SCORE, but that had the undesirable property of causing the Saucer to go small and deadly. So now you get the ship but the score doesn’t go up. I tried putting in a positive and negative score but that worked only intermittently for some reason. Anyway, this is “better”.

Yes, I am committing this code: Cheat code “s”.

I also noticed that missiles can pass right through the Saucer sometimes. I think its radius is a bit too small.

Drawing some circles around the saucer I find that a decent radius for the width would be 35 or 36 but the height is really about 26. An ellipse of “radius” 36x26 covers the Saucer nicely.

I suppose we could change the hit box to be an ellipse. Or a box, which would be less than ideal, because then you’d get hits through the corners that you don’t deserve.

The ship has a similar issue in that it is longer than it is wide.

I like the circle because you just check whether the distance between centers is less than the sum of the radii. You can probably get the radius of an ellipse at a given angle, but that seems unnecessarily complicated. I’ll think about it. For now, the free ship hack will suffice.

See you next time!