Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Simple Movement Code.  (Read 1786 times)

0 Members and 1 Guest are viewing this topic.

Koh

Simple Movement Code.
« on: February 14, 2007, 01:46:55 pm »
  • Tamer Koh
  • *
  • Reputation: +6/-0
  • Offline Offline
  • Gender: Male
  • Posts: 980
This is my custom movement code, which I made all by myself.  I figured out how to type it all in less than 2 minutes.

For objPlayer:

Create Event
   image_speed=0.3
   moveable=1
   moving=0

Step Event
   var d, u, l, r;

   d = keyboard_check(vk_down)
   u = keyboard_check(vk_up)
   l = keyboard_check(vk_left)
   r = keyboard_check(vk_right)
   
   if d && u{
      d=0
      u=0
   }

   if r && l{
      r=0
      l=0
   }

   if moveable{
      if d{
         if place_free(x,y+1){
            y+=1
         }
         else{
            move_contact_solid(270,1)
         }
      }
      if u{
         if place_free(x,y-1){
            y-=1
         }
         else{
            move_contact_solid(90,1)
         }
      }
      if l{
         if place_free(x-1,y){
            x-=1
         }
         else{
            move_contact_solid(180,1)
         }
      }
      if r{
         if place_free(x+1,y){
            x+=1
         }
         else{
            move_contact_solid(360,1)
         }
      }
   }

   if (d or u or l or r){
      moving=1
   }
   else{
      moving=0
   }

   if moving{
      image_speed=0.3
   }
   else{
      image_speed=0
   }
Dayjo: I set the message icon to GM6, please remember to do this next time.
« Last Edit: February 14, 2007, 08:34:59 pm by Dayjo »
Logged
  • Megaclipse Games
Re: Simple Movement Code.
« Reply #1 on: February 14, 2007, 02:33:11 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
Why is it that people always seem to add so many conditional statements to work out which direction the player should be moved in - lol, look at mine for CR its 2 lines long XD.

Code: [Select]
float movementX = ((float)BindedKeyDown("move_right") + -((float)BindedKeyDown("move_left")));
float movementY = ((float)BindedKeyDown("move_down") + -((float)BindedKeyDown("move_up")));

course in GML it would probably look something like this.

Code: [Select]
movementX = (keyboard_check(vk_right) + -keyboard_check(vk_left));
movementY = (keyboard_check(vk_down) + -keyboard_check(vk_up));

See? Even deals with the left+right, up+down key combos my making the movement 0 if they are pressed together XD.
« Last Edit: February 14, 2007, 02:35:32 pm by Infinitus »
Logged

mit

Re: Simple Movement Code.
« Reply #2 on: February 14, 2007, 07:27:25 pm »
  • QBASIC programmer since age 4. Take that, world.
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 1079
Mm, Helios, I use that a lot. However people like more complex engines as they a) want complicated sprite setups (such as changing to a sideways sprite when going diagonally and stuff) or b) want it easier to read through. Or something else.

Other time saving notes are doing stuff like timesing by an expression, so a space game might use:
Code: [Select]
direction = (keyboard_check(vk_right)-keyboard_check(vk_left))*5
speed=3*(sprite_index!=spr_explode) + 2*using_boost* - 5*keyboard_check(vk_down)
Not the most perfect example, but that'd stop you moving when you explode, move faster when you boost, and reverse when you hold backwards.
Logged
Programmer / Spriter / Level designer / Game Director / Web Designer / Music Sequencer for
Random Highscore table:

Play the Kousou Arcade today!
  • Kousou Games

Antidote

>.>
Re: Simple Movement Code.
« Reply #3 on: February 14, 2007, 10:42:06 pm »
  • In all seriousness who's serious?
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1485
I put a quick Zelda example together for anyone to use (GM6) using Helios' and mit's suggestion.

Step:
Code: [Select]
{
 movementX = (keyboard_check(vk_right) + -keyboard_check(vk_left));
 movementY = (keyboard_check(vk_down) + -keyboard_check(vk_up));
 if (movementX != 0 && movementY == 0)
 {
  switch(movementX)
  {
   case -1 : x -= 4; draw_set_color(c_blue);break;
   case 1  : x += 4; draw_set_color(c_black);break;
  }
 }
 else if (movementY != 0 && movementX == 0)
 {
  switch(movementY)
  {
   case -1 : y -= 4; draw_set_color(c_green);break;
   case 1  : y += 4; draw_set_color(c_red);break;
  }
 }
 else
 {
  switch(movementY)
  {
   case -1:
           y -=3;
           switch(movementX)
           {
            case -1: x -= 3; break;
            case 1 : x += 3; break;
           }
   break;
   case 1:
           y += 3;
           switch(movementX)
           {
            case -1: x -= 3; break;
            case 1 : x += 3; break;
           }
   break;
  }
 }
}
Draw:
Code: [Select]
{
 draw_rectangle(x-16, y-16, x+16, y+16,false);
}
of course you won't be drawing a rectangle but you get the idea.
if someone can think of a better way of doing this be my guest and improve on the code.

