Android is one of the most popular mobile operating systems presently. It uses the most popular programming language, Java, as the primary language for building apps of all types. However, this book is unlike other Android books in that it doesn’t assume that you already have Java proficiency. This new and significantly expanded second edition of Learning Java by Building Android Games shows you how to start building Android games from scratch. The difficulty level will grow steadily as you explore key Java topics, such as variables, loops, methods, object-oriented programming, and design patterns, including code and examples. At each stage, you will put what you’ve learned into practice by developing a game. You will build games such as a simple version of Minesweeper (Sub’ Hunter), Retro Pong, Bullet Hell, and Classic Snake and Scrolling Shooter games. By the end of the book, you will not only have grasped Java and Android but will also have developed six cool games for the Android platform.
- Set up a game development environment in Android Studio
- Implement screen locking, screen rotation, pixel graphics, and play sound effects
- Respond to a player’s touch, and program intelligent enemies who challenge the player in different ways
- Learn game development concepts, such as collision detection, animating sprite sheets, simple tracking and following, AI, parallax backgrounds, and particle explosions
- Animate objects at 60 frames per second (FPS) and manage multiple independent objects using Object-Oriented Programming (OOP)
- Understand the essentials of game programming, such as design patterns, object-oriented programming, Singleton, strategy, and entity-component patterns
- Learn how to use the Android API, including Activity lifecycle, detecting version number, SoundPool API, Paint, Canvas, and Bitmap classes
- Build a side-scrolling shooter using advanced OOP concepts and programming patterns
Hi John
I’ve already finished the books “Learning Java by Building Android Games” and “Android Game Programming by Example”, do you still recommend buying the second edition? In the trailer video I’ve seen new features…
thanks
Hi Don,
Much of the Java tutorials are copy & paste with a few improvements from the 1st edition but all the games are new. Even Snake and Pong which were in the 1st edition are new and improved. The final two games are completely new and are way more advanced than anything that was in the 1st edition. I am really pleased that they were squeezed in because the book starts at zero knowledge and progresses (over 750 pages) to a platform game which will be a big challenge yet achivable to newcomers. I hope this answers you question and thanks for your interest.
John
Hi John,
I’m enjoying your book very much. But I’m having trouble getting started with Android Studio. When I am creating a new project on the “Configure Your Project” screen the “next” button is not available so I’m not able to get to the “Customize Activity” screen in order to rename the activity or change the layout and compatibility options. The “Finish” button is available so that I can create the project but without being able to make those changes. What do you think?
Thanks and keep up the good work,
Richard
I just downloaded 3.3 the book uses 3.2. They have changed the project creation wizard so you see different options at the start of each project. I will try and help.
The main problem with the change if you click through the options at the start is that the starting class and file has a default name and you are not presented with an option to choose the name, ie SubHunter…
The quick answer is that it doesn’t matter if the class is called MainActivity instead of SubHunter. If however you copy and paste code from the download bundle and you copy and paste code that says
public class SubHunter extends Activity {
Etc…
Then that won’t work. You would need to copy and paste everything and swap SubHunter for whatever your class was automatically named (probably MainActivity).
The alternative action is to rename MainActivity but this is not done just by changing the text as that will not do a complete job and will cause an error. You need to right click MainActivity in the project explorer window and select Refactor || Rename and rename it to SubHunter. You can do this with all the projects and then you will be able to copy everything from the download bundle more easily.
If either of the previous options is used then the code will work so if you still have a crash then I guess your problem must be caused by something else. If you click on the Run tab (usually at the bottom of Android Studio) it will give feedback about the causes of the crash (usually highlighted in red). If you send that text to me I will be happy to try and identify the cause. As a guess at the possible cause it could be the following autogenerated line of code:
setContentView(R.layout.activity_main);
That code would not have been generated following the instructions using the old 3.2 project wizard. As you will see later on in the project we will add very similar code to the preceding code but if you have deleted the activity_main file that code causes a crash.
Feel free to ask for more specific info because I have a funny feeling that this is going to be an annoyance for lots of readers and I will try and provide whatever info is required to get everybody started.
When I finished the book I downloaded the latest beta 3.2 and updated chapter 1 accordingly. Then 3.3 came out and “simplified” the project creation wizard.
Hope this helps,
John
Thank you for taking the time to give me such a detailed answer! I was able to refactor and rename and it seemed to run normally but would crash in the emulator. So I downloaded Android Studio 3.2 and started again and everything runs without a hitch. Thanks again for your help!
Richard
Hi Richard, glad you got it working. At some point you will need to move to the latest version of Android Studio but hopefully by then refactoring/renaming a file and the class will be simple for you.
Hi John,
How can I get your books like Learning Java by Building Android Games Second Edition for free? 😀
Hi Peter,
You could try here: https://www.packtpub.com/books/info/packt/contact-us
On the “What would you like help with” drop-down selector choose “Review a book”.
Sales are quite good but there aren’t many reviews yet so they might give you a free copy in return for an honest Amazon review.
Good luck and thanks for the question.
John
Is Android studio is very good for game developers ?
It is the standard tool for Android development but depending on your project goals there are other options. http://gamecodeschool.com/blog/making-games-where-do-i-start/
Sir , android studio or Unity which is best for games development?
&
How many developers using android studio for game development?
“best” for game development is a difficult question. Try this discussion. http://gamecodeschool.com/blog/making-games-where-do-i-start/
Hi John. I am really enjoying going through your book! I believe I ran into an issue with the pong game. It works, but there appears to be an anomaly where the ball can get stuck against the edge or even off the screen. I recreated the issue using a Pixel 2 XL – real and virtual. Unless I am missing something, I think it is related to the way the frame rate can vary in the game along with the way the collision handling works.
Let’s say the frame rate drops as the ball is about to hit the left wall. A lower frame rate increases the number of pixels for the ball to be moved during the collision. So the X coordinate of the left side of the ball might go down to -25. Our code detects the collision and reverses the velocity. But now let’s say the the frame rate jumps up, thus lowering the number of pixels that the ball will move on the next frame. So perhaps it moves 15 pixels to the right along the X axis.
At this point the X coordinate is at -10, so still < 0 and our code will treat it as a collision and reverse the velocity again so the next frame moves the ball to a even lower negative X coordinate. At this point the collision check code executes on every frame (unless the frame rate drops low enough on a move the X value to the right to get us back on the screen where X would not be less than 0 or the game ends).
I realize the pong game is purposefully simple in order to teach a specific set of concepts and not meant to be a polished game, so I am not saying it needs to work perfectly. I am just curious if my suspicions about the cause for the behavior are correct.
Thanks for your message its nice to hear from you.
Yes I think your suggestion of the cause is right.
Yes the code is flawed through simplicity. A quick fix might be to move the ball each time it collides with a screen edge to the nearest pixel that does not cause another collision. As an example you could move the ball to pixel 0 horizontally when it collides with the left. You could also set the ball with the specific appropriate velocity (ie positive after left hand collision) rather that just calling reverse…() which can get in to a muddle.
// Left
if(mBall.getRect().left < 0){ // mBall.reverseXVelocity(); mSP.play(mBopID, 1, 1, 0, 0, 1); // Move ball to 0, top // Set the ball specifically to a positive horizontal velocity } Another solution is partly covered in the final project when the pixel coordinates are abstracted away into “real world” coordinates. The Camera class could be adapted from the final project and used to make a Pong game that had better collision detection if you also combined it with a separate thread for the “physics” and do the collision detection more frequently. At the moment the current thread, because of the drawing using SurfaceView is locked to a maximum of 60 frames per second. Even when this is maintained the current code might not work perfectly. If you updated, moved and checked for collisions with the ball say, 3 times for each time the screen was drawn, it would probably work really well. Thanks very much for getting my book and for sharing your thoughts.
Hi, john am just a begginner can u pls help me
Certainly. Feel free to ask a question.
Hello John, great book – if not a little bit dated for me (I’m using Android Studios 3.5). I have some previous Java experience and finished the first game, Sub Hunter in just the first couple hours, but have run into what I’m guessing are some compatibility issues, for example I noticed instead of extending ‘Activity’, the default is now ‘AppCompatActivity’. That was an easy fix, however I am simply unable to figure out how to force ‘full screen’ – your additions to the Manifest.xml are being rejected. Are there more recent tutorials I should look for, or would you recommend downloading Android Studios 3.2 (if even still possible)?
Hi Matt,
Thanks for your feedback. The default has been appCompat… for some years. What has changed is that the new project wizard no longer gives you a choice and you have to change it manually as you have worked out for yourself. As long as you use the same code as the download bundle/book text then none of the projects should cause you a problem with this aspect. Regarding the fullscreen issue this seems odd. I am in the middle of a long project and can’t upgrade to 3.5 as I don’t want to risk any issues until the project is done so can’t test it right now. I reccomend you stick with the latest version of Android Studio. I have just been searching the Web and I can’t find any reference that the XML to have a full screen layout has changed and it seems strange that they would break old apps. Usually they deprecate and still support changes. Are you by any chance copy and pasting the entire xml from the download rather than just adding the few lines suggested? This would cause an error as it contains the wrong package name(obviously everyone has a different package name). The XML and the Java package name is the only part of the code that can’t be copy and pasted. If this is the case then this next line needs to be amended in the xml to your package name:
package=”com.gamecodeschool.subhunter”>
If this is not the cause then perhaps they have discontinued the theme? If this is the case then try removing this line from the xml:
android:theme=”@android:style/Theme.NoTitleBar.Fullscreen”
And adding this line after the call to setContentView in the Java.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
For the above code to work you will need to import the WindowManager class with this line of code placed with the other imports.
import android.view.WindowManager;
But either way all the games will run fine with a title bar as long as the device is in landscape orientation.
It is true the book is a year old now and the project wizard has changed but the Java code throughout is 100% compatible with the latest Android devices and emulators so hopefully you won’t have any issues related to comaptibility for some time to come. I usually update every three years or so and if something “breaks” the book I will publish the solution here.
Thanks very much for getting my book. I wish you great success with your coding.
John
Thank you, John, for that swift response – thought you might like to know that your suggestion worked perfectly. I ended up having to add the call to getWindow() to the ‘onCreate’ method to achieve full screen – turned out the line: android:theme=”@android:style/Theme.NoTitleBar.Fullscreen” was doing nothing, it was just as happy without it. Perhaps you were right and they DID discontinue the theme – my understanding of the inner-workings of android studio are still far too limited to say one way or another yet. Bottom line: I’m back on track and greatly looking forward to the rest of this book (and any others you release in the not-too-distant future.
Glad this works for you.
Hey John Ive started to read your book Learning android apps by making games. I’ve gotta tell you, reading your book is an absolute blast and I love it. I have taken some courses in High School in computer science and learned a lot about java. Reading your books has helped me gain an even better understanding of these concepts and I just Wanted to thank you for your sharing your knowledge. I
Many thanks Muhammad! I am very pleased the book has helped.Good luck with your studies.
Hi John, great book I have been working through it and ran into a bit of a problem. I’m fairly new to Java and am not sure how to word the issue exactly. I’m currently working on the Pong game and just got to the part of code that moves the bat but when I do this the bat seems to just extend as if the area where it was at previously is not going a way so I am ending up with just one giant bat across the bottom of the screen. I have tried looking back over the code in the book but cannot seem to find anything that is different from the book’s code and what I have. Any advise would be really helpful. (Also I do not have a downloadable bundle for the code as you mentioned to so of the other comments.)
Hi Chris,
Nice to hear from you. I have had the growing object a few times myself. Most likely only one side of the object is having its coordinates updated and the other side is not moving, causing the object to grow instead of move. Comparing the code is the best way to find the problem. The cause could be in the drawing or the updating. You can browse and download the code from github here https://github.com/PacktPublishing/Learning-Java-by-Building-Android-Games-Second-Edition
Thanks for getting the book and I hopis helps.
John
Thank you for the quick reply as well as the link. I figured out the issue, it was just a simple mistake on my part. I tried setting the background color to black and ended up setting the alpha to 0 by mistake. Once I changed it everything was working correctly. Thanks again for your help.
I think I am having similar to Matt Foster above;
The latest version of Android Studio, 3.6.2, is creating “Phone / Tablet” blank activity projects that it seems to think are for general web browsing. When I try to edit the subhunter android manifest xml file with the screen orientation “landscape” it has an error about how Chrome users would not be able to have a good experience and fails to compile unless I use “unspecified” or “fullsensor”.
The template project does run “Hello World” in the emulated Pixel so I must have the basics set up properly. I’ll try to follow the above instructions to see if it works when I get there.
Stuff like this creates huge barriers to trying to learn on your own; I wish the people who made the studio program cared about that at all
Yes agreed. It seems that Google scheduleeach new release of Android Studuio for the day my new book is printed. If you are still struggling see if this is any help to get an empty project started albeit with a different name https://github.com/PassMyNaptime/tutorials-java-spaceinvaders
Hello John,
I’m using Android Studio 3.2.1 and immediately finished Chapter 23. But I got an error at Page 648, Where I am not able to solve it with google. Tried it with written main_menu.xml and other names in the res/menu/ folder
“// Create and scale the bitmaps
mMenuBitmap = BitmapFactory
.decodeResource(context.getResources(),
R.drawable.menu); ”
But the error is still there: ” error cannot find symbol variable menu ” , yes menu is red highlighted
I am running the apps on connected Device always.
Do you have an idea how to solve it?
Hi there. Have you tried invallidating caches and restarting? https://stackoverflow.com/questions/31884361/what-does-the-invalidate-caches-restart-do-in-android-studio
I am sorry. the fault was on my site. I didn’t the paste all graphics to the drawable folder. (menu was missing).
Well I´ve finishid the book.It is great. At last I have some problems with the collision on moveable platforms just testing to play more with the code. Startet with small projects some rework on the Pong-Game and a Copy of the classic Bombermann.
Many thanks Aleksej!
Hi, John:
My son is working at games in your well written book “Learning Java by Building Android Games 2nd Edition”. He is now reaching chapter 23, which asks him to ” Copy the LevelCity file from the Chapter
23/Levels folder of the download bundle. “. But the download bundle does not seem to have chapter 23. Checking the link “https://github.com/PacktPublishing/Learning-Java-by-Building-Android-Games-Second-Edition”, I found it also misses chapter 23.
Would you please tell us where to get chapter 23?
Thanks,
Davis
Hi there,
I just checked the GitHub link and all the files for chapter 22 and 23 are in the chapter 22 folder here: https://github.com/PacktPublishing/Learning-Java-by-Building-Android-Games-Second-Edition/tree/master/Chapter22 and the specific file you refer to is here: https://github.com/PacktPublishing/Learning-Java-by-Building-Android-Games-Second-Edition/tree/master/Chapter22/Java/Levels
Good luck with the project it is quite an achievement for a student to get this far without the help of a teacher!
All the best,
John
I sorry I no writing good. I got shocked by so much the electric when I working on a power pole for a big internet company. I also had the mini-strokes to. Now I the mentality disabled.
I wanting to thanking the Mr. John Horton for writing the book “Learning Java by Building Android Games”. It helping me learning computer programming again. I thinking I never be able to doing it again. But my brother buying me this book and saying “Give it a try….you can doing it.”. Mr. Horton’s book has given me purpose in life again. His book is so the awesome! Thank you again Mr Horton!
Hi John
I’ve got to page 472 without a hitch, but now I have an error message in GameEngine when initiating mGameState (mGameState = new GameState(this, context). ) The message tells me the call requires API of 33 (current min is 24). How do I deal with this?
I have another error message! The same line of code and the message says GameState expects ‘this’ to be a boolean, which GameEngine cannot be. Where has this gpne wrong?
hi John,
first of all thanks for book, it is very help full. I have three Questions.
1. I m curious that as android studio updated, is there any plan of your to write new version of this book.
2. specially in kotlin like “Learning kotlin by building android games”.
3. and any book for 3d game development using only android studio (openGL_Es) without using third party.