Finished part6-breakout write-up

This commit is contained in:
Adam Greenwood-Byrne 2020-07-16 11:42:46 +01:00
parent ba27d5ee26
commit fa6e06a413
3 changed files with 35 additions and 1 deletions

View file

@ -115,3 +115,37 @@ Keyboard input
We'll be using the UART to take input, just like we did in part4-miniuart. We'll be using the UART to take input, just like we did in part4-miniuart.
`getUart()` simply checks if a key has been pressed and, if so, it returns the character, otherwise 0. We don't want this function to wait for a key, because gameplay needs to continue regardless. `getUart()` simply checks if a key has been pressed and, if so, it returns the character, otherwise 0. We don't want this function to wait for a key, because gameplay needs to continue regardless.
Animating the gameplay
----------------------
Let's look at the new `main()` routine which controls the gameplay. Most of the initialisation code should be familiar to you, and the endgame of "Game over" or "Well done!" should also be clear. I'll therefore focus on the function of the inner loop.
First, we check if we need to move the paddle. We'll move it left if we get a keypress of "h", and right if we get a keypress of "l". I would use the arrow keys normally, but they're a little harder to capture over the UART so we'll keep it simple for now.
We now implement another function `moveObject(object, xoff, yoff)` to make these moves. This starts by calling another routine I've implemented in _fb.c_ to move a rectangular bitmap from one screen location to another, leaving only the background colour behind. It's a sloppy implementation, but it will do for now.
With our paddle logic in place, let's deal with the ball. We've set some initial velocities but, before we actually move the ball, we need to check for collisions. If we're about to hit a brick, we:
* remove that brick using `removeObject`
* reverse our vertical direction
* increment the score
* redraw the scoreboard
If we're about to hit the paddle, we:
* reverse our vertical direction
* make any necessary changes to our horizontal direction (in case we hit the side)
We can then actually move our ball after a slight delay so gameplay isn't too fast! The delay code is implemented in _fb.c_ and uses the on-board timer on the ARM.
Finally, we just need to make sure we ball is in the game arena. We bounce it off the sides if we need to, but if it escapes the bottom of the screen then we lose a life and reset the paddle & ball. This is easy enough since we can just remove them and create new ones!
Conclusion
----------
I hope writing your first game was easier than you thought it might be - and on bare metal too. It's pretty simple, granted, but once you've built it and got it going on your RPi4, I bet you'll be just a little bit addicted. I know I am!
_Well done, you've just written your first game!_
![The finished game](images/6-breakout-thefinishedgame.jpg)

Binary file not shown.

After

Width:  |  Height:  |  Size: 523 KiB

View file

@ -204,7 +204,7 @@ void main()
} }
} }
wait_msec(4000); // Wait for a tenth of a second wait_msec(4000); // Wait a little...
moveObject(ball, velocity_x, velocity_y); moveObject(ball, velocity_x, velocity_y);
// Check we're in the game arena still // Check we're in the game arena still