Tuesday, September 15, 2015

Making a Basic Platformer

This post will walk you through the basics of making a platformer with Scratch.


THE NECESSARY STUFF

Start by drawing the costumes for your level(s):
My levels.



Next, make the player sprite:
The sprite I am using. You can make yours however you want.





NOW FOR THE CODING
First, make the variables you'll be using for the project. You need one for x Velocity and one for Gravity:

(xvel)
(gravity)

Next we put the gravity variable to use. If you want to go a bit on the lazy side you can have it continually change y by some negative value as long as it's not touching the color you draw your platforms in, but that just doesn't look right. To make realistic gravity, you need the falling object to accelerate. Here is the code I wrote for this:
when flag clicked forever if (not (touching color [#009933])) then change [gravity v] by [-0.2] end end when flag clicked forever change y by (gravity) end
Now we need to make some jumping script. You can make your sprite jump as high as you want depending on how high your platforms are. The way this script works is it sets gravity to a positive value to go up, and since the player is no longer touching the platform sprite, the gravity will slow it down until it eventually comes to a stop, then moves it downwards until it is toughing the platform sprite.
when flag clicked forever if (touching color [#009933]) then set [gravity v] to [.1] set [gravity v] to [0] if (key [up arrow v] pressed?) then set [gravity v] to [4.5] end end end
Next we need to add horizontal movement. To do this, we will make an acceleration system similar to that we used for gravity.
when flag clicked point in direction (90 v) forever if (key [right arrow v] pressed?) then change [xvel v] by (1) end if (key [left arrow v] pressed?) change [xvel v] by (-1) end set [xvel v] to ((0.75) * (xvel)) change x by [ end

FINISHING UP

Now we need to make the level change when the player touches a sprite. You ca make yours however you want: a flag, a wall on the right of the screen, a portal, or anything else you want. I've made a flag for mine:


Now let's make some script for the level changing:
when flag clicked go to x: (253) y: (-133) go to front forever if (touching [player v] ?) then broadcast [nextlvl v] end end

Put this script in your flag/whatever your end sprite is....

When I receive [nextlvl v] next costume

FINAL TOUCHES

Now we need to make it so the player can't go through the ground when they jump up and move horizontally. Start by making the sprites:
Now for the scripting:
UP Script

LEFT Script

RIGHT Script
NOTE: Make sure you draw the levels in the same color you use in all the < touching color [] > s.

And you're done!
I hope you liked this tutorial and that it was useful to you. Thanks!
 -Erick

No comments:

Post a Comment