banner



How To Get More Apples In Snake

Serpent is a classic game where the actor will control a modest ophidian using arrow primal controls. When the serpent eats a collectable item on the map, information technology'll grow in length.

Fun, right? Permit's take a expect at how to easily create Snake in Scratch.

Sprite Creation

For a Snake game, at that place are two Scratch sprites that need to be created. One is the ophidian sprite and the other is the item the snake will be chasing. In this example, that collectable detail will be an apple. Scratch Paint can be used to make the sprites.

Design a Snake

The snake volition actually be divided into two sprites: its caput and body. The body will follow the head sprite around the map and grow as the ophidian collects items.

1. Go to Scratch > Create to get-go a new project titled "Ophidian Game".

two. Hover over the Choose a Sprite button and select Paint.

3. Draw a circle for the snake's head, using a black outline on the shape.

4. Describe a pink tongue for the snake, and click Dorsum to identify it under the snake'due south head.

snake head sprite scratch

Draw the Body

The snake'due south body will be drawn separately, so it tin can grow every bit the snake collects more than items without affecting the ophidian's head.

ane. Hover over the Cull a Sprite button and select Paint to create a new sprite.

2. Draw a circle with no outlines for the snake'south body.

Line Up the Ophidian

Now, with ii parts of the snake - the caput and the body - you tin can line them up in the game preview window (you might need to resize them).

1. Adjust the snake sprites so that they're small and centered on the screen.

2. Motion the body slightly to the left of the head, so they look connected.

Every bit a tip, the snake's trunk should be nigh 1/3rd the size of its head. Information technology should also slightly overlap the head, so it looks like a neck.

The sprites should exist small on the screen so the player has plenty of infinite to move around and collect items.

Snake Movement

But to be clear, in this game, the snake volition constantly exist moving forward. Information technology's up to the actor to then command the ophidian's direction using arrow fundamental commands.

Movement

To create movement:

1. Select the Snake Head sprite and add when flag clicked to the code area.

ii. Attach point in direction 90 and go to x, y  0, 0 to the block.

three. Adhere a forever block with move v steps within.

Control

To control the ophidian:

1. Add when infinite cardinal pressed and fix information technology to upwardly pointer.

2. Attach point in direction 0 to the block.

3. Right-click and indistinguishable the cake, and ready information technology to downwards pointer with a direction of 180.

4. Duplicate the block two more times.

5. Set ane to right arrow with a direction of 90, and left pointer at -90.

gif showing progress with code blocks

As can be seen in the image higher up, the snake's trunk doesn't follow the head. This is because there isn't any code attached to it yet, so the body doesn't know what to do.

To make the body look connected, it will need to be coded to follow the head sprite.

Follow the Leader

Select the Snake Body sprite and add when flag clicked to the lawmaking expanse.

Attach indicate in direction xc and a get to x, y block.

Attach a forever ↺ block with bespeak towards and move 5 steps within.

Choose Ophidian Head from the dropdown for the point towards block.

Changing Length

As a reminder, for the snake to grow, it needs to collect items (like the apple). In this game, any time the serpent eats an apple, its tail will grow in length.

To rail the snake's length, a variable needs to be created. This variable volition rail how many apples the snake eats, which will then be equal to how long the snake is.

Variables

This game will utilize two variables: a score and a length variable, which will determine how long the ophidian becomes.

In the Code tab, select Variables and click Make a Variable.

Create a variable namedscore and a variable namedlength.

Set the Length

The snake's torso will grow every time it eats an apple. To do this, it'll create clones of itself equal to the amount information technology'southward eaten.

i. Select the Snake Body sprite.

two. Attach theset length to 0 cake to the acme of its code.

three. Drag a create clone of myself block into the forever ↺ block (at the bottom).

4. Add together when I outset as a clone to the code surface area.

5. Attachwait anddelete this clone to the block.

6. Drag the length variable into the wait cake.

The length is set, but currently, the length will always be 0, because the variable isn't being changed. To change the variable, create and code the collectable detail.

Collecting Items

For the snake to grow, yous'll need to pattern an detail sprite that the snake tin consume.

Create a Collectable

This example uses an apple, but yous can design any collectable detail you want!

ane. Hover over the Choose a Sprite button and select Paint to create a new sprite.

2. Draw a collectable item.

Set the Position

The apple tree will announced at random points on the map, making it impossible to predict where it'll prove up next!

one. Select the Apple sprite and attachwhen flag clicked to the code area.

2. Attach a set score to 0 block. Eating apples will earn points for the histrion.

three. Attach get to random position,if on border, bounce, andbetoken in direction 90 to the code.

Collecting Apples

When the snake touches an apple, both the score and length variables will increase. The apple will likewise disappear, and so reappear in a new location.

1. Add togetherwhen flag clicked with aforever ↺ block to the lawmaking expanse.

2. Attach anif then <> cake with <touching Snake Head?> in the bracket.

three. Inside the if statement, add hide withmodify length by 0.1 and change score by 10.

iv. Add together a broadcast cake in the statement with the message "Reset Apple".

Reset the Apple

1. Add togetherwhen I receive Reset Apple to the lawmaking expanse.

2. Duplicate the go to random position code blocks onto the receive block.3

3. Adhere ashow cake to the code.

And that'southward information technology! you should now accept a Snake game to play in scratch. Thinking about what the next steps could be? Kids tin can consider things similar adding a "game over" if the snake touches its ain trunk or the map border.

If they are stuck and demand help, consider our many online coding classes for kids or Scratch summer camps.

A photo of Ryan

Ryan manages weblog content at iD Tech, starting with the company in 2008. He earned his MBA from Santa Clara Academy afterward obtaining his Available's caste from Arizona State. Connect on LinkedIn!

Source: https://www.idtech.com/blog/how-to-make-a-snake-game-in-scratch

0 Response to "How To Get More Apples In Snake"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel