null
3
Games, level 0, lesson 1
Locked
Snake (Part 1/20)
Let's write the game Snake. Rules of the game: 1. The player controls the movement of the snake, which slithers about a square field: up, down, right and left. The snake cannot stop moving. 2. The objective of the game is to eat as many apples as possible. Apples appear in succession in a random
3
Games, level 0, lesson 1
Locked
Snake (Part 2/20)
Let's color the playing field. You can do this and every other game action in the initialize method. But cramming everything into one method is bad practice. So, we'll initially create a couple of methods, each of which will be responsible for a specific job: the drawScene method is for drawing the
3
Games, level 0, lesson 1
Locked
Snake (Part 3/20)
To store the game objects, we need an additional class. Objects of this class will store cell coordinates. Let's call it GameObject.
3
Games, level 0, lesson 1
Locked
Snake (Part 4/20)
One of the game objects will be an apple. Create an Apple class and make it inherit the GameObject class. To represent an apple on the playing field, you could paint cells containing apples any particular color. Or you could draw some apple-looking symbol in the cell. Let the Apple class store thi
3
Games, level 0, lesson 1
Locked
Snake (Part 5/20)
Now let's tackle the snake, and we'll remove the test creation and drawing of the apple in the createGame() method. Create a Snake class. The snake will consist of several cells, i.e. several GameObjects. What's more, the number of cells can vary: the snake grows longer when it eats apples. We'll
3
Games, level 0, lesson 1
Locked
Snake (Part 6/20)
Once we have a snake, we need to draw it. To do this, by analogy with the Apple class, create HEAD_SIGN and BODY_SIGN constants in the Snake class. They will store symbols for drawing the head and body of the snake. Also, create a draw(Game) method to draw the snake on the playing field. We'll han
3
Games, level 0, lesson 1
Locked
Snake (Part 7/20)
An object on the playing field can have one of two states: it can be alive (isAlive=true) or dead (isAlive=false). For example, if the snake hits the edge of the playing field, it dies. The same thing happens to an apple when it is eaten by the snake. To store object state, create a boolean isAlive
3
Games, level 0, lesson 1
Locked
Snake (Part 8/20)
The snake must move with every turn. A turn takes a certain amount of time, which you can set. To implement the logic for moving the snake, create a move() method in the Snake class. Override the onTurn(int) method of the parent of the SnakeGame class. Everything that should happen in the game dur
3
Games, level 0, lesson 1
Locked
Snake (Part 9/20)
Moving the snake is very simple: in a cell next to the head, a new head is created, and the last element (tail) of the snake is removed. In the Snake class, implement a createNewHead() method that creates and returns a new GameObject. The new GameObject must be created next to the element that cur
3
Games, level 0, lesson 1
Locked
Snake (Part 10/20)
Now you need to combine the work of the createNewHead() and removeTail() methods. The move() method is a good place to do this. Add a check to see whether the snake has gone beyond the boundaries of the playing field. If the new head is beyond the boundaries of the playing field, set the snake's s
3
Games, level 0, lesson 1
Locked
Snake (Part 11/20)
Let's add the ability to control our snake. To handle key presses, override the Game parent class's onKeyPress(Key) method. Depending on which key is pressed, set the appropriate direction of snake movement. Bear in mind that a snake cannot instantly turn 180 degrees.
3
Games, level 0, lesson 1
Locked
Snake (Part 12/20)
Now we have an apple and a snake that can move. Now we'll implement the logic to make them interact. To start, add an apple variable to the SnakeGame class, and initialize its coordinates to (5, 5) when the game is created. And, draw an apple. Rewrite the Snake class's move() method. Now it should
3
Games, level 0, lesson 1
Locked
Snake (Part 13/20)
Until now, we've set the apple's coordinates manually. We need to create a method for generating new apples. Let's call it createNewApple(). The method should generate random cell coordinates within the playing field, which determine where the apple will appear. Assign a reference to the newly crea
3
Games, level 0, lesson 1
Locked
Snake (Part 14/20)
Currently, the snake can pass through its own body, but this is against the rules. To fix this, you need to check the newly created snake head to see whether it coincides with any of the other snake segments. To do this, create a checkCollision(GameObject) method in the Snake class. The checkCollis
3
Games, level 0, lesson 1
Locked
Snake (Part 15/20)
To store the game state, we'll need the isGameStopped variable in the SnakeGame class. Initialize it in the createGame() method. When a game is lost, stop the game and display a message about the loss to the player. To do this, create a gameOver() method and add the statements that should be execut
3
Games, level 0, lesson 1
Locked
Snake (Part 16/20)
The game must also stop in the event of a win. The player wins if the snake grows to a certain size. To store this size, in the SnakeGame class, create a GOAL constant and initialize it to 28. To determine the snake's current size, create and implement a getLength() method in the Snake class. By an
3
Games, level 0, lesson 1
Locked
Snake (Part 17/20)
At this stage, we have a bug — an apple can appear on top of the snake. To fix the bug, use our checkCollision(GameObject) method. The createNewApple() method must create a new apple (with new coordinates) in a loop until the call to the checkCollision(GameObject) method returns false. Let's also
3
Games, level 0, lesson 1
Locked
Snake (Part 18/20)
Let's fix our snake control. The direction field can only be changed to currently valid values: LEFT, RIGHT, UP, and DOWN.
3