Category Archives: Uncategorized

Shout out to a friend of mine who writes Android games

Hi,

 

I wanted to post a quick link to a great little platform game written by a friend of mine.

 

It’s written using B4A and Libgdx and is a great example of what can be done. the first version of the game was written in only three weeks!

Download it here:

https://play.google.com/store/apps/details?id=www.sagital.pknight

Advertisements

Support this site by purchasing B4X products!

We’re planning a number of new programming series on how to clone popular mobile games for Android using the B4A language.

And what would really help is if you’re thinking of purchasing B4A, please use the following link to visit the Official B4X site.

B4X Official Site – www.b4x.com

For every purchase made here, EasyAndroidcoding.com will receive a small amount of the sale and help give us the means and motivation to continue producing quality Basic4Android games programming articles and YouTube videos.

 

So what are you waiting for? If you’re on the edge, go for it! What do you have to lose? B4X offer a full 30 day money back guarantee!

Space Invaders clone – Download the source code now!

I’ve written a Space Invaders clone using the LibGDX library and Basic4Android.

Download it now from the Google Play store:

https://play.google.com/store/apps/details?id=uk.co.coffeeinducedgames.invaders

 

Invaders - screenshot thumbnail

Download the source code here! : 

https://dl.dropboxusercontent.com/u/8673694/my%20games/Invaders-source.zip

 

The source code link will be updated as the game is developed further

Write a Flappy Bird Clone using Basic4Android and LibGDX – Part 6

Part 6 – Collision detection and restarting the game

Download the source code here!

https://dl.dropboxusercontent.com/u/8673694/you%20tube%20files/basic4android/CloneyBirdTutorial/Part6.zip

Now we have our bird on the screen, it’s responding when we tap the screen, flapping its wings, but passes straight through every pipe, and even the ground. Let’s change that.

We know the size of our pipes, we know the size of the gap in the pipe, so we can calculate whether the bird is in that gap or not. The first thing to check is whether the X position of the bird overlaps a pipe. We then check the Y position to see if it’s in the gap or not, if it is then we can give it a point, if it’s not in the gap, then it’s crashed into the pipe and we kill it, game over.

Before we do this, we’ll need to add some variables in our globals section. We use gamerunning to store whether game is running or not, we use this later for showing a title screen before we start the game. We then declare birddead to check if the bird is living or dead, a restartCD variable to act as a countdown timer to stop the game restarting straight away after the bird dies. Finally we add an integer to store the score value.

Dim gamerunning As Boolean = True

Dim birddead As Boolean = False

Dim restartCD As Int = 60

Dim score As Int = 0   

Moving onto the LG_Render section of code, add the following code in the update pipes loop, after the pipe.x=pipe.x-speed line but before the if pipe.x < -100 line.

‘check for collision

‘if bird x coord is between left and right side of pipe

If (myBird.x+vpW*0.085+(myBird.width*scaleX) > Pipe.x) AND (myBird.x < (Pipe.x+vpW*0.125)) Then

‘ if bird y coor is not in the gap in the pipe

If (myBird.y < Pipe.y+(img_pipe.Height*scaleY*0.4375)) OR (myBird.y+vpH*0.04 > Pipe.y+((img_pipe.Height*0.5625)*scaleY)) Then

            ‘if birds not dead then kill it

            Ifbirddead = FalseThen

                  ‘set bird y movement to zero

                  myBird.my = 0

           End If

            ‘set bird dead to True

            birddead = True

‘set game speed to zero to stop screen scrolling

            speed = 0

Else

‘else increment score by one if bird passes pipe

            If Pipe.scored = False Then

                  ‘set pipe scored to true to only a one to score

 Pipe.scored = True

                  ‘increment score

                  score = score + 1

            End If

End If

End If ‘END COLLISION CHECK

 

Let’s look through this and try to make it simple. The first IF is checking the x position of the bird against the pipe, the pipe.x value is the left side of the pipe, so pipe.x + 12.5% of the viewport is the right side of the pipe. We then check if the bird is in the gap or not, using scaleY and the percentage of the height of the pipe the gap appears, we can calculate where the gap is on the screen and compare it to the birds Y position. If the bird is not in the gap then the first block of code is ran, otherwise the block after the else is ran. If the bird hits the pipe we set the bird.my to zero, we also set the birddead Boolean to true and the speed to zero to stop the bird moving to the right.

