PSP Lua Snippets - Jumping

Making a character jump can be one of the hardest things to learn how to do. That's what we're gonna learn here. We will make a simple character and ground, and make the character be able to move left and right, as well as jump. Let's get started.

Our first bit of code creates some color objects to use for printing later. You won't have to use these to jump, but I like to see what's going on when I'm putting code together.

--Create colors
green=Color.new(0,255,0)
white = Color.new(255,255,255)

The next section of code will create two blank images, and clear the color of the images to green and white. One image will be our character, the other our ground. The numbers within the parentheses are the size of the image in pixels (width,height).

player1 = Image.createEmpty(32,32)
player1:clear(white)

ground = Image.createEmpty(480,10)
ground:clear(green)

Now, we will make an array for our player. We will make some types for the array below as well. Look at the code, then I'll explain.

player = {}
player.gravity = 230
player.y = 230
player.x = 50
player.jumpspeed = 10
player.jumpstate = "ground"

player.gravity will store the gravity of our player.
player.y is the player's vertical position on the screen.
player.x is the player's horizontal position on the screen.
player.jumpspeed will be the speed our player jumps. This will also be used for the speed of falling back down.
player.jumpstate tells us what state the player is in right now. We start on the ground. We will later use jumping and falling.

Next, we will start our main loop, and put in some code to move our player left and right. If left is pressed then the player's x position will be decreased by 2, and if right is pressed it will be increased by 2.

while true do
pad = Controls.read()
screen:clear()


if pad:left() then
player.x = player.x - 2
end
if pad:right() then
player.x = player.x + 2
end

Now we're getting to the good stuff!
Check out the line below.

if pad:cross() and player.jumpstate == "ground" then player.jumpstate = "jumping" end

This code simply says that if the X button is pressed and we are on the ground change the state to jumping.

Here's the next part.

if player.jumpstate == "jumping" then
player.jumpspeed = player.jumpspeed - 0.5
player.gravity = player.gravity - player.jumpspeed
end

This is saying that if we are in a state of jumping then subtract 0.5 from our jumpspeed. This means our jump speed will get a little slower every time the loop performs. This will cause our player to move slower as he goes farther into the air. This will make for a more realistic jump.
The last line subtracts the jumpspeed from the player's gravity.

Let's see the next part.
if player.gravity < 0 then
player.jumpstate = "falling"
end

This code means that if our player's gravity goes below 0 then set our player's state to falling. When this is set he will no longer rise up in the air. He will start falling, once we code him to of course!

Next part...

if player.gravity < 230 and player.jumpstate == "falling" then
player.gravity = player.gravity + (player.jumpspeed + 3)
end

This means that if our gravity is less than 230 and our player is falling then increase the gravity by the jumpspeed plus 3. This will add 3 to gravity each loop, making you fall quickly to the ground.

Here we go again... next part!
if player.gravity == 230 then
player.jumpspeed = 10
player.jumpstate = "ground"
end

if player.gravity > 230 then player.gravity = 230 end

This code means that if our gravity is at 230 (ground level!) then set our jumpspeed back to 10, and change our state to ground. Our jumpspeed has to be set back so that we can use it to jump again.
The last line is a safeguard. If our gravity goes to anything over 230, then it will get set back to 230. This is our ground level so we should not be able to go past it.

Now, we need to set our player's vertical position to the gravity. Each loop the y of the player will get set to player.gravity. Here's the code.

player.y = player.gravity

Let's finish up our code with some simple stuff.
First, we'll paste our player and ground image to the screen.
Then we'll print the player's x and y position on the screen, as well as the player's jumpstate.
Then we end with the usual goodies.

screen:blit(player.x,player.y,player1)
screen:blit(0,262,ground)

screen:print(10,10,"X: "..player.x.." Y: "..player.y,green)
screen:print(10,20,"Jumpstate: "..player.jumpstate,green)

screen.waitVblankStart()
screen.flip()
end

Try it out! Left and right to move, and space to jump. Play with the numbers if you want to see how it affects the jumping.


Back To Snippet List

Please welcome anthonyjee, our newest member.

Who's Online:

Total Members: 522
Total Posts: 13085
Total Topics: 1515
Total Categories: 7
Total Boards: 42

Recent Posts:

Re: Hello every body... by DeniseVera
Need help please by Robbynator
Re: Hello every body... by horvathann
Re: background criminal record check by backgroundchecker
background criminal record check by backgroundchecker
Intro to Perl Part 2: What are Scalars? & Examples by Chi Kitory
Intro to Perl Part 1 About Perl and Hello World by Chi Kitory
Re: Trying To Get Rid of the Spammers by Charlie


Copyright © 2006-2009 www.EvilMana.com All rights reserved.
EvilMana Logo by emcp and Charlie