Hey guys! Ever wanted to create your own Snake game? Well, you're in the right place! In this step-by-step tutorial, we'll guide you through the process of building a classic Snake game using Scratch, a visual programming language perfect for beginners. Get ready to unleash your creativity and learn some awesome coding skills along the way. Let's dive in!

    Setting Up the Stage

    First things first, let's set up our stage. Think of the stage as the backdrop for our game. We need to make it look like a proper Snake game environment.

    Creating a New Project

    1. Open Scratch: Head over to the Scratch website (https://scratch.mit.edu) and click on "Create" to start a new project.
    2. Deleting the Default Sprite: You'll see a cat sprite by default. We don't need it for our Snake game, so right-click on it in the sprite list and select "delete."

    Designing the Game Board

    1. Choose a Backdrop: Click on the "Choose a Backdrop" button in the bottom right corner. You can either select a backdrop from the library or draw your own. A simple grid or a green background works great for a Snake game.
    2. Drawing a Grid (Optional): If you want a grid, use the paint editor to draw horizontal and vertical lines to create a grid pattern. This will help with the snake's movement later on.

    Adding the Snake Sprite

    Now, let's add our main character – the snake! We'll create a simple snake sprite using Scratch's built-in tools.

    1. Create a New Sprite: Click on the "Choose a Sprite" button and select "Paint" to create a new sprite.
    2. Drawing the Snake:
      • Use the rectangle tool to draw a small square. This will be one segment of our snake.
      • Choose a color for your snake. Green is a classic choice, but feel free to get creative!
      • Center the square in the paint editor. This is important for rotations and movement.

    Naming and Initializing the Snake

    1. Name the Sprite: In the sprite list, rename your sprite to "Snake."
    2. Initial Position: In the code area, drag the "when green flag clicked" block from the Events category. This starts the game when the green flag is clicked.
    3. Set Initial Position: Add a "go to x: () y: ()" block from the Motion category. Set the x and y coordinates to a starting position, like x:0 y:0, to place the snake in the center of the stage.
    4. Set Initial Direction: Add a "point in direction (90)" block from the Motion category. This makes the snake start moving to the right.

    With these initial steps, we've laid the groundwork for our Snake game. We've set up the stage, created the snake sprite, and positioned it at the starting point. This is where the real fun begins, so let's move on to making our snake move!

    Making the Snake Move

    Okay, now comes the exciting part – making our snake slither around the stage! We need to program the snake to move continuously and respond to our commands.

    Basic Movement

    1. Forever Loop: From the Control category, drag a "forever" block and attach it below the "when green flag clicked" block. This will make the code inside it run continuously.
    2. Move Steps: Inside the "forever" loop, add a "move (10) steps" block from the Motion category. Adjust the number of steps to control the snake's speed. A value of 5-10 usually works well.
    3. If on Edge, Bounce: Add an "if on edge, bounce" block from the Motion category. This prevents the snake from going off-screen and makes it bounce back.

    Controlling the Snake

    To make the game interactive, we need to control the snake's direction using the arrow keys.

    1. Turning Left:

      • From the Events category, drag a "when key pressed" block. Change the key to "left arrow."
      • Add a "point in direction (-90)" block from the Motion category below the "when left arrow key pressed" block. This makes the snake turn left when the left arrow key is pressed.
    2. Turning Right:

      • Repeat the above steps, but this time, change the key to "right arrow" and the direction to "90" (or right arrow). This makes the snake turn right when the right arrow key is pressed.
    3. Turning Up:

      • Repeat the above steps, but this time, change the key to "up arrow" and the direction to "0" (or up arrow). This makes the snake turn up when the up arrow key is pressed.
    4. Turning Down:

      • Repeat the above steps, but this time, change the key to "down arrow" and the direction to "180" (or down arrow). This makes the snake turn down when the down arrow key is pressed.

    Preventing Opposite Direction

    To prevent the snake from immediately reversing direction (which would cause it to collide with itself), we can add a few more checks.

    1. Check Direction:
      • Add an "if" block (from the Control Category) inside each of your arrow key press event handlers, before the "point in direction" block.
      • Use a "not" block (from the Operators Category) and an "equal to" block, to check against the opposite direction, before pointing in the new direction. For example, for the left arrow, you would check not direction = 90. For the Right Arrow, you would check not direction = -90. For the Up Arrow, you would check not direction = 180. For the Down Arrow, you would check not direction = 0.

    With these steps, your snake should now be moving around the stage and responding to the arrow keys! Play around with the speed and directions to get a feel for how it works. Now, let's add some food for our snake to eat!

    Adding Food

    What's a Snake game without food? Let's add a tasty treat for our snake to munch on. We'll create a new sprite for the food and program it to appear in random locations.

    Creating the Food Sprite

    1. Create a New Sprite: Click on the "Choose a Sprite" button and select "Paint" to create a new sprite.
    2. Drawing the Food:
      • Use the circle tool to draw a small circle. This will be our food.
      • Choose a color for your food. Red is a common choice, but any color will do.
      • Center the circle in the paint editor.

    Naming and Initializing the Food

    1. Name the Sprite: In the sprite list, rename your sprite to "Food."
    2. Initial Appearance: In the code area for the Food sprite, drag the "when green flag clicked" block from the Events category.
    3. Hide Initially: Add a "hide" block from the Looks category. We want the food to appear only when needed.

    Placing the Food Randomly

    1. Create a Custom Block: From the "My Blocks" category, click on "Make a Block." Name it "place food" and click "OK."
    2. Define the Block:
      • Inside the "define place food" block, add a "go to x: () y: ()" block from the Motion category.
      • For the x and y coordinates, use the "pick random () to ()" block from the Operators category. Set the ranges to cover the visible stage area (e.g., x: -200 to 200, y: -150 to 150).
      • Add a "show" block from the Looks category to make the food visible.
    3. Call the Block: After the "hide" block inside the "when green flag clicked" block, add the "place food" block from the "My Blocks" category. This places the food in a random location when the game starts.

    Making the Food Reappear

    We need the food to reappear in a new location after the snake eats it.

    1. Detecting Collision: In the code area for the Snake sprite, add a new "forever" loop.
    2. If Touching Food: Inside the loop, add an "if touching Food?" block from the Sensing category.
    3. Call the Place Food Block: Inside the "if" block, add a "broadcast (message1)" block from the Events category. Create a new message called "eat food".
    4. Make Food Reappear: In the code area for the Food sprite, add a "when I receive (eat food)" block from the Events category. Attach the custom block "place food" to this event.

    Now, the food should appear in a random location, and when the snake touches it, the food disappears and reappears in a new random spot. Great job! Let's move on to making the snake grow when it eats the food.

    Making the Snake Grow

    To make our Snake game more interesting, let's make the snake grow longer each time it eats the food. We'll do this by adding new segments to the snake's body.

    Creating Snake Body Segments

    1. Clone the Snake: When the snake eats food, we'll create a clone of the snake sprite to act as a new body segment. Go back to the Snake Sprite, and inside the if touching Food? block that already has the broadcast (eat food) block in it, add a create clone of myself block.

    Managing the Clones

    1. Clone Behavior: Add a when I start as a clone event handler, to define what happens to each clone.

    2. Initial Clone Setup: Set up the initial position of the clone to be the same as the original snake with a "go to x: () y: ()" block from the Motion category. Set the x and y coordinates the current x and y position of the original Snake sprite. Also, make sure you show the clone.

    3. Moving the Clones: Clones need to follow the original snake. Do this by creating a List Variable in the Variables Category. Name the list Snake Body. This is a list of the X,Y positions of the original snake.

    4. Adding to the Snake Body List: Before the move (10) steps block, add two blocks that add the current position of the snake to the Snake Body list. Use the add (item) to (list) block from the Variables category. Add the current X position with the x position block from the Motion Category, and the current Y position with the y position block from the Motion Category.

    5. Clone Movements: Inside the when I start as a clone event handler, create a forever loop. Inside that loop, set the clones X and Y position to the last X and Y positions of the snake. Do this by setting the X and Y position of the clone to the last two items in the Snake Body list, before deleting those list items. To do this, use the following:

      • go to x: (item (length of (Snake Body) -1) of (Snake Body)) y: (item (length of (Snake Body)) of (Snake Body))
      • delete (length of (Snake Body) -1) of (Snake Body)
      • delete (length of (Snake Body)) of (Snake Body)
    6. Start of Game: At the start of the game, you need to clear out the contents of the Snake Body list. Use the delete all of (Snake Body) at the top of the when green flag clicked event.

    With these steps, each time the snake eats the food, a new segment is added to its body, making it grow longer. Now, let's add the final touch – game over conditions!

    Adding Game Over Conditions

    No game is complete without a game over condition! We need to detect when the snake collides with itself or the edge of the screen and end the game accordingly.

    Detecting Self-Collision

    1. Check for Collision: In the Snake sprite's code area, inside the main "forever" loop (the one that controls the snake's movement), add an "if touching Snake?" block from the Sensing category.

    2. Broadcasting Game Over Message: Inside the "if touching Snake?" block, add a "broadcast (message1)" block from the Events category. Create a new message called "game over."

    Handling Game Over

    1. Stop the Game: Add a stop all block from the Control category to the when I receive (game over) event.

    Displaying Game Over Message

    1. Create a New Sprite: Click on the "Choose a Sprite" button and select "Paint" to create a new sprite.
    2. Drawing the Text: Use the text tool to write "Game Over" on the sprite.
    3. Initial Appearance: In the code area for the Game Over sprite, drag the "when green flag clicked" block from the Events category and add a "hide" block from the Looks category.
    4. Show the Message: Add a "when I receive (game over)" block from the Events category and a "show" block from the Looks category.

    Restarting the Game

    If you want to restart the game after it's over, you can add a button that resets the game when clicked.

    1. Create a New Sprite: Click on the "Choose a Sprite" button and select "Paint" to create a new sprite.
    2. Drawing the Button: Draw a button with the text "Restart" on it.
    3. Restart Script: In the code area for the Restart button sprite, drag the "when this sprite clicked" block from the Events category. Add a "broadcast (message1)" block from the Events category. Create a new message called "restart."
    4. Restart Game Event: In the code area for all sprites, use the "when I receive (restart)" block from the Events category. You will need to re-initialize all the game settings, like the direction and position of the snake, hide/show the sprites, and clear the Snake Body list.

    Congratulations! You've successfully created a Snake game in Scratch. You can now share it with your friends and family, and even add your own creative twists to make it even more fun. Keep experimenting and happy coding!