Snake In Scratch

Snake In Scratch

May 07 2021

Snake In Scratch

In this tutorial you will learn how to build the game snake. The game is an arcade game and it has very simple logic, which is why it is an ideal example to demonstrate how to build games with Pygame.

Snake In Scratch you can create a snake game by following these steps Objectives 1. To understand how the pen tool works 2. To understand how to use colours in Scratch to make game Step 1: Create 2 Sprites A mouse. Your snake will try and capture this. This is what you will control. The snake game, or Snaaaaaake, is a. Since all Scratch sprites start facing 90 degrees (that is, to the right), you should draw the head facing right. After drawing it, rename the sprite Head. Use the Shrink tool or Grow tool at the top of the Scratch editor to shrink or grow your Head sprite. The Scratch Wiki is made by and for Scratchers. Do you want to contribute? Learn more about joining as an editor! See discussions in the Community Portal. Creating a Multiplayer Game. This article or section may not have content matching Scratch Wiki editing standards.

The player is represented as snake, which grows if it eats an apple. The goal of the game is to eat as many apples as possible without colliding into yourself. This is very easy in the early phase of the game but is increasingly more difficult as the length of the snake grows.

Related course:

Getting started: Basic structure and event handling.
We define a class Player which holds the players position on the screen and the speed by which it moves. In addition we define the actions a Player instance can do (movements):

Snake In Scratch

A player object can be created and variables can be modified using the movement methods.
We link those methods to the events. In Pygame we can get non-blocking keyboard input using this code:

The complete code gives us the ability to move the player across the screen:

You can now move the block around the screen with the arrow keys.

<caption id=”attachment_589” align=”alignnone” width=”400”] pygame example. Move the block around the screen

Related course:

Building the player (snake)
The player controls a snake which has an initial length. This snake is always moving and changes the direction it moves when pressing an arrow key. To do so, update the player class:

and don’t forget to add a delay to the game loop.

This starts to act more as a snake, but it does not have a base length yet. We keep track of the snakes old positions and move the head of the snake. We also moved the drawing method into the snake. Copy the code and you will have a moving snake:

Result:

<caption id=”attachment_594” align=”alignnone” width=”592”] python snake

Game logic
The snake game has some rules:


  • If the snake eats an apple, the apple moves to a new position.

  • If the snake eats an apple, the snakes length grows.

  • If a snake collapses with itself, game over.


We start by creating a new class that enables us to create apples:
For simplicity sake, we display the apples as green cubes. We have this basic code, but it does not do a lot more than moving the snake and showing the apple:
We have thus to add the game logic. To know if the snakes position matches the apples position, we have to do collision detection. This simply means veryfing that the cordinate of the snake are intersecting with the coordinates of the apple. We create a new method to do that:
It will return True if the coordinates (x1,y1) are intersection with (x2,y2) given its block size bsize. We call this method to determine if the snake collides with the apple. We need to check this for the entire snake and not only for the head, because we do not want the new position of the apple to be somewhere on the snake. We use the same isCollision method to determine if the snake collides with itself (= lose game).
Full source:
Related course:

Conclusion:

How To Code Snake In Scratch

You learned how to create the game snake in Python along with concepts such as collision detection, image loading and event handling. Many things could be added to this little toy game but this serves as a very simple example. :-)

Here you have a scratch tutorial for a very fun game
This is a classic game it was actually one of the first pc games in the world, and it is the forefather of many popular games like slither.io and supersnake.io.
Her you can try snake on scratch

Instruction

  • Make 3 figures

Figure 1

  1. Make a button that is visible when the program start
  2. Add some code:
    • When the button is clicked, hide it and broadcast “new game”

Figure 2

  1. Add first code:
    • When I receive “New game”
    • Hide
    • Broadcast “Make New”
  2. Second code:
    • When I receive “Make New”
    • go to x “”14″*” pick random “-15” to “15”” y “”14″*” pick random “-10” to “10””
    • create clone of “myself”
  3. clone code:
    • When I start as a clone
    • Show
    • Wait until “touching “Figure 3”
    • Change “Length” by “2”
    • Brodcast “Make New”
    • Delete this clone

Original Snake Game

Figure 3

Snakes 3d Crazy Games

  1. Add the first code:
    • When start clicked go to x “-20” y “0” point in direction “-90”
  2. The second code you need to make is a test “if key (…) pressed point in direction (…)”
  3. Make one test for all the arrow keys and put it in an endless loop under “When I receive “new game””
  4. Third code is:
  5. Make 3 variables
    • Time
    • My time
    • Length
  6. Start when I receive “new game”
  7. Sett “Time” to “0”, and “Length” to “5”
  8. Make a loop:
    • Repeat until “touching “edge” or touching color “the color on your snake””
  9. Inside the loop
    • Set “My time” to “Time”
    • Change “Time” by “1”
    • wait “0,1”secunds
    • Create clone of “myself”
    • move “14” steps
  10. Make a code for the clone:
    • when I start as a clone
    • Wait until “Time” – “Length” = “My time”
    • Delete this clone

Snake In Scratch

Leave a Reply

Cancel reply