
-----------------------------------
Mazer
Sat Dec 22, 2007 10:46 am

[Tutorial] How to Jump
-----------------------------------
If your game involves any kind of movement you should already be keeping track of the following variables:
 players x and y position
 players x and y velocity
It seems as though alot of you forget about the second one. Go ahead and add it now.
Now then... % some constants, tweak this to your liking
const GROUND_HEIGHT := 120
const RUN_SPEED := 10
const JUMP_SPEED := 30
const GRAVITY := 2

var win := Window.Open ("graphics:640;480,offscreenonly")
var chars : array char of boolean

% player position and velocity
var posx, posy : int
var velx, vely : real

posx := 20
velx := 0
posy := 400
vely := 0

loop
    Input.KeyDown (chars)
    if chars ('q') then
        exit
    end if
    
    % to make the player move
    if chars (KEY_LEFT_ARROW) then
        velx := -RUN_SPEED
    elsif chars (KEY_RIGHT_ARROW) then
        velx := RUN_SPEED
    else
        velx := 0
    end if
    
    % remember, in order to jump you MUST be on the ground when pressing UP
    if chars (KEY_UP_ARROW) and posy = GROUND_HEIGHT then
        vely := JUMP_SPEED
    end if

    % subtract your "gravity" constant from the velocity EACH frame
    vely -= GRAVITY
    posx += round (velx)
    posy += round (vely)

    % simple "collision" check. just makes sure the player stays above ground
    if posy < GROUND_HEIGHT then
        posy := GROUND_HEIGHT
        vely := 0
    end if

    % different colours just to illustrate whether or not you may jump
    if posy = GROUND_HEIGHT then
        drawfillbox (posx - 10, posy, posx + 10, posy + 20, green)
    else
        drawfillbox (posx - 10, posy, posx + 10, posy + 20, red)
    end if

    drawline (0, GROUND_HEIGHT, maxx, GROUND_HEIGHT, blue)
    View.Update
    delay (25)
    cls
end loop

Window.Close (win)


That's a really simple example, but it should be enough to get going. If you can't adapt that to account for things like floating platforms or ceilings or pits (ie, real collision detection) then you've probably picked a project that is too complicated for you right now.

-----------------------------------
Zampano
Sat Dec 22, 2007 10:37 pm

Re: [Tutorial] How to Jump
-----------------------------------
A tutorial like this is is very helpful, as are all that teach one how to perform tasks without processes.
I do have three questions, though.
First, is there anyway to do the same things you did but without declaring a costly 'real' variable? If the user decides not to jump, vely will continue to decrease and decrease even though it will not be used (unless one jumps). Surely there is a way to avoid something as redundant as that.
Second, what  is the purpose of of velx if you could simply increment posx by the value of ground speed?
Third, what is the purpose of declaring those four constants if there values are already constants by themselves?

I think that though a tutorial such as this does not fit into the Turing Walkthrough, tutorials like these should be listed elsewhere; perhaps in some sort of index to solutions to common programming goals.

-----------------------------------
Tony
Sun Dec 23, 2007 12:31 am

Re: [Tutorial] How to Jump
-----------------------------------

is there anyway to do the same things you did but without declaring a costly 'real' variable? If the user decides not to jump, vely will continue to decrease and decrease

no, if the character stays on the ground, the y-velocity will be 0. If it was to decrease, the character would obviously fall down through the ground.

what  is the purpose of of velx if you could simply increment posx by the value of ground speed?

the value of ground speed is velx.

what is the purpose of declaring those four constants if there values are already constants by themselves?

the purpose of declaring those four constants is that their values are already constant.

remember how you can't change the values of constants? Yeah, it's just like that.

-----------------------------------
Mazer
Sun Dec 23, 2007 12:36 am