EDIT:
EDIT2:
As you may notice i did not add collision to this engine example becuase I do not want to release my collision code right now. but i'll release a collision demo for my game (which was HIATUS but now active again (need spriters and misc. gfx artists))

TO AVOID ARGUMENTS.
« Last Edit: February 14, 2007, 10:50:29 pm by Antidote »
Logged
  • Axiomatic Data Laboratories
Re: Simple Movement Code.
« Reply #4 on: February 14, 2007, 11:17:20 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
I took the liberty of compressing that down Antidote, as quite a few of the conditional statements in that code are redundent. I'm not sure if GML actually has a ternary operator so you may have to change the draw_set_color calls a bit.

There are ways of compressing it down further but most will just make it more complex to maintain and rather unneccessary.

Code: [Select]
{
 movementX = (keyboard_check(vk_right) + -keyboard_check(vk_left));
 movementY = (keyboard_check(vk_down) + -keyboard_check(vk_up));
 if (movementX != 0 && movementY == 0)
 {
  x += movementX * 4;
  draw_set_color(movementX == -1 ? c_blue : c_black);
 }
 else if (movementY != 0 && movementX == 0)
 {
  y += movementY * 4;
  draw_set_color(movementY == -1 ? c_green : c_red);
 }
 else
 {
  y += movementY * 3;
  x += movementX * 3;
 }
}
« Last Edit: February 14, 2007, 11:20:57 pm by Infinitus »
Logged

Goodnight

Once and future Captain
Re: Simple Movement Code.
« Reply #5 on: February 14, 2007, 11:54:48 pm »
  • With a Capital G
  • *
  • Reputation: +1/-0
  • Offline Offline
  • Gender: Male
  • Posts: 706
Why is it that people always seem to add so many conditional statements to work out which direction the player should be moved in - lol, look at mine for CR its 2 lines long XD.

It looks like the first part of dlbrooks's Step event (which records keychecks to variables and cancels opposing directions) was probably inspired by my engine.. and you see, I originally did it the same way as you (x movement = right keycheck - left keycheck). But I changed it to the other way because there were some extra calculations that had to be done before figuring out the final movement steps. Like seeing if the player is trying to walk around a corner.
Logged

Antidote

>.>
Re: Simple Movement Code.
« Reply #6 on: February 15, 2007, 01:13:00 pm »
  • In all seriousness who's serious?
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1485
Oh, thanks Helios. The only problem is that the movementX == -1 ? c_blue : c_red portion isn't legal XD so no ternary ops
« Last Edit: February 15, 2007, 01:17:29 pm by Antidote »
Logged
  • Axiomatic Data Laboratories
Re: Simple Movement Code.
« Reply #7 on: February 15, 2007, 01:29:38 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
ah ok, 1 sec.
Logged

Koh

Re: Simple Movement Code.
« Reply #8 on: February 16, 2007, 03:47:15 am »
  • Tamer Koh
  • *
  • Reputation: +6/-0
  • Offline Offline
  • Gender: Male
  • Posts: 980
Why is it that people always seem to add so many conditional statements to work out which direction the player should be moved in - lol, look at mine for CR its 2 lines long XD.

Code: [Select]
float movementX = ((float)BindedKeyDown("move_right") + -((float)BindedKeyDown("move_left")));
float movementY = ((float)BindedKeyDown("move_down") + -((float)BindedKeyDown("move_up")));

course in GML it would probably look something like this.

Code: [Select]
movementX = (keyboard_check(vk_right) + -keyboard_check(vk_left));
movementY = (keyboard_check(vk_down) + -keyboard_check(vk_up));

See? Even deals with the left+right, up+down key combos my making the movement 0 if they are pressed together XD.
Well,w ehn you have the nditionals, it is easier to add things to those to argue and what not.
Logged
  • Megaclipse Games

Antidote

>.>
Re: Simple Movement Code.
« Reply #9 on: February 16, 2007, 05:24:21 am »
  • In all seriousness who's serious?
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1485
there ARE other ways of doing that.
Logged
  • Axiomatic Data Laboratories
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.241 seconds with 57 queries.

anything