Android Game 3 Java Programming tutorial


Hello, I’m Edu for Edu4java and this is tutorial number 3 of Android game programming in this tutorial we are going to see the concept of game loop and a bit of animation techniques what is game loop? loop is something which repeates itself ehh what we are going to do in the game loop is mainly two principal activities one is the so called physics update which updates the physics and the other one would be the drawing as we said before a video is made up of a lot of images which are similar and when we see them one after the other one in a high frequency we think it is a video or an animation inside this loop we are going to print different screens and it is going to look us if it were an animation or a film this would be the drawing inside the game loop, the other important activities would be the update of the physics for example; if we have a character in our screen and it walks to the right with a 1 pixel speed… 10 pixel per second each print each new print has to update it’s position to be able to print it where we want this would be one of the activities developed in the update ehhh the update of the score is another of these activities imagine if you are playing a game which takes points of your score as you play this would also be done here ok, let’s do our game loop our game loop is going to receive in another thread let’s create a thread for this loop for this game loop so that it’s independent emmm we are going to do this with this class here we are going to make a class GameLoopThread we are going to put inside this code it’s a bit untidy ok, we have a view which we are going to pass as a parameter when we build this object Game thread we are going to build the object in the view, so we are going to pass a this as a parameter it also has a variable running, while it is true it is going to run the loop. It is going to run one time and another while this variable is true in the run method the while is going to repeat until running is true. The way of making this loop stop is to set run to false, then in the next round it would stop in this version here of the game loop mmm we have put the painting of the canvas; the draw here we see we have get the canvas doing the lock. We do a view.onDraw and lastly we do unlock we repeat this one time and another ok, now what we need is ehh make the declaration of a field in the SurfaceView we add a new field we do the creation we create the Thread object and now we start it now instead of calling it in the SurfaceCreated we setRunning to true and directly we call start and start is going to make the execution in another thread of the method run and it is going to start going around and the method onDraw, here is going to be called once and again we are needing the imports … I didn’t save the… the… class ok, if everything is ok this should run it is not going to be very interesting because what we are going to see that even if the loops is running once and again it does it the same place so there is really nothing to see we should try to make this a bit more lively for this we are going to create a variable x in the view which is going to gives us the position x for our printing of the icon here and here we are going to write an x shift+ctrl+f tidies up a bit ok, let’s try here we can see if you watch carefully you can notice the speed is not constant, it increases and decreases this is because … the loop goes around as quickly as possible this depends on the CPU given to the process how much execution time it has and this time is variable because of this when it has a lot of time it goes quicker and when there is more process running at the same time it goes slower. This is not good We can solve this limiting how many frames per second we are going to print frames per second… let’s put 10, this means that a maximum of 10 frames are going to be printed it is going to go around 10 times maximum in a second if there is time for more we are going to slow it using the instruction sleep let’s make it sleep as much as… … … the loop has a certain time to execute itself, if it goes very quickly we are going to stop it This is what we are going to do. This are the frame per second, in our case 10, if we divide it by 1000 it would be 1 second this is going to result in ticksPS the ticksPs are going to give us 100, what does this means? 100 is the minumum which each loop has to last then, at the beginning of the loop we take the time in milliseconds we make all the process and when we finish we see what is the difference between the present time and when it started to the total time we discount the difference we just calculated this is the time we have to sleep if the sleeping time is>than 0 we sleep, if not, this means that we took more time than the frames we asked for, for than 100 ms anyway we send a small sleep of 10ms so that our application doesn’t need a lot of CPU and in this way it leaves CPU for other applications ok, let’s run it and if you watch carefully now you can see that there is no increase or decrease in the speed the last thing would be to finish this small tutorial … to manage the speed variable in the view let’s add a speed field and let’s make it bounce against the borders instead of this simple if ok the only thing I did here is add a speed field initialized with a one which is the increment that we are going to do. This means that when it gets to the right border. This border is the length of the screen minus the length of the bmp if it get’s to that value it goes in the other direction and if get’s to zero it goes back in the other direction here we have the increment, instead of a constant we have this variable and everything else the same let’s see if it works don’t worry, this was a cut I made so that you don’t have to wait and as you see here it bounces in the right border and after this other cut we can see how it get’s to the left border and bounces back ok, with this we finish the tutorial number 3 of Android game programming. See you in the next one!!

28 thoughts on “Android Game 3 Java Programming tutorial

  1. You are amazing tutor! This is exactly what I've been looking for! I really appreciate posting your code when you explain your tutorials. Keep it up and thank you so much!

  2. Good videos but I don't like the copying and pasting. I feel the viewer gets a little less with it since they are always catching up instead of following.

  3. your a really good teacher, please make more tutorials, and avoid copying and pasting. When you copy and paste, its slightly hard to follow.

  4. This is a very good tutorial series. Thank you very much! I don't mind the copy paste either, I can still follow.

  5. Thanks, great tutorials. I was getting nullPointerException with the code as is, I *think* I fixed it by putting an 'if (c != null) {}' around the synchronized view.onDraw call. Under certain circumstances the lock returned a null it looks like.

  6. @Bence Sárosi Enlighten us with few. This is just the second one in the series. So what is the bad practice, how could it be done better?

  7. I am using Android Studio, and cut and paste your code from the home page, and sadly its not working. Android Studio even warns that onDraw should not be called this way. I got a blank screen, though your first tutorial with simple View override worked perfectly.

Leave a Reply

Your email address will not be published. Required fields are marked *