So we have our background, our pipes, our ground scrolling, our bird and the score. We have collision detection in place and the game restarts after you die. So what’s left to do before we can call this a real game? Let’s just use this list: High-score, Title screen, sound effects.
Get the source code here!
We have a score, so we’ll add the high-score first.
In the globals section, we’ll add a variable for the high-score value and then a file object for the score file.
Dim highscore As Int
Dim scorefile As RandomAccessFile
We need to add a new library here too, in the libs tab on your B4A IDE, tick the box next to RandomAccessFile, this gives us the file commands we need to handle the high-score.
Moving to the Activity_Create section add the following to handle the high-score file:
‘create highscore file
If File.Exists(File.DirInternal,”scores.dat”) Then ‘only open if file exists
scorefile.Initialize(File.DirInternal,”scores.dat”,True) ‘ open file
highscore = scorefile.ReadInt(0) ‘read score from file into highscore variable
scorefile.Close ‘ close file after use
highscore = 0 ‘ set high-score to zero if first run
So if the high-score file exists, then B4A will open it and read the integer value at position zero from it into the high-score variable. Otherwise, just set the high-score to zero if no file is found, this will happen the first time the game is played.
At the end of the LG_Render section, we’ll now add the code to write the high-score to the screen if the bird is dead. It will also display the message to flap to restart when the restartCD value drops below zero.
If birddead = False Then
‘if bird alive then just draw score
shadowtext(“”&score,vpW*0.50,vpH*0.90) ‘ draw score on screen
Else ‘otherwise if bird dead
restartCD = restartCD – 1 ‘decrease restartCD
‘write high-score file if new high-score
If score > highscore Then ‘if score higher than high score
highscore = score ‘set high-score to score
‘write high-score to file
scorefile.writeInt(high-score,0) ‘write score to file
scorefile.Close ‘close score file
shadowtext(“Score: ” & score,vpW*0.50,vpH*0.80) ‘write score to screen
shadowtext(“High Score: ” & highscore,vpW*0.50,vpH*0.70) ‘write high score to screen
If restartCD < 0 Then ‘if restart timer is less than zero
shadowtext(“Flap to Restart”,vpW*0.5,vpH*0.60) ‘write flap to restart text to screen
To explain this in sections, first, if the bird is alive, then just draw the score to the screen, otherwise, decrease the restartCD countdown, then check if the score is a new high-score, if it is, then save the high-score to the score file, then draw the score to the screen along with the high-score. Then if the restartCD countdown is less than zero then display the “Flap to restart” message on screen. This code should go in place of the command you had that just wrote the score to the screen.
Test it out, you should now see a screen that looks like this when you die:
Let’s add some sound:
Simple sound effects are all we need for this game. I’ve used some simple sound effects from a game I made several years ago. I’ve included these in the source code download with this tutorial. If you want to make your own, I’d advise using a simple tool like SFXR. Google “SFXR download” to find it. It’s a simple sound effect generator which is easy to use and has actually been used for commercial games such as VVVVVV which is available on Steam.
To add sounds, we need to define an object for each sound effect, these objects are of type LGSound, add the following lines to the globals section to add an effect for flapping wings and one for dying
Dim snd_hit As lgSound
Dim snd_flap As lgSound
The next step is to point these objects at the sound files, in the LG_Create section, add the following:
snd_hit = lGdx.Audio.NewSound(“shoot.wav”) ‘ load bird hitting object sound
snd_flap = lGdx.Audio.NewSound(“select.wav”) ‘ load flapping sound
These files need to be in your files folder. We’re now ready to use them. Find the section in the LG_Render where the bird hits the ground, insert these commands just before the line that sets birddead to true.
If birddead = False Then
We put this just before the line that sets birddead to true as it means it will only play the sound effect once.
We then add the sound effect for flapping the birds wings. Add this to the IP_Touchdown method just under the line that alters my
so the top of the If statement in the IP_TouchDown method should now read:
If birddead = False Then ‘ and bird alive
myBird.my = 4*scaleY ‘flap wings
Else If birddead = True AND restartCD < 0 Then
The last thing to add is the title screen
This can be done in several ways when writing games, my personal approach is to have a variable that says whether the game is running or not, if it is, then run the main LG_Render loop just as we’ve written it. If it’s not, then display a title screen. To do this, add a gamerunning boolean variable in the globals section:
Dim gamerunning As Boolean = False
We set it as false initially as we want the title screen to show first.
At the start of the LG_Render section, add the following code block:
If gamerunning = False Then
‘increment secs variable
frames = frames + speed
‘draw ground tiles
For x = 0 To 6
‘draw each tile over 20%
Batch.DrawTex2(myGD,(x*vpW*0.20)-(frames Mod (vpW*0.20)),0,vpW*0.20,vpH*0.20)
Batch.DrawRegion3(BirdFrames(0,(birdColour*3)+(frames/8 Mod 3)),myBird.x,myBird.y+(10*Sin(frames/10)),vpW*0.0425,vpH*0.02,vpW*0.085,vpH*0.04,1,1,0)
‘draw title text
shadowtext(“CloneyBird”,vpW*0.5,vpH*0.8) ‘ Cloney Bird title
shadowtext(“Tap to start”,vpW*0.5,vpH*0.7) ‘ Tap to start text
shadowtext(“Get Ready!”,vpW*0.5,vpH*0.5) ‘ Get Ready text
‘END TITLE SCREEN —————————————————————————–
‘MAIN GAME LOOP ——————————————————————————-
If gamerunning = True Then
You also need to add an End if at the end of the main game loop, just after the batch.end command but before the end sub statement.
The above section now needs some explaining. Firstly, it only runs if the gamerunning value is false. And it’s sole purpose is to display a title screen with the game game, a background, a scrolling ground and the prompt to tap the screen to start. So we increment our frames variable just as normal, then draw the background, the floor tiles just as in part 2 to make the ground scroll. We then draw the bird. For the Y coordinate of the bird, I wanted to make the bird bob up and down, the best way to do this and make it look natural is think back to trig class in maths at school, and use the Sin function to add this bobbing motion. If you take the sin value of angle between 0 and 360 then you get a smooth change in values going from –1 to 1, as shown below:
Try running the code now and you should have a fully working game, with a highscore, title screen, sound effects and the ability to restart after you die.
Now it’s up to you what to do next. You’ve got the basics for writing a game using LibGDX and Basic4Android. If you’ve used the trial version to follow this tutorial, then I’d strongly advise to go for the full version, it’s really worth it. You’ll get free upgrades during your licence period and full access to the Basic4Android forums where the community is amazingly helpful and welcoming.
Follow the “Purchase Basic4Android” link on this site to claim a 30% discount off the full price if you do decide to buy.
Thanks for following this series. I hope you’ve enjoyed writing what could be your first Android game and that you’ve leant a lot.
Please contact me if you have any requests for further tutorials, or if you want to point out any errors or improvements I could make to this series.