Isometric WASD movement

Previous topic - Next topic

Omegavolt

Hi!

Im quite new to programming and GLBasic, but I wanted to give it a shot.  I decided to try moving an isometric sprite at the usual isometric angles (about 30 degrees clockwise) using WASD controls.  I was able to get it to work, but my code is rather simplistic and doesnt account for holding down multiple keys or leaving the sprite in an idle stance after the keys are released.

Code (glbasic) Select
LOADBMP "ground.bmp"
LOADSPRITE "human0.bmp", 1
LOADSPRITE "human1.bmp", 2
LOADSPRITE "human2.bmp", 3
LOADSPRITE "human1.bmp", 4
LOADSPRITE "human4.bmp", 5
LOADSPRITE "human3.bmp", 6
LOADSPRITE "human5.bmp", 7
LOADSPRITE "human3.bmp", 8
LOADSPRITE "human7.bmp", 9
LOADSPRITE "human6.bmp", 10
LOADSPRITE "human8.bmp", 11
LOADSPRITE "human6.bmp", 12
LOADSPRITE "human9.bmp", 13
LOADSPRITE "human10.bmp", 14
LOADSPRITE "human11.bmp", 15
LOADSPRITE "human10.bmp", 16

human_x=100
human_y=100
ani=2
movedelay=0

WHILE TRUE
SPRITE ani, human_x, human_y

IF KEY(32) //'d' button

movedelay=movedelay+1 //increments animation delay
human_x=human_x+3 //moves charsprite 330 degrees
human_y=human_y+1
IF movedelay>4 //triggers animation
ani=ani+1 //increments animation frame
IF ani>4 // triggers frame reset
ani=1 //resets animation frame
ENDIF

movedelay=0 //resets animation delay
ENDIF

ENDIF

IF KEY(17) //'w' button

movedelay=movedelay+1
human_x=human_x+3 //moves charsprite 60 degrees
human_y=human_y-1
IF movedelay>4
ani=ani+1
IF ani>8
ani=5
ENDIF

movedelay=0
ENDIF

ENDIF

IF KEY(30) //'a' button

movedelay=movedelay+1
human_x=human_x-3 //moves charsprite 150 degrees
human_y=human_y-1
IF movedelay>4
ani=ani+1
IF ani>12
ani=9
ENDIF

movedelay=0
ENDIF

ENDIF

IF KEY(31) //'s' button

movedelay=movedelay+1
human_x=human_x-3 //moves charsprite 240 degrees
human_y=human_y+1
IF movedelay>4
ani=ani+1
IF ani>16
ani=13
ENDIF

movedelay=0
ENDIF

ENDIF

SHOWSCREEN
WEND
MOUSEWAIT
END
Is there anyway to use the IF operations to prevent multiple key entries and leave an idle sprite?  Or is this code too simplistic for the task?

Kitty Hello

Code (glbasic) Select
// Fix multiple keys:
dx = 0 // x-movement
dy = 0 // y-movement

dx = KEY(32) - KEY(30) // dx=-1(a), +1(d)
IF dx = 0
   dy = KEY(31) - KEY(17) // dy=-1(w), +1(s)
ENDIF

// map othogonal directon
// to isometric
// dx  1 -1  0  0
// dy  0  0  1 -1
// mx  1 -1 -1  1
// my  1 -1  1 -1
IF dx
   mx = dx
   my = dx
ELSE
   mx = -dy
   my = dy
ENDIF

// move player
human_x  =human_x + mx*3
human_y = human_y + my

// animation
IF mx OR my
   movedelay = movedelay + 1
   ani=MOD(ani+1, 4) // 0,1,2,3,0,1,2,...
   directioon = 1
   IF dx=1 THEN direction=9
   IF dy=1 THEN direction=13
   IF dy=-1 THEN direction=5
ENDIF
// draw the sprite with animation + direction offset index
SPRITE ani + direction, human_x, human_y
Was this what you need? Do you understand what I did?

Omegavolt

Yes!  Thank you!

I finished out the animation delay with an IF statement, and it works perfectly. :D