How to Make a Platformer on Scratch
Introduction: How to Make a Platformer on Scratch
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
Step 1: Create Sprites
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.
Step 2: Gravity
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. 149
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.
Step 3: Movement
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
Step 4: Horizontal Collision Detection
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. 149