A short introduction to creating a game on the Android platform using Java
The first example covers the basics: There are no objectives, but it shows you how you create a basic part of a 2D game using SurfaceView.
Make sure to save any important data when you create a game; everything else will be lost
Game using Canvas and SurfaceView
This covers how you can create a basic 2D game using SurfaceView.
First, we need an activity:
The activity also has to be declared in the Android Manifest.
Now for the game itself. First, we start by implementing a game thread:
That is the basic part. Now you have the ability to draw onto the screen.
Now, let's start by adding to integers:
For this next part, you are going to need an image. It should be about 100x100 but it can be bigger or smaller. For learning, a Rect can also be used(but that requires change in code a little bit down)
Now, we declare a Bitmap:
In render, we need to draw this bitmap.
BEFORE LAUNCHING there are still some things to be done
We need a boolean first:
in onTouchEvent, we add:
And in tick we need this to move the player:
and now we need this in init:
And we need these to variables:
At this point, the game is runnable. Meaning you can launch it and test it.
Now you should have a player image or rect going up and down the screen. The player can be created as a custom class if needed. Then all the player-related things can be moved into that class, and use an instance of that class to move, render and do other logic.
Now, as you probably saw under testing it flies off the screen. So we need to limit it.
First, we need to declare the Rect:
In init, after initializing width and height, we create a new rect that is the screen.
Now we need another rect in the form of a method:
and in tick:
The implementation of gameOVer can also be used to show the start of a game.
Other aspects of a game worth noting:
Saving(currently missing in documentation)
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0