How to Make a Platformer with Ground Detection in Scratch | Platformer Tutorial Part 2

Alphabets Sounds Video

share us on:

In this lesson, Zenia teaches how to create a platformer game in Scratch, focusing on ground detection and character interactions. Students learn to implement ground collision by using conditional blocks to prevent the character from falling through the ground and to restrict jumping to when the character is on the ground. The lesson also emphasizes organizing code for clarity and preparing for future enhancements like wall jumping and danger areas.

How to Make a Platformer with Ground Detection in Scratch

Hey everyone, it’s Zenia here! Today, I’m excited to show you how to make a platformer game in Scratch. In this part, we’ll learn how to make our character interact with the ground and jump off platforms. In the next part, we’ll explore wall jumping, danger areas, and multiple levels. Let’s dive into ground collision!

Fixing Ground Collision

In the first part, we created a character that can move left and right and jump. However, it can fall through the ground and jump in midair. Let’s fix these issues. We want our character to stop falling when it hits the ground. To do this, we’ll use an “if” block from the control category. We’ll check if the character is touching the ground color using the “touching color” block from the sensing category. Use the eyedropper tool to select the ground color in your game.

When the character hits the ground, we need to stop it from falling. Remember, when the character is falling, its Y speed is negative. So, we’ll set the Y speed to zero to stop it from falling. Drag out “set Y speed to zero” from the variables category.

Adjusting Character Position

Sometimes, the character might still go slightly into the ground. This happens because it falls a little before detecting the ground. To fix this, we need to move the character up a bit. Try adding a “change Y by 10” block to see if that helps.

However, changing Y by 10 isn’t always accurate. Instead, let’s adjust it correctly by changing Y by negative Y speed. This will move the character up by the same amount it went down. For example, if the character’s Y speed is 8, it will go down by 8 and then move up by 8, getting it out of the ground.

Testing and Organizing Code

Let’s test it out! Click the green flag, and now you can jump around and collide with the ground. It looks great! Next, let’s make sure the character can’t jump from midair. We’ll organize our code into blocks for clarity.

Create a block for jumping and call it “jump.” Place it after the character moves left and right. Group the two blocks handling gravity under a block called “apply gravity.” Also, create a block called “check for the ground” and add it to the main loop. Now everything is organized!

Preventing Midair Jumps

Currently, the character can jump anytime the up arrow key is pressed. Let’s change that so the player can only jump if the up arrow key is pressed and they’re touching the ground color. Use an “and” block from the operators category to check both conditions.

Test it out! If the character isn’t jumping, it might not be touching the ground color. To fix this, create a new block called “let the player jump from the ground” and add it to the main loop. This block will slightly dip the character into the ground to check for contact and then move them back up.

Duplicate the jump code here and allow the character to jump if they’re on the ground. Adjust their position back to where they were. Test it again! Now you can jump off the ground but not while in midair, which is what we wanted.

Exploring Platforms

Let’s see how it works with some platforms. You can jump around and land on them. However, there are a couple of issues, like being able to go right through walls. We’ll address these in part three, along with adding danger areas and multiple levels.

For now, enjoy this stage of your platformer, and I’ll see you next time. Keep creating with Scratch!

  1. What was the most challenging aspect of implementing ground collision in your Scratch platformer, and how did you overcome it?
  2. How did organizing your code into blocks, such as “jump” and “apply gravity,” help you manage the complexity of your project?
  3. Reflect on the process of preventing midair jumps. What did you learn about using logical operators like “and” in Scratch?
  4. How did testing your game after each change help you identify and fix issues with ground detection and jumping?
  5. What strategies did you use to ensure that your character’s position was accurately adjusted when interacting with the ground?
  6. How do you plan to address the issue of characters passing through walls in the next part of your platformer project?
  7. What new insights did you gain about game design and development through creating a platformer in Scratch?
  8. How do you think the skills you learned in this project could be applied to other programming or game development tasks?
  1. Create Your Own Platformer Character

    Design a unique character for your platformer game in Scratch. Use the costume editor to draw your character and make sure it has a clear outline to help with ground detection. Once you’re done, share your character with the class and explain your design choices.

  2. Experiment with Ground Collision

    Use the “if” block and “touching color” block to detect when your character is on the ground. Try changing the ground color and see how it affects the game. Discuss with your classmates why accurate ground detection is important in a platformer game.

  3. Organize Your Code with Blocks

    Create custom blocks in Scratch to organize your code. Make blocks for “jump,” “apply gravity,” and “check for the ground.” This will help you keep your code neat and easy to understand. Share your organized code with a partner and explain how each block works.

  4. Test and Troubleshoot Midair Jumps

    Ensure your character can only jump when touching the ground. Use the “and” block to check if the up arrow key is pressed and the character is on the ground. Test your game and troubleshoot any issues with jumping. Discuss with a friend how you fixed any problems.

  5. Design and Test Platforms

    Create different platforms in your game and test how your character interacts with them. Experiment with different heights and distances between platforms. Share your game with the class and get feedback on how the platforms affect gameplay.

