How to create a classic snake hunting game using Scratch

How to create a classic snake hunting game using Scratch

How to create a classic snake hunting game using Scratch

Content Protection by DMCA.com

The game Snake is associated with the childhood of many generations. And now, even with little programming experience, children can create a Snake game using Scratch.

Choose a suitable wallpaper

For the Snake game, children will need two backgrounds as follows:

The first background image is for the gaming area and will appear immediately at the start of the game. Kids can draw their own wallpaper, upload an existing wallpaper, or choose from Scratch’s library. First, choose a background image from Scratch’s available library by clicking the background selection option as shown below:

Then, in the “outdoors” section of the Scratch library, select the forest background as shown below:

Choose a forest setting

The second background image displays when the game ends (game over); This background image will appear as soon as the snake touches the edge of the playing area. For this wallpaper, select the paint tool and fill the screen with any color via the fill tool. First, you need to convert the image to bitmap by clicking the corresponding button below, then click on the screen to fill the selected color.

Color

The next step is to click on the T option (used to insert text), choose a different font style (e.g. curly font), choose a different fill color, and then write the text “GAME OVER” appears on the screen as shown in the illustration below.

Adjust game context

Additionally, you can use your creativity to add different objects using the built-in shaping tools as shown below, and finally rename the background.

Read More  How to make a rock, paper, scissors game in Scratch

Adjust the game backdrop over

Now, children have 2 backgrounds for the project: forest photo and game over.

Select sprites

This game requires two sprites: food and snake. You can choose any sprite as food from the library, upload it, or draw it yourself. Since snakes often eat insects, select the grasshopper sprite from the Scratch library by clicking the Choose a sprite option (Choose a sprite) as shown below.

Select Sprite

Next, select the grasshopper sprite by going to the animals category. Children can even easily see grasshoppers in the available search box.

Photo of a grasshopper

The next sprite will be the snake. To do this, the guide downloaded images of the snake’s scaly head and body from the internet; Before use, remove the background of these images by uploading them to https://www.remove.bg/upload and then redownload them to your device. You can then upload one of the images as a sprite and include the other image in the costume section of that sprite. Finally, we will have a sprite called “snake” with two costumes, the head and the body, as illustrated below.

Costumes for snakes

Programming for background

As soon as you click on the green flag, the first background that appears will be the forest – which serves as the playing area. To do this, write the following code.

Additionally, if you want background music to play continuously, you can nest the sound play command block inside the “continuous” block (forever. forever) when clicking the green flag, as shown below. Kids can also choose their desired sound from the built-in sound library (located next to the costume section) or even upload or record their own sounds.

Read More  Instructions for making Cross the Road game on Scratch

Program the food sprite to be a grasshopper

First, create two variables, `score. score` (score) and `length. length` (length), then initialize their value to 0. That is, as soon as the green flag is clicked, the value of the ` variablesscore. score` and `length. length` will be set to 0.

Next, move the grasshopper on the stage to the farthest left corner to check the coordinates xthen move it to the farthest right corner and check the coordinates x there. This will be the coordinate range x where grasshoppers may appear. In this case, that range is approximately -217 to 217. Similarly, check the y coordinates at the top and bottom edges of the stage by dragging the grasshopper to those locations.

Thus, the grasshopper will receive random x and y coordinates every time we start the game. Now, inside the ` command blockforever. forever` (continuously), check to see if the grasshopper is touching the snake. When there is a collision, increment the ` variablescore. score` add 1 (plus 1 point) and increase the variable `length. length` add 0.1; Increasing this `length` variable will subsequently increase the length of the snake. Finally, use the move command block again to x, y coordinates (`go to x: y:`) to send the grasshopper to a random location every time the snake touches it; thereby completing the programming for the grasshopper character.

Grasshopper programming

Programming a snake sprite

Here, use one more variable named `direction. direction` to manage the snake’s movement direction. Variables can store many different types of data such as numbers, strings (text), logical values ​​(true or false), etc. Therefore, when we press the up arrow key, we will assign a value “Forward” (forward) for variable `direction. direction`. Similarly, for the down, left and right arrow keys, we will assign the values ​​”backward. backward” (backward) “left. left” (left) and “right. right” (right) for this variable.

Read More  Instructions for playing the game Scary Shawarma Kiosk: The Anomaly

Snake programming

Next, when clicking on the green flag, set the snake’s initial position at the center, with x and y coordinates both equal to 0. Then, convert the snake’s outfit to a head shape. To keep the snake moving continuously, check the value of the direction variable (direction. direction) previously set. Depending on whether the direction of movement is forward, backward, left or right, changes its x and y coordinates. As the sprite moves up, the y value will increase; As you move down, the y value will decrease. Similarly, when moving to the left, x decreases, and when moving to the right, x increases.

Here, inside the “continuous” command block (forever. forever), checks one more condition whether the snake is touching any edge of the screen. If it touches an edge, set the length variable to 0 and switch the background to the game over scene. Also, use the “stop all” command block (stop all) to stop executing all character scripts.

Also in this “continuous” command block, to increase the length of the snake, continuously create copies (clones) of the snake behind.

Navigation programming

As the clone begins its journey, make sure it takes on the appearance of the original object, as it is the image of that object that needs to be displayed in increasing size. Since duplicates are created continuously and can cause crashes due to memory usage, programmatically delete them after a period of time corresponding to the value of the variable `length. length`. Thus, the value of the variable `length` The larger it is, the longer the copy stays on the screen and the larger the display size is.

Programming Scratch games

Here is one of the resulting images:

Classic snake hunting game

GameHov is group of expert in gaming industry that cover all gaming news from e-sport to casual video entertainment.

Post Comment