Scratch 2 Player Platformer

0 views
Skip to first unread message

Karina Edling

unread,
Aug 3, 2024, 3:35:46 PM8/3/24
to ponnaithoders

This article covers a step-by-step process about how to make a Scrolling Platformer. A scrolling platformer is a type of platformer where the camera follows the player as they move through the level(s).

Often, Scratchers make basic platformers before they make a scrolling platformer, and they may want to use their old script to make a scrolling platformer. To do this, the "ground" sprite would move with the player. Making a script to do this is simple:

A platformer is a simulation of actual physics that take place in real life. Objects fall, move, slide, jump, and bounce, and a platformer associates those properties into a game in which one controls a character and tries to move it toward a goal. This tutorial will explain how to make a basic platformer.

The platformer sprite is the avatar controlled by the player. Its appearance can affect gameplay slightly, depending on the angles and size of its Costumes. For example, a character shouldn't be saved from a fall because the brim of her hat snagged on the edge of a cliff. Sprites that are animated by lots of costume changes are even more tricky, as a changing costume might get pulled inside the ground and get stuck.

The shapes do not need to be geometric, but can be organic, meaning an unordinary, inconsistent structure. There can be curvature to the various colors and platforms, which can be used to create diverse, numerous levels. The following image displays an example of some organic shapes being used:

The scripts within the "forever" loop can be merged with the larger physics script shown farther above. Merging the scripts reduces the amount of conditions being checked at once and can possibly make the project more uniform and orderly, meaning the "Player" makes each movement and then checks for the conditions instead of the conditions possibly being checked during the sprite's movement.

Last of all, comes the win background. After finishing all the levels in the platformer, something would come up that says something like "You Win!". Put it as the last costume in the sprite/background. It can be some text in a basic white background saying "You win" or the art can be complex.

In this tutorial, we're going to learn how to make a Mario game in Scratch. Super Mario Bros, released in 1985, is arguably one of the most popular video games of all time, and the poster child of not only the entire Mario franchise, but, to a degree, Nintendo as a whole. Super Mario Bros belongs to an extremely popular genre of arcade games: the Side-Scrolling Platformer.

'Platformer' refers to the way a player must navigate obstacles in any given level of the game, often by literally jumping on platforms. And a true 'Side-Scroller' is defined by the way the player continuously reveals new parts of a level by moving their character towards the right side of the screen.

Another common version of a Side-Scroller is one in which a level (or 'world,' to use Mario terminology), is broken up into parts, and to get to a new part, you must reach the end of the previous part. This is the type of Side-Scroller we'll be building today!

We'll also focus on building a game that will be easy to expand on. When game developers build a game, they often first build a game 'engine'. The engine is sort of like the bare bones of the game; it determines graphics, and how sprites move and interact.

Super Marios Bros 2 was released in 1988, just three years after the original. Nintendo didn't build a whole new game from scratch, they simply expanded on the first game. They added new sprites and levels, some new features as well, but the gameplay was very similar. With a good game engine, you can build countless games that will all be fun and easy to build! We'll look at some strategies in this tutorial to make our Scratch coding game easy to build on.

To create even more cool games, join our award-winning Scratch classes (Scratch Ninja for Grades 2-5 and Accelerated Scratch for Grades 5-9), designed by professionals from Google, Stanford, and MIT. There's no risk in trying with our free intro Scratch class.

To create our Mario game today, we'll be using a lot of broadcasting and messaging, and a lot of X and Y positions. We'll also learn some advanced techniques for creating platformers, and focus on keeping our game easy to edit and add to. Here's the Finished Project.

You can absolutely follow along with this tutorial by creating your own Scratch project, but if this is your first platformer, we recommend remixing our Starter Project, which has all of the Sprites and Costumes already put together and sized correctly. We'll still need to write all the code though!

We're going to start building this game by getting our images and backgrounds in order. Let's create a new backdrop called 'Assets'. We won't actually use this backdrop, but it will contain all of our custom drawings we'll use for sprites and creating backgrounds. It will be easier to build our game if we have everything in one place. Games that are built on the same game engine will often use either the same assets, or very similar ones.

