Python 032 - Ship Count
Let’s see about displaying the available ship count, little ships in a row.
I have a little time and need to de-stress. Stuff going on here. Cat may be sick and other matters. Code calms me. I hope.
The story is to display one ship per available ships, underneath the score. Should be “easy”. Since it’s a display issue, I’ll not be writing a test. It seems we should do it here:
def draw_everything():
screen.fill("midnightblue")
for ship in ships:
ship.draw(screen)
for asteroid in asteroids:
asteroid.draw(screen)
for missile in missiles:
missile.draw(screen)
draw_score()
def draw_score():
score_surface, score_rect = render_score()
screen.blit(score_surface, score_rect)
Since draw_score
looks complete, let’s just add draw_available_ships
.
I just typed this in:
def draw_available_ships():
ship = Ship(Vector2(100, 100))
ship.angle = 90
for i in range(0, ships_remaining):
draw_available_ship(i, ship)
def draw_available_ship(ship_number, ship):
ship.position += Vector2(30, 0)
ship.draw(screen)
The idea is to create a phantom ship, aim it upward, and draw it, stepping along each time. It looks like this:
That’s nearly good. I think we should space them a little further apart and start them near the left margin, probably just a bit right of where the score displays.
def draw_available_ships():
ship = Ship(Vector2(20, 100))
ship.angle = 90
for i in range(0, ships_remaining):
draw_available_ship(i, ship)
def draw_available_ship(ship_number, ship):
ship.position += Vector2(35, 0)
ship.draw(screen)
This will do for now. One might want to scale the free ships down a bit, could adjust the positioning better. But the code seems pretty reasonable to me and even reuses the regular ship draw
.
I thought briefly of saving and restoring the real ship’s position, but that seemed unnecessary. Here, we certainly could create the available ship once and for all. But let’s save that idea until we move the main loop into an object.
I’m calling this good enough for now. Commit: Available ships display below score.
Summary
Pretty simple, really. We should clean up the magic numbers, maybe scale the available ships down a bit, maybe tune up the positioning. We’ll leave the latter two concerns to the product owner, and the former is now in my keyboard tray Jira.
See you next time!