Getting started with the Snake game

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.

  • Rename the Activity Name to SnakeActivity.
  • Uncheck the Generate Layout File option—we do not want a layout generated as we will be laying out every pixel ourselves.
  • Uncheck Backwards Compatibility (AppCompat)—we won't need it for our game and it will still run on almost every phone and tablet. We will be working with the plain vanilla Activity class in this final project.

Note

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.

Make the app full screen and landscape

Here is how to edit the AndroidManifest.xml file to achieve this:

  1. Open the AndroidManifest.xml file in the editor window.
  2. In the AndroidManifest.xml file, locate the following line of code: android:name=".SnakeActivity">
  3. Place the cursor before the closing > 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.
  4. Immediately below 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"

Tip

For more context/details, refer to the AndroidManifest.xml file in the download bundle.

Adding some empty classes

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.

Coding SnakeActivity

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.

Adding the sound effects

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.212.211