Snake game App Inventor (PART 2) | How to make Snake Game in MIT App Inventor
Hello friends and welcome to Obsidian Soft,
We started the classic Nokia snake game in the last tutorial and we will finish its code today.
We are going to start with the snake’s movement logic.
First, remember the screen coordinate system: The x coordinate increases when we move right and decreases when we move left. The y coordinate increases when we move down and decreases when we move up.
In each game loop i.e. clock’s timer event of 300 milliseconds, we will call a snake movement procedure.
Let’s see what will happen in this procedure:
Our snake head starts at position 50,50
State of the lists:
snakeXList = [50]
snakeYList =[50]
Let’s assume that the user presses the right arrow button.
So, in our moveSnake procedure,
We will check the direction and if it is equal to RIGHT, we will insert a new head at index =1 with movement pixels=16 added to the previous head’s X i.e. 50. Our new head’s Y will stay the same
So, now this is the state of our lists:
snakeXList = [66, 50]
snakeYList =[50, 50]
But as you notice, our snake has grown without eating food, so we are going to check if it has eaten food. If it hasn’t, then we will remove the tail, i.e., the last segment in our snake, which in this scenario is the previous head at index =2.
So, now this is the state of our lists:
snakeXList = [66]
snakeYList =[50]
You can see that our snake will look as if it has moved right in this cool way of just inserting a new head and removing the extra tail.
After another 300 ms, we will come inside the snake move procedure again and check for directions. Assuming that it is still RIGHT, we will insert another head after adding 16 to the previous head’s X.
So, now this is the state of our lists:
snakeXList = [82, 66]
snakeYList =[50, 50]
We will also check if the snake’s head is touching food which is true so we will not remove the tail.
So, this is the basic way our snake moves.
Now, let’s work on the moveSnake procedure:
First, we make two local variables for storing previousHeadX and previousHeadY values which are values at index 1 for snakeXlist and snakeYlist. We need to store these for later use. Then we insert a new head at index 1 after calculating its new position depending on the value of direction.
So, depending on the direction, we have inserted a new head.
Now, we will check if we need to remove the tail. I will work on the collision detection code later so let’s just put an if/else block from control. The condition being true will mean that food was eaten. I will work on that later but the other condition means food was not eaten so remove the extra tail. Tail is the last segment in our snake so it will correspond to index = length of list. Remember that we must remove it from both the X and Y lists.
Now, we will check for screen edges and give a new position to the head of the snake if it has reached an edge. For example, if the x of the head is greater than the canvas's width then this means that it has reached the extreme right of the screen. In such a condition, we are going to change the x to 0 so that the head will now appear on the left side. We will do this kind of logic for all sides.
Now, we have to write some code to detect collisions between circles. Why? Because we have the snake’s head that is a circle and it should detect when it touches the food circle. Similarly, we need to know when the snake’s head touches any other circular part of its body.
We will use the concept of Euclidean distance. Euclidean distance is the shortest distance between two points (the length of the line between two points)
The formula is
So, we can find this distance between the centers of two circles. If this distance comes out to be less than or equal to the sum of the radii of the two circles, then this means that the two circles are either touching or overlapping.
So, let’s make a new procedure for calculating this distance. It takes in five inputs where four are the x and y coordinates of the two circle centers and one is the radius
Once we have the detection code, let’s complete our moveSnake procedure where we had to detect food.
We plug the call for detectCollision in our if condition. We will keep radius = circle size as we want the food to be detected even on touch.
What should happen when the food is supposedly eaten:
Our score should increase by 1
The score label should be updated on the screen.
Food should be given a new position.
So, moveSnake procedure is complete.
Let's finally work on our game loop which is the timer event. First call moveSnake procedure.
Next, we are going to check for collision between the head and each part of its body using a for loop that starts from index 2 i.e. the second segment of the snake and we compare for collision with the head at index =1. We will pass radius as 4 instead of circle size as I want to decrease the sensitivity of collision so that if the snake’s head overlaps with any part of the body, game over is triggered and not just on touching (remember that the snake head is always touching its second segment). If you change the circle size to some other size, you will have to update this radius of 4 here too (make it half of your circle size ) otherwise your GAME OVER condition will be triggered as soon as the game starts.
If the collision is detected, we will make gameOver true and disable the timer so that the snake stops moving. A break block from control will get us out of the for loop as we don’t need to check further.
In the end, we just draw everything by calling Canvas.draw so this is our final game loop:
The snake game is finally complete.
Thank you for being patient and waiting for the part 2 of the tutorial.
Go to Part 1 of the Written Tutorial
I hope you enjoy making this game as much as I enjoyed designing and creating it for you.
Please don’t forget to subscribe if you haven’t done it yet so that you don’t miss any of the great tutorials that i have planned for you.
Please like my videos and share them with your friends and family. Also, subscribe to my channel and press the bell icon so you don't miss any of the great projects I have planned for you.https://www.youtube.com/c/obsidiansofteducation
Comments
Post a Comment