To get started, make a new project called Snake
. This time, we will use the Empty Activity template as we will not be creating any UI whatsoever.
SnakeActivity
.Activity
class in this final project.All the code and assets for this project are in the Chapter 28
folder of the download bundle. Note that for this project, the completed game, as it will be by completing the steps up to the end of Chapter 29, Enumerations and Finishing the Snake Game, is in the Chapter 28
folder. There is no separate code bundle for Chapter 29, Enumerations and Finishing the Snake Game.
Now, we will take an additional step to stop the screen from rotating and locking it in the landscape position.
Here is how to edit the AndroidManifest.xml
file to achieve this:
AndroidManifest.xml
file in the editor window.AndroidManifest.xml
file, locate the following line of code: android:name=".SnakeActivity">
>
symbol shown previously. Tap the Enter key a couple of times to move the >
symbol a couple of lines below the rest of the line shown previously.SnakeActivity,
but before the newly positioned >
symbol, type or copy and paste these two lines to make the game run full screen and lock it in the landscape orientation:android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:screenOrientation="landscape"
We will also make some empty classes ready for us to add code as we proceed through the project. This will mean there are fewer errors as we proceed.
As we have done in previous projects, you can create a new class by selecting File | New | Java Class. Create three empty classes called Snake
, Apple
, and SnakeGame.
Now we are getting comfortable with OOP, we will save a couple of lines of code and pass point
straight into the SnakeGame
constructor instead of dissecting it into separate horizontal and vertical int
variables, as we did in the Live Drawing project. Add all of the following code.
Here is the entire SnakeActivity
code:
import android.app.Activity; import android.graphics.Point; import android.os.Bundle; import android.view.Display; public class SnakeActivity extends Activity { // Declare an instance of SnakeGame SnakeGame mSnakeGame; // Set the game up @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the pixel dimensions of the screen Display display = getWindowManager() .getDefaultDisplay(); // Initialize the result into a Point object Point size = new Point(); display.getSize(size); // Create a new instance of the SnakeEngine class mSnakeGame = new SnakeGame(this, size); // Make snakeEngine the view of the Activity setContentView(mSnakeGame); } // Start the thread in snakeEngine @Override protected void onResume() { super.onResume(); mSnakeGame.resume(); } // Stop the thread in snakeEngine @Override protected void onPause() { super.onPause(); mSnakeGame.pause(); } }
The previous code should look very familiar.
As mentioned previously, we don't bother reading the x and y values from size;
we just pass it straight into the SnakeGame
constructor.
The rest of the code for the SnakeActivity
is identical in function to the Live Drawing project. Obviously, we are using a new variable name, mSnakeGame
.
Grab the sound files for this project; they are in the Chapter 28
folder of the download bundle. Copy the assets
folder, and then navigate to Snake/app/src/main
using your operating system's file browser and paste the assets
folder along with all its contents.
The sound files are now available for the project.
18.191.212.211