Re: [Tutorial] How to Jump
-----------------------------------
First, is there anyway to do the same things you did but without declaring a costly 'real' variable?
Go with integers if you really want. It's actually pretty pointless since I'm rounding anyways.
If the user decides not to jump, vely will continue to decrease and decrease even though it will not be used (unless one jumps). Surely there is a way to avoid something as redundant as that.
You're doing loads of processing in a game regardless of whether the user is doing anything. In the big scheme of things, decreasing vely probably won't count for much of a performance hit. If you prefer, you can put it inside that if statement so that it only decreases when the player is above the ground.
Second, what  is the purpose of of velx if you could simply increment posx by the value of ground speed?
I just wanted to be consistent; the y position isn't directly controlled by keyboard input so I wanted to do the same with the x position. And maybe you'll want to add friction to your game. Or prevent the player from moving horizontally whilst airborne.
Third, what is the purpose of declaring those four constants if there values are already constants by themselves?
That's the whole point. I could easily get rid of those constants and replace every occurrence of GROUND_HEIGHT with 120, but what happens next week when I want to change the ground height in my suddenly larger game? Besides tweaking variables, this makes for easier reading. 120 is the height in pixels of the "surface" in this game, but it's also the minimum height in centimeters for an officer of the Royal Canadian Mounted Police and I don't want to waste one second thinking about which I'm referring to. In fact, the minimum height was 180 centimeters and that rule hasn't actually applied in a really really long time, but I felt like using that in my example.

I think that though a tutorial such as this does not fit into the Turing Walkthrough, tutorials like these should be listed elsewhere; perhaps in some sort of index to solutions to common programming goals.
I don't give a damn about the Turing Walkthrough. What concerns me is the number of retards who come in here and suggest the use of a for loop for something like jumping. DON'T ****ING DO THAT I WILL SLAP YOU. Not that it matters anyways; the people in question wouldn't look at common problems just like they wouldn't look at this tutorial. They want a solution, not a learning experience.

-----------------------------------
rcbhs
Wed Jan 02, 2008 7:59 am

Re: [Tutorial] How to Jump
-----------------------------------
Ratards cause we are new and are basing our ideas off of what we have seen in other programs? Harsh.

-----------------------------------
Clayton
Wed Jan 02, 2008 12:03 pm

Re: [Tutorial] How to Jump
-----------------------------------
Ratards cause we are new and are basing our ideas off of what we have seen in other programs? Harsh.

Not quite.

-----------------------------------
rcbhs
Thu Jan 03, 2008 5:01 am

Re: [Tutorial] How to Jump
-----------------------------------
Oh and thx for this though. Working nicely but now every time I go in one direction I don't stop, i can turn directions and sich but cant stop. I also cant go up with the up arrows key anymore (jump is TAB btw) which I need for ladder climbing. Still trying to figure it out.

-----------------------------------
Mazer
Thu Jan 03, 2008 12:15 pm

RE:[Tutorial] How to Jump
-----------------------------------
That's kind of confusing. You'll have to explain your problem better or post some code.

-----------------------------------
thrivey
Fri Jan 11, 2008 4:01 pm

Re: [Tutorial] How to Jump
-----------------------------------
I am trying to make my character jump onto a platform. Can some one please tell me what an if statement would look like if there are 2 conditions because if my character passes the Y co-ordinate he will teleport to the platform and if he passes the X co-ordinate he teleports to the platform aswell. I need a code that needs both co-ordinates of the platform before it teleports to the platform.. Which wouldn't look like it teleported there cause the co-ordinates would be the same. This is what i got.


    if placex = 70 and placey = 70 then
        placex := 70
        placey := 70
    end if


Edit: Or I just need any code that allows my character to jump on a specific like say 60, 60, 60, 60,red

-----------------------------------
Clayton
Fri Jan 11, 2008 6:54 pm

RE:[Tutorial] How to Jump
-----------------------------------

 if placex = 70 and placey = 70 then
    placex := 70
    placey := 70
end if 


Think about that piece of code for a minute. I mean chew it over, think about what's happening. Afterwards, tell yourself what's wrong with it.

-----------------------------------
thrivey
Mon Jan 14, 2008 6:45 am

Re: [Tutorial] How to Jump
-----------------------------------
Honestly, I don't know :P. I just know what I'm learning in school which isn't very much. That was just me trying get my character to jump onto something.

-----------------------------------
[Gandalf]
Mon Jan 14, 2008 10:08 am

RE:[Tutorial] How to Jump
-----------------------------------
If an apple is worth $5 and an orange is worth $5 then adjust the price of the apple to $5 and the orange to $5.  That's essentially what you're doing.  Will it have any effect, except wasting time?  No.

Using += will probably get your further...

-----------------------------------
thrivey
Mon Jan 14, 2008 12:26 pm

Re: [Tutorial] How to Jump
-----------------------------------
I'm still pretty lost.. Isn't there a command that I can make that when my character collides with my platform it = the base of the platform and therefore stays on?

