The Partridge Family were neither partridges nor a family. Discuss.
-
Izichy
- Posts: 2
- Joined: February 9th, 2019, 7:09 pm
Post
by Izichy » February 9th, 2019, 8:33 pm
I've came up with a very simply code for making a character jump. I'm uploading this just in case anybody has a need for a jumping code so feel free to use it. Below is the code but I am also attaching the source code so you can it working.
Code: Select all
float x, y, vx, vy;
float gravity = 1.0f;
bool jumping = false;
drawPlayer(x, y, r, g, b);
if (jumping == false && kbd.KeyIsPressed(VK_SPACE))
{
vy = -20;
jumping = true;
}
if (jumping == true)
{
if (y + vy < 500) \\ while player is in the air
{
y += vy;
vy += gravity;
}
else
{
jumping = false;
y = 500;
}
}
-
albinopapa
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Post
by albinopapa » February 10th, 2019, 4:03 am
Thanks for sharing.
I would suggest separating out your drawing code from your logic code though. It's not relevant to the jumping code, and things get harder to reason about when you start mixing, like asking yourself "what state is the character in when it's drawn?", you might forget and try drawing again.
Also, in case anyone misses it, the variables at the top are not initialized, so assume they belong to a class and that they are initialized. I believe for the sake of simplicity they are left uninitialized as this is just an example of how to implement a jump mechanic.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com
-
Izichy
- Posts: 2
- Joined: February 9th, 2019, 7:09 pm
Post
by Izichy » February 10th, 2019, 6:38 am
Yes this is just for demonstration. I’m an old head here just don’t know my old login lol
-
chili
- Site Admin
- Posts: 3948
- Joined: December 31st, 2011, 4:53 pm
- Location: Japan
-
Contact:
Post
by chili » February 11th, 2019, 1:54 am
Yeah, this is the basic logic. It forms a very crude sort of state machine. In the old intermediate tutorials, I show a more sophisticated state machine for handling all sorts of platformer movement (though it's probably still very jank, lacking any modern C++ techniques and also my style has developed a lot since then
).
A lot of newbies ask about how to make a character jump, maybe this will give them a push in the right direction
Chili