2d platform game. Moving a character

Posted By: Harstad

2d platform game. Moving a character - 01/12/09 22:15

Ok, so I'm trying to make a simple 2d Platform game, just to get started.

this is what I got so far:

#include <acknex.h>
#include <default.c>

BMAP* background = "second.bmp";
BMAP* gras = "gress.bmp";
BMAP* person = "player.bmp";

PANEL* new_background = //this is the background.
{
pos_x = 0;
pos_y = 0;
bmap = background;
layer = 1;
flags = VISIBLE;
}

PANEL* new_ground = //this is the ground for the player to walk on.
{
pos_x = 0;
pos_y = 0;
bmap = gras;
layer = 3;
flags = OVERLAY | VISIBLE;
}


PANEL* Player_2 = //this is the player.
{
pos_x = 250;
pos_y = 450;
layer = 2;
bmap = person;
flags = VISIBLE | OVERLAY;
}

PANEL* Player_1 =
{
pos_x = 250;
pos_y = 425;
Layer = 2;
bmap = "player.bmp";
flags = VISIBLE | OVERLAY;
}

function move_right()
{
new_background.pos_x += 5;
wait (1);
}

function quit_program()
{
sys_exit (NULL);
}



function main ()
{
video_mode = 10;
video_screen = 1;
screen_color.blue = 150;
handle = media_loop ("123.mp3",NULL,50);
on_q = quit_program;
while(1)
{
new_background.pos_x -=15*(key_d-key_a)*time_step; //this enables the player to walk forwards and backwards
new_ground.pos_x -=15*(key_d-key_a)*time_step;
if (key_w)
{
Player_2.pos_y -=30*time_step;
}
else
{
Player_2.pos_y +=30*time_step;
}

if (Player_2.pos_y > 450)
{
Player_2.pos_y = 450;
}
wait(1);
}
}
Posted By: Ralph

Re: 2d platform game. Moving a character - 01/12/09 22:39

Hi,
Replace this:
Code:
on_d = move_right;

With this:
Code:
while(1){
  Player_2.pos_x +=5*(key_d-key_a)*time_step;
  wait(1);
}

Posted By: Harstad

Re: 2d platform game. Moving a character - 01/13/09 10:50

thanks^^
Posted By: Harstad

Re: 2d platform game. Moving a character - 01/13/09 11:09

Deleted.

Posted By: Harstad

Re: 2d platform game. Moving a character - 01/16/09 23:30

ok, I got this moving, and stuff...

But I need some more help...

I've added code for a jump:
if (key_w)
{
Player_2.pos_y -=30*time_step;
}
else
{
Player_2.pos_y +=30*time_step;
}
I get him to move up and down, but here lies the problem, how can I get him to move to pos_y = 400, and then fall back to 450? As it is now, he goes further up as long as I hold the button.
Posted By: pewpew

Re: 2d platform game. Moving a character - 01/17/09 01:07

Originally Posted By: Ralph
Hi,
Replace this:
Code:
on_d = move_right;

With this:
Code:
while(1){
  Player_2.pos_x +=5*(key_d-key_a)*time_step;
  wait(1);
}


i dont understand - why do you do the (key_d - key_a) thing? how does that work..?
Posted By: Harstad

Re: 2d platform game. Moving a character - 01/17/09 09:23

because, you then dont have to make two functions for moving left and right, with that code you can simply use a and d to move left and right with one line;)

Posted By: Quad

Re: 2d platform game. Moving a character - 01/17/09 09:29

yeah if user presses a itll -5*time_step. if presses d it'll be 5*time_step.

but moving player by directly changin it's dimensions is not good. If you want collisions and stuff, you need to use c_move or physical functions.
Posted By: pewpew

Re: 2d platform game. Moving a character - 01/17/09 11:22

ohh.. i see now. key_a and key_d are 1 or 0. i was assuming that it converted the key into an ascii code and subtracted. heh i have a horrible tendency to look straight past little tricks like that.
Posted By: Harstad

Re: 2d platform game. Moving a character - 01/17/09 11:34

Originally Posted By: Quadraxas
yeah if user presses a itll -5*time_step. if presses d it'll be 5*time_step.

but moving player by directly changin it's dimensions is not good. If you want collisions and stuff, you need to use c_move or physical functions.


I'm not yet that technical, and I try to make just a simple game of sidescrolling:P though, I was hoping for something to add collision, making like a rock you have to jump, a door and stuff like that:P
Posted By: Darkyyes

Re: 2d platform game. Moving a character - 01/23/09 21:34

Originally Posted By: Harstad
ok, I got this moving, and stuff...

But I need some more help...

I've added code for a jump:
if (key_w)
{
Player_2.pos_y -=30*time_step;
}
else
{
Player_2.pos_y +=30*time_step;
}
I get him to move up and down, but here lies the problem, how can I get him to move to pos_y = 400, and then fall back to 450? As it is now, he goes further up as long as I hold the button.


I was wondering about the same thing, as it would be useful if anyone would give a solution smile
© 2024 lite-C Forums