Let's create a new sprite called 'Backgrounds'. Each costume will be another level of our world, and we can create each level by copying and pasting pieces from our Assets backdrop. This is how to make a world in Mario Scratch games: get the building blocks put together in your assets section, and copy and rearrange them to build each level. You can build as many levels as you want, but we're using five for this tutorial.

Our Backgrounds sprite will be responsible for moving us through the world, and also telling our other sprites when they need to get in place for each level. Each level will have its own ? Blocks, Coins, and Enemies. We'll broadcast Delete Clones and Set Level to make sure everything resets correctly between levels. This will happen when the green flag is clicked, and also when we go to the next Backgrounds costume.

You may have noticed we left some space in between blocks when we created our levels. We're going to fill some of those in with ? Blocks. These blocks behave differently than normal background blocks, so they need to be their own sprite. When we set a level, we're going to move our ? block sprite everywhere we want one, and clone it. Where we move it depends on what level we're on.

In your project, for each level, drag your ? Block sprite where you want it, and take note of its X and Y position. That's the position we'll tell it to go to on that level, and clone itself. We'll use a strategy like this to place our Coins and Enemies later as well.

In order to move Mario around our level, he needs to be able to detect walls, floors, and ceilings. We shouldn't be able to go forwards if there's a pipe in front of us, or jump through a block. We can't just say, 'if touching Background, don't move'; if the front of Mario is touching a wall, he can't move forwards, but he can still move backwards, and jump up.

We'll create four sprites, thin lines that follow Mario's location, and these sprites will let us know information about Mario's surroundings. Those sprites will set variables that can tell us if he's able to move right, left, up, or down. For example, if the 'Right' sprite is touching the 'Background' sprite, then we'll set 'Right-Touching' to 1, and we'll know Mario has something in front of him. If 'Right-Touching' is 0, then Mario has nothing in front of him.

Mario should move Left and Right, be able to Jump when he's standing on something, and fall down when there's nothing below him. We'll use our detection variables to help us out here. We also want Mario to be animated when he moves, and make sure he starts Level 1 standing in the right spot, facing the right direction.

If you're wondering how to make a scrolling platformer on Scratch, one method is to move to the next level whenever the player sprite gets to the end of the previous one. We do this by forever checking the X position of our sprite, and broadcasting the 'Next Screen' message when they touch the right edge. Another method would be to make the entire 'world' one big costume in the Backgrounds sprite, and move the X position of that sprite to the left as our player moves forward. The latter method creates a cool effect, but makes editing your game and adding to it much more complicated.

There's a lot of code here, and more to add later, so it's a good idea to use comments to help keep us organized. You can add a comment to any code block by right clicking on it. Comments are extremely important when you have a lot of code, especially if it's complex. Comments should be short and descriptive. Fixing bugs in code will go a lot quicker if you can quickly see what chunk of code is responsible for what.

We'll introduce a score variable now. In each level, let's add some floating coins, much in the same way we added the ? Blocks. Paint a new sprite and create its costume by copying the Coin from our Assets backdrop. We'll move the sprite where it should be in that level, and create a clone of it. When that clone is created, we'll forever check and see if Mario touches it, and if he does, we can add a point to the score. Having them glide just a little bit up and down will create a nice floating animation.

In the original Super Mario Bros, hitting a ? Block could give you either a coin or a power-up. We'll do something similar for our game. Most of the time, a ? Block will have a coin that will add to our score, but sometimes, it will have a star that we can pick up and get temporary invincibility from. Invincibility will protect us from the Enemies we'll add later, and we'll code that part in the next step.

Let's add code to our ? Block first. When Mario jumps into it, we'll switch its costume, move it up and down a little, choose which prize to give, and save the position of the block. The ?-Block-X and ?-Block-Y variables will be responsible for telling our new Prize Star and Prize Coin sprites where to go.

Now we can create the Prize Star Sprite and Prize Coin Sprite. When these sprites receive a message, they'll go to the ? Block that was just hit and appear. If it's a coin, it will disappear and add to the score. If it's a star, it will wait for Mario to pick it up, and broadcast 'Invincible' when he does.

c80f0f1006
Reply all
Reply to author
Forward
0 new messages