How to make a platformer
id: 751581
category: Help with Scripts
posts: 3
SCRATCH-GUIDE0
loading
This article is about how to make a basic platformer. For a more advanced physics tutorial, see Advanced Platformer Physics. For scrolling platformers, see Scrolling Platformer Tutorial.
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.
Contents
1 Creating the Platformer Sprite
2 Walking
3 Jumping
4 Making Levels
5 Making the Win Background
6 See Also
7 References
Creating the Platformer Sprite
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.
Below is an example of a simple script for a platformer sprite. It uses two variables:
“X velocity” stores a value representing the sprite's horizontal speed. It was set as a local variable by checking the option “For this sprite only” in the creation dialog. This means (i) the variable can only be changed by scripts in the same sprite, (ii) the variable name does not needlessly clutter the variable pane of other sprites, and (iii) the same variable name may be used in other sprites without causing conflicts.
“Gravity” stores a value reflecting the strength of the sprite's tendency to fall. In this example it is set as a negative number because moving a sprite downwards requires making the value of its Y position smaller. “Gravity” need not be set as a local variable; a realistic game would subject all its characters to the same gravitational force.
whenclicked
set
gravityto
-5
forever
if
key
left arrowpressed?then
set
x velocityto
-4else
if
key
right arrowpressed?then
set
x velocityto
4else
set
x velocityto
0
no arrow keys means no movement
using “else” saves processing later ifs unnecessary
if
not
touching
ground sprite?then
changeyby
gravity
sprite falls till touching ground
changexby
xvelocity
Walking
Here is the code for platformer walking.
whenclicked
forever
if
key
apressed?then
change
velocityby
-1
if
key
dpressed?then
change
velocityby
1
speedcap
5
changexby
velocity
define
speedcap
maxspeed
if
velocity>
maxspeedthen
set
velocityto
maxspeed
if
velocity<
maxspeed*
-1then
set
velocityto
maxspeed*
-1
set
velocityto
velocity*
0.9
Jumping
To jump, use this code:
whenclicked
forever
if
key
up arrowpressed?then
repeat
10
changeyby
15
repeatuntil
touching
ground sprite
changeyby
-5
the jumping key
Making Levels
Colors can be used in a platform for detection of the end of a level or an object which sends one back to the beginning of the level. For this tutorial, assume the following:
The character sprite performing the physics is named “Player”
Black is the color of the platform, or ground and walls, in which the character cannot pass through
Red is the color that sends one back to the beginning of the level they are on
Yellow is the color which must be reached to move on to the next level
Backgrounds are used as levels instead of sprites
Scrolling is not incorporated
Level.png
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:
Alt Level.png
When the levels are finished, add the following script to the “Player” sprite:
whenclicked
forever
if
touchingcolor
?then
gotox:
-180y:
-47
relocate to the start
if in contact with the color red
if
touchingcolor
?then
gotox:
-180y:
-47
relocate to the start
switchbackdropto
next backdrop
next level
if at the end of a level
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.
Note Note: A condition is a statement that is checked for a true/false response. In the example above, when the sprite checks if it's touching a color, it's checking a condition.
Then add the following script to any sprite:
whenclicked
switchbackdropto
level 1
begin with the first level
Lastly, add the following script to the “Player” sprite:
whenclicked
show
waituntil
backdrop #of
Stage=
amountofbackdrops
wait until the last level is reached
stop
all
Making the Win Background
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.
A basic You Win background with just some black text and a white background
A basic You Win background with just some black text and a white background
A You Win background with complex art
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.
Contents
1 Creating the Platformer Sprite
2 Walking
3 Jumping
4 Making Levels
5 Making the Win Background
6 See Also
7 References
Creating the Platformer Sprite
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.
Below is an example of a simple script for a platformer sprite. It uses two variables:
“X velocity” stores a value representing the sprite's horizontal speed. It was set as a local variable by checking the option “For this sprite only” in the creation dialog. This means (i) the variable can only be changed by scripts in the same sprite, (ii) the variable name does not needlessly clutter the variable pane of other sprites, and (iii) the same variable name may be used in other sprites without causing conflicts.
“Gravity” stores a value reflecting the strength of the sprite's tendency to fall. In this example it is set as a negative number because moving a sprite downwards requires making the value of its Y position smaller. “Gravity” need not be set as a local variable; a realistic game would subject all its characters to the same gravitational force.
whenclicked
set
gravityto
-5
forever
if
key
left arrowpressed?then
set
x velocityto
-4else
if
key
right arrowpressed?then
set
x velocityto
4else
set
x velocityto
0
no arrow keys means no movement
using “else” saves processing later ifs unnecessary
if
not
touching
ground sprite?then
changeyby
gravity
sprite falls till touching ground
changexby
xvelocity
Walking
Here is the code for platformer walking.
whenclicked
forever
if
key
apressed?then
change
velocityby
-1
if
key
dpressed?then
change
velocityby
1
speedcap
5
changexby
velocity
define
speedcap
maxspeed
if
velocity>
maxspeedthen
set
velocityto
maxspeed
if
velocity<
maxspeed*
-1then
set
velocityto
maxspeed*
-1
set
velocityto
velocity*
0.9
Jumping
To jump, use this code:
whenclicked
forever
if
key
up arrowpressed?then
repeat
10
changeyby
15
repeatuntil
touching
ground sprite
changeyby
-5
the jumping key
Making Levels
Colors can be used in a platform for detection of the end of a level or an object which sends one back to the beginning of the level. For this tutorial, assume the following:
The character sprite performing the physics is named “Player”
Black is the color of the platform, or ground and walls, in which the character cannot pass through
Red is the color that sends one back to the beginning of the level they are on
Yellow is the color which must be reached to move on to the next level
Backgrounds are used as levels instead of sprites
Scrolling is not incorporated
Level.png
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:
Alt Level.png
When the levels are finished, add the following script to the “Player” sprite:
whenclicked
forever
if
touchingcolor
?then
gotox:
-180y:
-47
relocate to the start
if in contact with the color red
if
touchingcolor
?then
gotox:
-180y:
-47
relocate to the start
switchbackdropto
next backdrop
next level
if at the end of a level
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.
Note Note: A condition is a statement that is checked for a true/false response. In the example above, when the sprite checks if it's touching a color, it's checking a condition.
Then add the following script to any sprite:
whenclicked
switchbackdropto
level 1
begin with the first level
Lastly, add the following script to the “Player” sprite:
whenclicked
show
waituntil
backdrop #of
Stage=
amountofbackdrops
wait until the last level is reached
stop
all
Making the Win Background
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.
A basic You Win background with just some black text and a white background
A basic You Win background with just some black text and a white background
A You Win background with complex art
Paddle2See
loading
It's really nice that you want to help out with a guide! However, we have a number of them in the forums already and we don't have a good way to showcase any more. If you want to make a guide in a Scratch project, that would be fine. You could advertise it in the “Show and Tell” section too.
There are lots of other ways to help out in the forums too. Helpful Scratchers are always welcome in the “Questions About Scratch”, “Help With Scripts”, “Requests” and other sections. Just look around and ask questions
There are lots of other ways to help out in the forums too. Helpful Scratchers are always welcome in the “Questions About Scratch”, “Help With Scripts”, “Requests” and other sections. Just look around and ask questions