Build a Snake Game - Adding Interaction

In this tutorial series, you'll learn how to create a game like Snake. The objective of the game is to grab the apples on screen to raise the score. Read on!


Where We Left Off. . .

Please be sure to check part 1 of the series to fully understand and prepare for this tutorial.


Step 1: Start Button Listeners

This function adds the necesary listeners to the TitleView buttons.


Step 2: Show Credits

The credits screen is shown when the user taps the about button, a tap listener is added to the credits view to remove it.


Step 3: Hide Credits

When the credits screen is tapped, it'll be tweened out of the stage and removed.


Step 4: Show Game View

When the Start button is tapped, the title view is tweened and removed revealing the game view. There are many parts involved in this view so we'll split them in the next steps.


Step 5: Add Game Background

This code places the game background in the stage.


Step 6: Add Game Pad

In order to move the snake in the screen we'll need a game pad, this will take care of that. A tap listener will be added later to each arrow to handle the movement.


Step 7: Score Text

This line adds the score textfield:


Step 8: Head

Next we add the snake's head. As mentioned in the previous tutorial, a hit area will be added on top of it and then both will be stored in a group.


Step 9: Initial Apple

The first apple is added by this code at a random position.


Step 10: Game Listeners

This function adds the necessary listeners to start the game logic.


Step 11: Move Player Function

The direction variable is changed by this function, this will make the snake go in the pressed direction.


Step 12: Hit Test Objects

We'll use an excellent and useful function for collision detection without physics, you can find the original example and source at the CoronaLabs Code Exchange website.


Step 13: Get Head Last Position

We capture the snake's head last position to update the parts position, each one replacing the x and y coordinates of the other.


Step 14: Move Parts

This complements the behavior explained in the past step.


Step 15: Check for Game Over

The game ends when the snake's head touches another part of the snake. A sound is played as feedback.


Step 16: Move Head

These lines move the head according to the direction stablished by the movePlayer function created in step 11.


Step 17: Apple-Head Collision

Several actions occurr when the snake eats an apple we will go through them in the following steps.

First we remove the apple.


Step 18: Update Score

Next we update the score textfield and play a sound to indicate that the apple has been eaten.


Step 19: Add the Snake Part

Then a new part is added to the snake. Its position is calculated by the last part added (the first time is the head) and then they are added to a group.


Step 20: New Apple

Lastly, we create a new apple and place it in a random position in the stage.


Step 21: Screen Limits

This makes the snake appear on the other side of the stage.


Step 22: Call Main Function

In order to start the game, the Main function needs to be called. With the above code in place, we'll do that here:


Step 23: Loading Screen

The Default.png file is an image that will be displayed right when you start the application while the iOS loads the basic data to show the Main Screen. Add this image to your project source folder, it will be automatically added by the Corona compliler.


Step 24: Icon

Using the graphics you created before you can now create a nice and good looking icon. The icon size for the non-retina iPhone icon is 57x57px, but the retina version is 114x114px and the iTunes store requires a 512x512px version. I suggest creating the 512x512 version first and then scaling down for the other sizes.

It doesn't need to have the rounded corners or the transparent glare, iTunes and the iPhone will do that for you.


Step 25: Testing in Simulator

It's time to do the final test. Open the Corona Simulator, browse to your project folder, and then click open. If everything works as expected, you are ready for the final step!


Step 26: Build

In the Corona Simulator go to File > Build and select your target device. Fill the required data and click build. Wait a few seconds and your app will be ready for device testing and/or submission for distribution!


Conclusion

Experiment with the final result and try to make your custom version of the game!

I hope you liked this tutorial series and find it helpful. Thank you for reading!

Tags:

Comments

Related Articles