DIV ARENA FORUMS

Day 3 - Player input

MikeDX - 3-3-2016 at 10:09 PM

All the code and assets for this tutorial are on github !

On day 2, we created our player process and drew our graphic on screen. We can now get user input and move him around.

To catch up, download day2 from github

Open up the DIV IDE. If you choose to continue at the splash screen, we should see where we were when we left off yesterday.






Following on from yesterday, lets talk about getting player input.

The easiest way to do this is with the mouse, but we want to use keys for this. DIV provides a simple way to know if a key is pressed via the KEY() function.

using the IF statement, we check to see if keys are pressed, and if so, perform actions.

For our player, we will use the LEFT (_left) and RIGHT (_right) cursor keys as parameters for the KEY() function to determine what has been pressed.

Add the following code to our player() process below the LOOP statement but above the FRAME; command :

Code:
IF(KEY(_left)) END IF(KEY(_right)) END


This sets up two different actions that can happen when the left or right keys are pressed. This isn't any good without some action to perform, so lets add that now.

Add the following between the KEY(_right) condition:
Code:
x=x+1;


and this between the KEY(_left) condition:
Code:
x=x-1;


Now our code should look like this:



This tells DIV that when the LEFT key is pressed, to move our player 1 pixel to the left of the screen (x is a cross remember ;)) and when RIGHT is pressed, to move one pixel to the right of the screen!

Now, when we run the program with F10 and press the left and right keys, our player moves!





At this point, our player is facing the wrong way when we press eft (he's still facing right) so lets fix that with the FLAGS variable.

the FLAGS LOCAL PROCESS variable tells DIV how to render our GRAPH in different ways:

FLAGS can be set to one of the following values:

0-Normal graphic.
1-Horizontal mirror.
2-vertical mirror.
3-Horizontal and vertical mirror (180ΓΈ).
4-Transparent graphic.
5-Horizontal transparencies and mirror.
6-vertical transparencies and mirror.
7-Transparencies, horizontal and vertical mirror.


We want to make our player "flip" horizontally, so we need flags = 1. Lets add that to our code:

In the LEFT IF() condition, add the following:

Code:
flags=1;


and in the RIGHT IF() - reset it back to 0 for normal rendering:
Code:
flags=0;





When we run the program with F10 - our player now faces the correct way when we move him!





Keywords learned in this lesson:

Code:
IF() KEY() _left _right x flags END





See you in the next step, where we will add some animation frames!