Let's Build SNAKE - Game Development Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello! Welcome to Build Succeeded! My name is Lucian and today I’m going to build the Snake game. The first Snake-like game was released in 1976 by a company named Gremlin. It was a 2 player game where the player who grows longer wins. There was no food to grow, you are just continuously growing until you hit yourself or the other player. This game style popularity increased dramatically with a Snake version that was released on Nokia phones in 1998. Today, there are over 300 Snake-like games just for iOS So … let’s just build another one. This is a game where a round resolution like 800 by 600 becomes really useful, because the position of the snake’s head, body and food is discrete, so we can split the screen in “boxes” of, let’s say, 20x20 pixels. We will be able to have game elements only in fixed positions inside these boxes. So, for this game, instead of using a structure of 2 floating point numbers to represent positions, I’m going to use a structure of 2 integers. How do we define the snake? We can see the snake is composed of a number of segments, each of them in its own position. So the snake can be an array of 2D structures, staring with position 0 (the head) and counting 1, 2, 3, 4 towards the tail. Since the snake can grow, we need to also know its length. We will initialize that by 5 and grow it as our snake eats. In order to move, we need to know which direction the snake is moving, so we will use another 2D structure for speed. It can be (-1, 0) for left, (1, 0) for right, (0, -1) for up and (0, 1) for down. When the snake moves, we need to go through each segment, starting from the last and going towards the head, and copy the position of the segment ahead of it. This one will take the position of this one, this one will take the position of this one, and so on until we get to the second one, which will take the position of the head. The head will advance by taking its own position and adding the speed to it. The other element on the screen is the food the snake has to eat. That is simply going to be represented by a 2D point. One main difference for this game compared to the previous games we built is that, since we are working with discrete values for position, we need to intentionally introduce a delay in each frame so the snake does not move too fast. I’ll test with a 100 milliseconds delay for each frame, which will max our FPS to 10. It’s fine for this game. Actually, it’s a good thing to have a low FPS. Now let’s take a look at our game parts. The first part is capturing and processing user input. In our case, I need to capture when the user presses the arrow keys to change the snake direction. We will need a bit of custom logic here, because the snake can’t take any direction. For example, if the snake goes to the left, the direction can only be changed to up or down. If the snake goes up, we can only change the direction to left or right. The other input I want to capture is the mouse left click, and I will use that to reset the game after the player hits itself or the walls. The second part is the logic of the game. And this time, we only have three items The first item is the movement of the snake. We discussed this earlier. The second item is checking if we are hitting the food. We can accomplish this by comparing the snake’s head position with the food’s position. If they are the same, we do 2 things: One, we grow the snake. We do that by increasing the length variable with one and setting the position of the new last element to the old last element. Two, we re-spawn the food. We generate a random new location, but we have to be careful not to generate over the body of the snake. So we check if the food’s position matches any of the snake’s body positions. If it does, we try again with a new position, until we’re in the clear. The third thing in our logic is to check if the snake hits the walls or itself. For the walls, we simply check the snake’s head position and test if it’s outside the screen area. For itself, we compare the position of the snake’s head with the position of every other snake segment’s position. If any collide, the game is over. The last part of the game engine, as usual, is drawing on the screen. We only need to draw the ball, then each segment of the snake. We are going to draw all as balls. I’m going to implement it using VIsual C++ and Direct 2D. Let’s do it! Development always goes better with a mexican cervesa. There’s our snake! Quite simple! We can control it with the keyboard. If we go over the food, the snake will grow. If the snake hits itself, the game stops. We can reset it with a mouse click and start over. Same happens when we go outside the play area. As you can notice, I’ve added a score, which is actually the length of the snake, and also a top score, which is the highest score so far. This is the main engine class. In the constructor, we initialize the snake and the food. The KeyUp method is where we handle the user’s keypress. The Logic method is where all the game logic happens. We do Snake->Advance() to make the snake move forward, then we check for food collision, in which case we reset the food’s position and grow the snake, and last, we check for the snake colliding with itself. This method actually contains logic for wall hits as well, so the name is maybe poorly chosen. In the Draw method, we call the Draw methods of the game elements: the snake and the food. The Food class is quite simple. The most important method is Reset. As you can see, we generate a random position, then test for overlapping the snake. And we do that until we find an empty spot on the screen. The Snake class contains most of the Snake logic. We have Reset, for when we need to reset the Snake in a fixed position. Then we have methods for changing directions, with logic to check if we can change to that specific direction. The Advance method moves the snake forward by copying each segment’s position from the previous one, except for the head, which will use the direction. CheckSelfCollision does two things actually: one is checking if the snake hits itself, the second one checks if the snake’s head is outside the play area. And then we have the Grow method which simply grows the snake’s length and sets the position of the new last element. We draw the snake using red for the head and green for the rest of the body. If you want to play around with the code, maybe make some improvements, you will find a link to the Github repository in the description of the video. Thank you for watching! If you like it, give me a thumbs up, and If you want to see more games built in the future, make sure to subscribe. See you soon!
Info
Channel: Build Succeeded
Views: 12,952
Rating: undefined out of 5
Keywords:
Id: XEeZujXWpZg
Channel Id: undefined
Length: 9min 8sec (548 seconds)
Published: Fri Apr 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.