Here’s a sanitized version of the provided YouTube transcript:

Hey everyone, it’s Zenia here! Today, I wanted to show you part two of how to make a platformer in Scratch. In this part, I’ll demonstrate how to add collision with the ground and how to make the character jump off platforms. Then, in part three, I’ll cover wall jumping, danger areas, and multiple levels. So, let’s get started with collision!

In part one, we created a character that can move left and right and jump, but it can fall through the ground and jump in midair. Let’s fix those issues. We want our character to perform an action when it hits the ground. To do this, I’ll go to the control category and drag out an “if” block. Then, to test whether we’ve hit the ground, we can check if we’ve touched the ground color. I’ll go to the sensing category and drag out the “touching color” block, then use the eyedropper to select the ground color in our game.

Now, what should the character do if it hits the ground? If you remember from the last part, when it’s falling, the Y speed is negative. So, when it hits the ground, we should set the Y speed to zero to stop it from falling. I’ll go to the variables category and drag out “set Y speed to zero.” Let’s give that a try.

It’s not quite working; my Y speed goes to zero, but I’m still slightly going into the ground. This might be because I fell into the ground a little before detecting it. So, when we hit the ground, we not only need to set the Y speed to zero but also move the character up a bit to get it out of the ground. I’ll try adding a “change Y by 10” block to see if that helps.

Now, the character isn’t going into the ground, but changing Y by 10 isn’t always accurate. How can we adjust it correctly? We know that at this point in the code, the character just changed Y by Y speed, which made it go down. So, let’s change Y by negative Y speed to move it up by the same amount it went down. For example, if the character is falling and its Y speed is 8, this block will make it go down by 8, and this block will move it up by 8, getting it out of the ground.

Let’s test it out! I’ll click the green flag, and now I can jump around and collide with the ground. That looks great! Now, let’s ensure the character can’t jump from midair, so the player needs to jump on platforms. Before that, I’ll organize my code into blocks for clarity.

I’ll create a block for jumping, call it “jump,” and place it after the character moves left and right. The two blocks handling gravity will be grouped under a block called “apply gravity.” I’ll also create a block called “check for the ground” and add it to the main loop. Now everything is organized!

Next, let’s prevent the character from jumping while in midair. Currently, the character can jump anytime the up arrow key is pressed. Let’s change that so the player can only jump if the up arrow key is pressed and they’re touching the ground color. I’ll drag out an “and” block from the operators category and set it up to check both conditions.

Let’s test that! I’m pressing the up arrow key, but it’s not letting me jump. The character may not actually be touching the ground color. To fix this, I’ll create a new block called “let the player jump from the ground” and add it to the main loop. This block will give the player another chance to jump by slightly dipping them into the ground to check for contact and then moving them back up.

I’ll duplicate the jump code here and allow them to jump if they’re on the ground. After that, I’ll adjust their position back to where they were. Let’s test it out! Now I can jump off the ground, but I can’t jump while in midair, which is what we wanted.

Let’s see how it works with some platforms. I can jump around and land on them. However, there are a couple of issues, like being able to go right through walls, which we’ll address in part three, along with adding danger areas and multiple levels.

For now, I hope you enjoy this stage of your platformer, and I’ll see you next time. Scratch on!

This version removes informal language and any unnecessary filler while maintaining the instructional content.

PlatformerA type of video game where the player controls a character to jump between different platforms or obstacles. – Mario is a famous platformer game where you have to jump over obstacles to reach the end of the level.

ScratchA programming language designed for beginners to create interactive stories, games, and animations using blocks. – In computer class, we used Scratch to make a simple game where a cat chases a mouse.

GroundThe surface or base in a game where characters can walk or stand. – In the game, if the character falls off the ground, you have to start the level again.

JumpA movement in games where a character moves upwards to avoid obstacles or reach higher platforms. – Press the spacebar to make your character jump over the gap.

CharacterA person or figure that a player controls in a video game. – You can choose your favorite character to play as in the game.

SpeedHow fast a character or object moves in a game. – You can increase the speed of the car to win the race faster.

CodeA set of instructions written in a programming language that tells a computer what to do. – We wrote code to make the robot move forward and turn left.

CollisionAn event in a game where two objects hit each other. – If there is a collision between the character and an enemy, the character loses a life.

TestThe process of running a program to check if it works correctly. – After writing the code, we need to test the game to make sure there are no bugs.

PlatformsFlat surfaces in a game that characters can jump on or off. – The platforms in the game are tricky, and you have to time your jumps perfectly.

All Video Lessons

Login your account

Please login your account to get started.

Don't have an account?

Register your account

Please sign up your account to get started.

Already have an account?