
-----------------------------------
Tony
Tue Nov 19, 2002 10:33 pm

[Tutorial] Character control on screen (Input.KeyDown)
-----------------------------------
Note: Input.KeyDown does NOT work with turing v3 (or lower... you should not be using anything lower anyway). Try to get yours hands on a v4 compiler. Bitch at your teacher for having a crappy version or something

By demand, I'm posting a tutorial on how to give the user a control over a character in the game.

Here's the theory behind it... user presses the button, you check what button is pressed and react accordingly... Well here's the code to start us off:

var x,y : int
x:=100
y:=100
var chars : array char of boolean
        loop
            Input.KeyDown (chars)
            
            if chars (KEY_UP_ARROW) then
                y:=y+5
            end if
            if chars (KEY_RIGHT_ARROW) then
                x:=x+5
            end if
            if chars (KEY_LEFT_ARROW) then
                x:=x-5
            end if
            if chars (KEY_DOWN_ARROW) then
                y:=y-5
            end if
            
            drawoval(x,y,4,4,red)
            delay(10)
cls
            
        end loop

code works already... you can control a small red circle around the screen with arrow buttons. Come on, try it out... I'll wait.

Now to understand how this code works. X and Y are variables to store the current location of the circle. Each is assigned a value of 100 to declear initial position... You can change that to anything.

var chars : array char of boolean this declears an array of characters with boolean values... don't ask me how or what about it... its like an API, you just don't ask... We need this line and thats final.

then we start a loop... so that you can move your circle(square, triangle, 3D spacefighter, etc) more then once.

here's another turing API, Input.KeyDown (chars) This function stores all the buttons pressed at the moment on the keyboard in the array called chars which we decleared few lines before. Using this instead of getch() allows us for multiple button input, so we can move diagnoly  :D 

Next are the IF statments... Gous like this  If chars(BUTTON) then do whatever

and yes, KEY_UP_ARROW is a real button name, though you can use letters and ASCII values too.

Having 4 separate if statments allows us to check for all 4 buttons and execute them all, not just the first one we find. Using IF-ELSEIF structure will allow to use only 1 button and the first one found will be executed, nothing else.

Finally we draw our circle with new coordinates, run a little delay (to slow down the process) and clear the screen so we don't have garbage all over it.

And we're DONE! You've got a nice little red circle that a user can control!!!   :D


BONUS: Lets go one step further, try adding additional control to the circle... say if you press "ENTER" if will do something special. Like change color or size of w/e.

-----------------------------------
Tony
Tue Nov 19, 2002 10:37 pm


-----------------------------------
Alright, I'm gonna provide you with the answer, but atleast try to do that yourself... If you already tried, feel free to scroll down



\/


 if chars (KEY_ENTER) then
                 for i:1..10
                 drawoval(x,y,4+i,4+i,blue)
                 delay(50)
                 end for
             end if

Special NOTE You can look up all system key names in the help file. Characters have single quotations around them. Such as 'a' or 'Z'

There's a good example [url=http://www.compsci.ca/v2/viewtopic.php?p=16903]here

-----------------------------------
bizkit
Sun Nov 24, 2002 11:54 pm

hrm... riiight
-----------------------------------
i did a copy paste of your code and for all the key inputs i got errors saying:
"KeyDown" is not the export list of "input"
not sure why its doin that

-----------------------------------
Dan
Mon Nov 25, 2002 3:40 am

Re: hrm... riiight
-----------------------------------
i did a copy paste of your code and for all the key inputs i got errors saying:
"KeyDown" is not the export list of "input"
not sure why its doin that

i can think of a few reasions why you may be geting this erro, the main one being that i dont think that some of the older virosions of turing sport a few of the fuctions used in that code.

a few chages you coude make to use tha code with older turing versions whode be chage "var chars...." line to:

var chars: string (1)

chage "input.keydown(chars)" to 

getch (chars)

chage all the lines like "if chars (KEY....." to 

if ch = chr (num of key) then

some of the nums for the keys are: 208 (down), 200 (up), 203 (left), 205 (right)


well thats all i can think of right now, i have not had time to test these chages so try them and post and tell us if they wrok, i am all tied up with working on making the server wrok. good luck.

-----------------------------------
Tony
Mon Nov 25, 2002 10:02 pm


-----------------------------------
sorry dan... chars should stay as that special array of boolean chars stuff...

what that does is that on every KeyDown function an array stores ALL the buttons pressed down. This allows to move up and right simutaniusly and perform a special action at same time  :D 

If you're using a version of turing under 4.0, then you might not have that function included. If so, you'd have to use getch() function.

var char:string(1)
loop
getch(char)
if char="w" then
%do w/e
elsif char = "a" then
%do something else
...
end if
end loop

the code is untested, but I think thats how it works. KeyDown should work in 4.0 and up.

-----------------------------------
Dan
Tue Nov 26, 2002 10:35 pm

la la la
-----------------------------------
sory for the cofuasion, i mean that you have to make thouse chages if you got a craper version of turing and you dont got the keydown stuff to use with the boolen vars, not that you posted the code wrong or anything.

-----------------------------------
JayLo
Wed Dec 04, 2002 12:11 am


-----------------------------------
can't you use a fork process??

-----------------------------------
Tony
Wed Dec 04, 2002 12:20 am


-----------------------------------
you can use fork with getch() to continue on with your game, without waiting for the button to be pressed, as in snake or pacman where character keeps on moving in same direction.

-----------------------------------
Vicous
Thu Feb 27, 2003 11:35 am

not in the export list?
-----------------------------------
when I try to run the original code in this forum, turing highlites Keydown, keyuparrow, etc. and comes up keydown is not in the export list of input, Im using turing 4.01, whats wrong?

p.s. No offence, but why are so many good programmers such bad spellers? I'm 16 and Im laughing at some of the spelling in here!!!

________________________
"Why don't you go into the kitchen and get yourself a big glass of SHUT THE FUCK UP!" 
                          Unknown

-----------------------------------
Tony
Thu Feb 27, 2003 11:51 am


-----------------------------------
I got winoot 4.0.4c and the code works. You can download a free patch from holtsoft's site.

about the spelling... dan just cant spell, end of story... If I got bad spelling, its ether a typo or... i donno, lets just say its a typo :wink:

-----------------------------------
Vicous
Fri Feb 28, 2003 8:41 am

thx
-----------------------------------
you were right bout 4.04... thanks
Oh, and a tip, avoid using cls like you did in the tutorial, try instead to redraw a circle (White) over the old one

____________________
How do I automaticly set up a signature down here anyway?

-----------------------------------
skatelhs
Thu May 22, 2003 5:59 pm


-----------------------------------
I'm really not sure if im allowed to link from here... Theres a lot of rules... But I'm just going to put the link to those Turing Update Patches, should fix all that stuff with the errors about the imput functions.  
http://www.holtsoft.com/turing/support/#turing4patches
Go there and scroll down, theres some patches to update to turing 4.04, its 9.16 megabyte.  
Let me know also if im not allowed to do this...
adam

-----------------------------------
skatelhs
Thu May 22, 2003 6:07 pm


-----------------------------------
Im also thinking, for this movement thing, How do you make the red circle STOP at the min and max x and y values? would you assign a variable to the x and y values of the circle, and put

if x:minx, maxx then "Stop"
if y:miny, maxy then "Stop"

And I'm also not sure how to make it "Stop", any suggestions?

If this sounds completely ridiculous then tell me, this is my first year of compsci at school.

-----------------------------------
Tony
Thu May 22, 2003 6:26 pm


-----------------------------------
it should be


if chars (KEY_RIGHT_ARROW) and x 