Can anyone add to the above "How to Jump" code and make it so that box can jump onto something.. I think i'd be able to get it to work from there.

-----------------------------------
Tony
Mon Jan 14, 2008 12:50 pm

RE:[Tutorial] How to Jump
-----------------------------------
go back and read over the code, it's already there

% simple "collision" check. just makes sure the player stays above ground 
    if posy < GROUND_HEIGHT then 
        posy := GROUND_HEIGHT 
        vely := 0 
    end if


-----------------------------------
thrivey
Mon Jan 14, 2008 12:53 pm

Re: [Tutorial] How to Jump
-----------------------------------
I tried something like that already.. but it doesn't work.. =/


    if placey = 75 and placex > 100 and placex < 200 then
        placey := 75
        speedy := 0
    end if

-----------------------------------
Tony
Mon Jan 14, 2008 1:04 pm

RE:[Tutorial] How to Jump
-----------------------------------
that's because you are skipping over the details

 100 and placex < 200 then 
        speedy := jumpspeed
    end if

    if chars (KEY_UP_ARROW) and placey = 340 then
        speedy := jumpspeed
    end if
    % down force (gravity)
    speedy -= downforce
    placex += round (speedx)
    placey += round (speedy)
    
    %making sure the character doesn't go through the platforms floor, and walls.

    if placey < ground then
        placey := ground
        speedy := 0
    end if

    if placey > 95 and placex < 100 and placex < 200 and placey < 85 then
        placey := 90
        speedy := 90
    end if

    if placey  230 and placex < 400 then
        placey := 340
        speedy := 0
    end if

    if placex < 0 then
        placex := 0
        speedx := 0
    end if

    if placex > 770 then
        placex := 770
        speedx := 770
    end if
    
    %picture management(so the picture of the character never leaves)

    if not placey = ground then
        Pic.ScreenLoad ("mario4.jpg", placex - 2, placey + 2, 0)
    end if


    if chars (KEY_UP_ARROW) then
        Pic.ScreenLoad ("mario4.jpg", placex - 2, placey + 2, 0)
    end if

    if not placey = ground and chars (KEY_LEFT_ARROW) then
        Pic.ScreenLoad ("mario3.jpg", placex - 2, placey + 2, 0)
    end if

    if not placey = ground and chars (KEY_RIGHT_ARROW) then
        Pic.ScreenLoad ("mario4.jpg", placex - 2, placey + 2, 0)
    end if

    if placey = ground and chars (KEY_LEFT_ARROW) then
        Pic.ScreenLoad ("mario3.jpg", placex - 2, placey + 2, 0)
    end if

    if placey = ground and chars (KEY_RIGHT_ARROW) then
        Pic.ScreenLoad ("mario4.jpg", placex - 2, placey + 2, 0)
    end if

    if placey = ground and not chars (KEY_LEFT_ARROW) then
        Pic.ScreenLoad ("mario4.jpg", placex - 2, placey + 2, 0)
    end if
    
    %platforms and floor.

    drawline (0, ground, maxx, ground, blue)
    drawline (100, 90, 200, 90, 6)
    drawline (230, 340, 400, 340, 6)
    View.Update
    delay (25)
    cls
end loop

-----------------------------------
thrivey
Thu Jan 17, 2008 11:55 am

Re: [Tutorial] How to Jump
-----------------------------------
Any suggestions?

-----------------------------------
TopBypass
Sat Jan 19, 2008 11:53 am

RE:[Tutorial] How to Jump
-----------------------------------
Is it possible to change the object from a box to an image?

-----------------------------------
Clayton
Sat Jan 19, 2008 12:38 pm

RE:[Tutorial] How to Jump
-----------------------------------
Yes, instead of drawing your box, draw the image in it's place...

-----------------------------------
TopBypass
Sat Jan 19, 2008 12:51 pm

RE:[Tutorial] How to Jump
-----------------------------------
i mean inserting an image.

-----------------------------------
Clayton
Sat Jan 19, 2008 2:24 pm

RE:[Tutorial] How to Jump
-----------------------------------
It's the exact same thing. However, this really deserves it's own topic in [Turing Help], ask your question there.

-----------------------------------
Michael516
Fri Mar 14, 2008 2:18 pm

Re: [Tutorial] How to Jump
-----------------------------------
thrivey if you still cant figure out how to put platforms in i have edited the how to jump program so that is has a platform in it
here it is...

