In this project, we continue the development of our Alien Intruders game by initializing and drawing our ship at the correct location.

The hardware setup for this project should be the same setup that was completed for the project. Here's a reminder:

Now that we have the framebuffer initialized and have verified that the LED Matrix is working, let's put our spaceship where it's supposed to be located — in the middle of the LED Matrix horizontally, and in the bottom row.

To determine the middle of the LED Matrix, we could take the known width (8 dots) and divide that by two — that would give us our middle dot (x=4). But, what if you later want to use this code on a smaller or larger LED Matrix display? For example, let's say you put four LED Matrices together to build a 16x16 display. If you put x=4 right into your code, when you run the program on your new 16x16 display, your spaceship will no longer be in the middle of the display.

Luckily, we have a way to handle this. If you remember back to our `FrameBuffer` concept, we have an attribute called `fb.width` that will determine how many LED Matrices you have chained together and will tell you the total width of your LED Matrix display. We can use that attribute to determine the actual width of the display, and then divide that actual width by 2 to get the starting position of our spaceship.

Here is what the initialization code for the initial position of the ship would look like:

# Initialize the spaceship spaceship_position = fb.width / 2

In our previous project, we just drew the ship at (3,0). But, now we have the actual X coordinate from our calculation above, so we can draw the ship at its actual starting position.

But, there's one thing we need to keep in mind — now that we're using a calculation to determine the position of the ship, it's possible that the result of our calculation won't be a whole number (for example, if we were using an LED Matrix that was 7 dots wide, our initial position would be x=3.5). To accommodate for that, we need to round our calculation to the nearest whole number, and that is where we will display our ship.

We do that using the built-in math function called `round()`, which will round the value we give it to the nearest whole number.

Here is what the code for this project should look like at this point (with new changes highlighted with arrows):

# ################################ # Initialize the game environment # ################################ # Import required functions/modules from rstem.led_matrix import FrameBuffer *import time # Initialize framebuffer fb = FrameBuffer() # Initialize accelerometer # Initialize spaceship *spaceship_position = fb.width / 2 # Initialize aliens # Initialize missiles while True: # ################################ # Check for user input # ################################ # Get button input # Get accelerometer input # ################################ # Update the game world # ################################ # Update the location of any missiles # Update the location of the spaceship # Update the location of the aliens # Check for any missile collisions # ################################ # Draw graphics on the screen # ################################ fb.erase() # Draw the spaceship * fb.point(round(spaceship_position), 0) # Draw the aliens # Draw any missiles fb.show() # Wait briefly time.sleep(0.001) # ################################ # Finish and cleanup # ################################ # Print "You won!" if the player wins # Print "You lost..." if the player loses

1. If you don't use `round()` as described above, does the code still work? Why or why not?

2. Start the spaceship at the right edge of the screen, instead of in the center.