Posted: Tue Nov 19, 2002 10:33 pm Post subject: [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
var chars : array char of boolean
if chars (KEY_UP_ARROW) then
if chars (KEY_RIGHT_ARROW) then
if chars (KEY_LEFT_ARROW) then
if chars (KEY_DOWN_ARROW) then
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
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!!!
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.
Posted: Tue Nov 19, 2002 10:37 pm Post subject: (No subject)
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
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'
Posted: Tue Nov 26, 2002 10:35 pm Post subject: 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.
Computer Science CanadaHelp with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Posted: Wed Dec 04, 2002 12:11 am Post subject: (No subject)
can't you use a fork process??
Posted: Wed Dec 04, 2002 12:20 am Post subject: (No subject)
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.
Posted: Fri Feb 28, 2003 8:41 am Post subject: 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?
Posted: Thu May 22, 2003 5:59 pm Post subject: (No subject)
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.
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...
Posted: Thu May 22, 2003 6:07 pm Post subject: (No subject)
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.
Posted: Thu May 22, 2003 6:26 pm Post subject: (No subject)
it should be
if chars (KEY_RIGHT_ARROW) and x <maxx -5 then
this way it also checks that the circle is located to the left of the max X value (-5 to allow for final move) before moving. You edit the rest of the if statments in a similar way
Posted: Wed Aug 27, 2003 12:34 pm Post subject: (No subject)
I'm havin sometruble with the space bar. I looked in the help files and it sayed to use ORD_SPACE but ORD is an INT value not a char so I can't use the same var chars := array char of boolean because it has to be an int (I think)
So far I've thought of doin this
var spacebar : array 1 .. * of boolean
if spacebar (ORD_SPACE) then
I know thin is not right but it was the best I could come up with and it gave me the fewest errors. Please help me