Learn PureBasic by example. Follow the development step by step of a simple 2D game : the snake. This famous game lets you control a snake on a screen where apples appear at random. When the snake eats an apple, it grows in size. The goal is to survive the longest.
In this long episode, I start by adding two new states and their corresponding screens (start and gameover). Then, I compute and display the score in a scoreboard using a font bitmap.
For the new states, I want the game to start in the "start" state. Then, when the user hits spacebar, it starts the game. When the player loses or wins, it goes to the "gameover" state. In this state, if any key is hit after a one second period, it goes back to the "start" state.
I also wanted a separate screen for each state. So, a start screen and a gameover screen. And finally, I wanted that each screen had its own music.
For the score, I keep a global variable that is initialized at 0 and incremented by one every time the snake eats an apple.
To display the score, I draw a scoreboard in the top-left corner of the playing screen and I display the score within it using a font bitmap (loaded as a sprite) that is clipped to the wanted character for each digit of the score. In the scoreboard, the score is displayed aligned to the right.
In the gameover screen, I also display the score so that the player, who is not supposed to look at the scoreboard often, knows his score when he loses (or wins). In the gameover screen, the score is aligned on the left as it must flow with existing text.
And voila!
I do not distribute the sound files for copyright reasons. But you can find sound effects at http://freesound.org
To convert files to OGG format, you can go to https://convertio.co
Download the code from this episode at https://bit.ly/2ZF9fvr
Download PureBasic at http://www.purebasic.com/pp
Pure Programming blog
https://pure-programming.com
Facebook
/ pureprogramming
Follow me on Instagram at @pure.programming
Support the channel with some Merchandising
EU : shop.spreadshirt.net/pure-programming
US : shop.spreadshirt.com/pure-programming
Photo by Kristopher Roller on Unsplash
Pause/Resume icons made by Freepik from www.flaticon.com