-----------------------------------
A.J
Sun Mar 16, 2008 4:21 pm

Re: [Tutorial] How to Jump
-----------------------------------
just to add to this tutorial,
the example Michael516 gave is good, but make sure you can't jump up through the line
(it shouldn't be accessible up or down)

and make sure you can't go out of the screen (a couple more if statements)

-----------------------------------
Doug101
Wed Mar 26, 2008 7:11 pm

Re: [Tutorial] How to Jump
-----------------------------------
Hey Micheal its Doug its a coll game but wat do these stand for "var velx, vely : real" im really confused? :?

-----------------------------------
Mackie
Wed Mar 26, 2008 7:17 pm

RE:[Tutorial] How to Jump
-----------------------------------
They stand for X Velocity and Y Velocity. Which is the speed the character is moving at for both x, and y axis.

-----------------------------------
Doug101
Wed Mar 26, 2008 7:22 pm

Re: [Tutorial] How to Jump
-----------------------------------
Thanks Mackie that explains alot :D

-----------------------------------
Jujeff
Mon May 12, 2008 12:08 pm

Re: [Tutorial] How to Jump
-----------------------------------
I've already posted this to collision detection, but I don't know where to really put it, so I put it here too.  I'm new to here, and Turing (sort of) so I'm sorry if I appear a noob. I wanted to make a platformer for a school project, so I realized I needed to make my character jump. I found the highly-helpful Mazer Jump Tutorial, copied and pasted (there are some numbers I couldn't figure out the use of), and tweaked it to my needs. 

After putting in a platform, I discovered that this messed everything up (the character cannot jump once on a platform), I tried to fix it as best as I could, and added in a boolean variable by recommendation of a friend, however as it is not clear to me the use of this, I will paste the code that I had before the boolean variable. 

(The problem of the jumping of the platform, as far as I can see, is that the Yvelocity must be 0 to be able to jump, but the character can only be on the platform if Yvelocity is above the value of "gravity".) 

Code: 

setscreen ("graphics:1011;699;nobuttonbar;offscreenonly") 

% VARIABLES! 

var keystroke : array char of boolean 

var height : int := 1 
var LeftWall : int := 1 
var RightWall : int := 1011 
var movespeed : int := 10 
var jumpspeed : int := 30 
var gravity : int := 2 

var Xposition, Yposition : int 
var Xvelocity, Yvelocity : real 

Xposition := 20 
Yposition := 400 
Xvelocity := 0 
Yvelocity := 0 

% game loop 

loop 

Input.KeyDown (keystroke) 

% horizontal movement 

if keystroke ('a') then 
Xvelocity := -movespeed 
elsif keystroke ('d') then 
Xvelocity := movespeed 
else 
Xvelocity := 0 
end if 

% jump movement 

if keystroke ('w') and Yposition = height then 
Yvelocity := jumpspeed 
end if 
% world (lines and where you can walk and such) 

drawline (0, height, maxx, height, blue) 

drawline (245, 146, 384, 146, blue) 

% line (if you're on the middle line, stay on it) 

if Xposition >= 245 and Xposition = 140 then 

Yposition := 140 
Yvelocity := 10 
end if 

% reinstate gravity every loop 

Yvelocity -= gravity 
Xposition += round (Xvelocity) 
Yposition += round (Yvelocity) 

% don't fall through the ground or go past the edges 

if Yposition  RightWall then 
Xposition := 1010 
end if 

% character (square for now) 

drawfillbox (Xposition - 10, Yposition, Xposition + 10, Yposition + 20, green) 

drawfillbox (Xposition - 10, Yposition, Xposition + 10, Yposition + 20, red) 

% animation 

View.Update 
delay (25) 
cls 

end loop

-----------------------------------
Jujeff
Mon May 12, 2008 7:47 pm

Re: [Tutorial] How to Jump
-----------------------------------
EDIT::: no need, answer is found

-----------------------------------
DaveAngus
Tue May 20, 2008 12:35 pm

RE:[Tutorial] How to Jump
-----------------------------------
This tutorial is great and has helped me out a lot! Thanks Mazer!

-----------------------------------
SAMZ
Mon May 26, 2008 9:17 am

Re: [Tutorial] How to Jump
-----------------------------------
What about a wall? How would I jump over and back with a wall?