If the bird doesn’t hit the pipe, then we set the scored boolean value of the pipe to true and increment the score.

Now we have our collision detection code in place, give the game a go, check that you can crash into the left side of the pipe, the bottom of the gap, and the top of the gap in the pipes.

 

We’re now going to stop the bird going off the bottom of the screen to simulate crashing into the ground, add the following code after the pipes update loop:

‘stop bird going off bottom of viewport

If myBird.y < vpH*0.20 Then ‘if bird lower than 20% of viewport hieght

speed = 0 ‘set speed to zero to stop scrolling

birddead = True’kill bird

myBird.my = 0 ‘set bird my speed to zero

myBird.y = vpH*0.20 ‘set bird y coord to 20% of viewport height

End If

We check the bird.y value, if it goes lower than 20% off the bottom of the viewport then we stop the bird and kill it. We set the speed to zero, birddead to zero, mybird.my to zero and myBird.y to 20% up the viewport.

Try it out, you should find your bird hits a pipe and falls to the ground, or just stops if it hits the ground. We now have an issue however as we have to restart the game by quitting with the back button and re-opening the app.

Before we do that, we need to decrease out restartCD value for each frame, add the following lines to the end of the LG_Render method:

‘decrease countdown timer if bird is dead

If birddead = True Then

restartCD = restartCD – 1 ‘decrease restartCD

End If

So let’s add code to let us restart the game. We’ll add this in the IP_TouchDown method. Replace the current code in there with the following:

If gamerunning = True Then ‘if game running

            Ifbirddead = FalseThen’ and bird alive

                  myBird.my = 4*scaleY ‘flap wings         

            ElseIfbirddead = TrueANDrestartCD < 0Then

                  ‘restart game bird

birddead = False ‘revive bird

                  pipes.Clear ‘remove all pipes

                  speed = 3’reset speed

                  myBird.Y = vpH*0.5′ set bird y coord to restart

                  myBird.my = 4*scaleY’set starting flap

                  restartCD = 60’set restart countdown

                  pipeCD = 100’set first pipe countdown

                  birdColour = Rnd(0,4) ‘ random bird colour

                  score = 0 ‘reset score

            End If

      Else

            ‘start game

            gamerunning = True’ set game to running state

            myBird.my = 4*scaleY’ alter my to make bird flap

      End If

 

This basically means that if the game is running and the bird is dead then make the bird flap it’s wings, or, if the bird’s dead and the countdown timer is less than zero then reset all the variables. Pipes.clear deletes all the pipes in the list. This causes the game to restart as the main loop speed is set back to a number higher than zero and the bird position is reset, we even randomise the colour of the bird!

The bit at the bottom just starts the game running after the title screen is shown, this is the title screen we haven’t added yet. That comes in the next tutorial.

 

Basic4Android Library Spotlight – Accelerated Surfaces

I’ve just uploaded a new video onto my Youtube channel showing off a new library I found recently on the Basic4Android.com forums. It’s called accelerated surfaces and it look really useful for getting access to OpenGL accelerated graphics using simple commands.

Here’s the forum URL: http://www.basic4ppc.com/android/forum/threads/lib-accelerated-surface.27462/#content

The library is by the user “Informatix”

In his own words:

“This library provides a dedicated drawing surface embedded inside a view, which benefits from the hardware acceleration. With it, you get the speed of OpenGL for 2D without the complexity.
It includes many Canvas methods (with anti-aliasing, matrix and camera) and a few useful methods for Bitmaps and Drawables (AlterColors, Crop, LoadNinePatch, ReduceColors, SetDensity, etc.). You can import the Matrix, Camera, Paint and Path objects from another library (if they are not wrapped).”

 

It’s recommended to use it with Android version 3 and up to get the full benefit from it. It will work on 2.1 – 2.3 devices, but you won’t get hardware acceleration.

 

Accelerated Surfaces

I’ve added a picture here showing it running on three different devices. If you look closely you’ll see the Galaxy S2 with the 120 Androids falling down the screen is running at over 50 frames per second. Which is great!