How to Make a Platformer on Scratch

by mmckenzie20 in Circuits > Software

36981 Views, 36 Favorites, 0 Comments

How to Make a Platformer on Scratch

instructable final.png

Scratch is a website where people create games and other programs using premade blocks that you drag and drop to a work space. Today I will show you how to make a platforming game on scratch.

Supplies

The only thing you will need is a phone or a computer and a browser that can run scratch

Create Sprites

instructabl 2.png

Start by logging in to scratch, this is needed so if you want to publish or come back to the game. Then click on create and it will put you on the project editor. This is were we will be making our project.

On the top left, you will find the costumes button. Click it if you want to change your player. Rename "sprite" to "player."

Then create your platforms by making a new sprite. Rename that sprite "ground."

Rename your game to whatever you want.

Gravity

instructable 3.png

Add a "when green flag is clicked." Then Place a "set position" block and put in the coordinates on where you want your player to spawn. Get a "forever" loop and a "repeat until" loop place the "forever" loop under the "set position" block.

Create a variable. This will act as our gravity. Mine was name "y vel" (short for y velocity). Right underneath the "set position" block, place a "set variable to ___." Change the Variable to whatever you named your variable (I will be calling it y vel now) and change the number to zero. Next add a "change y by ___," and put a "y vel" in it. Put that in the "forever loop." Below that add a "change 'yvel' by '-1.'"

Create a block called Touch ground. MAKE SURE WHEN DOING THIS YOU CLICK RUN WITHOUT SCREEN REFRESH.

Insert a "repeat until" loop below the "touch ground". Add a "not" in the Boolean and in the "not" add a "touching 'Ground.'" In that put a "change y by '1.'" Underneath that put a "set 'y vel' to '0.'"

If you followed all of that, your code should look like the picture above.

Movement

instructable 4.png

Underneath the "when green flag is clicked," put a "set rotation style 'left-right.'"

Inside the "forever' loop add two "if then blocks." In the Boolean add two "key ___ pressed." Set it to what keys you want to go left and right.

Make a block called "x detection" (it will help in next step). Click run without screen refresh. Add an input, call it "speed."

In the right direction, put "detection x '5 (how fast you want your sprite to move),'" (speed depends on what you want), and "point in direction '90.'" Do the same for the left but multiply all the numbers by negative one.

Make a block called jump. Put it in the "forever' loop.

Create a new variable called "falling." Under "touch ground," place a "change 'falling' by '1.'" Under that, in the "repeat until" loop, place a "set 'falling' to 'zero'"

Define jump. Add an "if then" block. In the Boolean put a "_____ and ____." In one Boolean, put "key 'what your jump key will be' pressed," then add "if '5' is greater than 'falling.'" In the "if then" block, put "set 'y vel' to '12 (jump height.'"

You may notice that your player is not always touching the ground. Make a new costume, make it smaller on all sides. Inside the "forever" loop put in "switch costume to 'costume2 (new costume).'" Below that, put a "switch costume to 'costume1 (original costume).'"

If you have done this correctly, this should look like the picture above

Horizontal Collision Detection

instructable 5.png

Create a new variable called slope. Underneath "change x by 'speed,'" add a "set slope to '0.'"

Add a repeat until block underneath that. Put a "'touching 'ground' or 'slope' equals '8.'" In that add a "change y by '1,'" and "change slope by '1.'" Under the "repeat until" loop, add a "if 'slope' equals '9' then," loop. In that add a "change x by '0' minus 'speed,'" then add a "change y by '0' minus 'slope.'"

If you did this step correctly, your code should look like the picture above.

Vertical Collision Detection

instructable 6.png

Edit the block "touch ground" add a Boolean, call it "ceiling." In our "touch ground" in the "forever" loop add "'y vel' is greater than '0."

Take everything out of the "repeat until" loop. Put a "if then else" loop in the "repeat until" loop and add "ceiling" in the Boolean. In the "if then" part, add "change y by '-1.'" In the "else" part, add "change y by '1,'" and "set falling '0.'"

Below the "if then else" loop, add a "set 'y vel' to '0.'"

If you followed instructions, it should look like the picture above

Extra

instructable final.png

Create a new sprite. This will contain the extras.

In the forever loop, Get an "if 'touch color red (you must get the exact color)' then" loop. In that put a "set position" block. Enter in the coordinates where your player spawns. Get an "if 'touch color blue' then" loop. In that put a "set position" block. Enter in the coordinates where your player spawns. In it as well broadcast "message1"

In both ground and extras, get "when I receive 'message1,'" and put "next costume."

You now have the skills to make a basic platformer. Add to it.

For more help, click the link. He does everything this talks about, this is his code.

https://scratch.mit.edu/projects/68924432/