Computer Science Canada [Tutorial] Character control on screen (Input.KeyDown) |
Author: | Tony [ 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:
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. |
Author: | Tony [ Tue Nov 19, 2002 10:37 pm ] | ||
Post 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 \/
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 here |
Author: | bizkit [ Sun Nov 24, 2002 11:54 pm ] |
Post subject: | 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 |
Author: | Dan [ Mon Nov 25, 2002 3:40 am ] |
Post subject: | Re: hrm... riiight |
bizkit wrote: 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. |
Author: | Tony [ Mon Nov 25, 2002 10:02 pm ] | ||
Post subject: | |||
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 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.
the code is untested, but I think thats how it works. KeyDown should work in 4.0 and up. |
Author: | Dan [ 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. |
Author: | JayLo [ Wed Dec 04, 2002 12:11 am ] |
Post subject: | |
can't you use a fork process?? |
Author: | Tony [ Wed Dec 04, 2002 12:20 am ] |
Post 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. |
Author: | Vicous [ Thu Feb 27, 2003 11:35 am ] |
Post subject: | 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 |
Author: | Tony [ Thu Feb 27, 2003 11:51 am ] |
Post subject: | |
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 |
Author: | Vicous [ 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? |
Author: | skatelhs [ Thu May 22, 2003 5:59 pm ] |
Post 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. 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 |
Author: | skatelhs [ Thu May 22, 2003 6:07 pm ] |
Post 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. |
Author: | Tony [ Thu May 22, 2003 6:26 pm ] | ||
Post subject: | |||
it should be
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 |
Author: | ¿§¥Jåmës¥§¿ [ Wed Aug 27, 2003 12:34 pm ] | ||
Post 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
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 |
Author: | Tony [ Wed Aug 27, 2003 5:52 pm ] |
Post subject: | |
chars (chr (ORD_SPACE)) should return the correct value for you to use with chars array. I sujest making your own constant and assigning it that value so that it is not calculated each time you check |
Author: | Jonny Tight Lips [ Thu Feb 12, 2004 4:05 pm ] |
Post subject: | question |
Quote: if ch = chr (num of key) then some of the nums for the keys are: 208 (down), 200 (up), 203 (left), 205 (right) I was wondering what the number would be 4 the space bar and the enter keys?? |
Author: | recneps [ Thu Feb 12, 2004 4:15 pm ] |
Post subject: | |
Its in the Turing Referance, on the first page, i believe and if you have the Turing textbook (which covers next to nothing ;p) its in there.... and if you looked up you would've seen in the examples, "chr(ORD_SPACE)" and enter is just enter i believe...? |
Author: | shorthair [ Thu Feb 12, 2004 4:29 pm ] |
Post subject: | |
ummm if you seach compsci ( i dont know where it is ) i wrote an app ,that tells you the ord of every button on the ascii keyboard , so you just run itand press a key and the number of that key pops up ( sorry to plug the tutorial ) but it would just be a good thing to have while making a game |
Author: | Cervantes [ Thu Feb 12, 2004 4:29 pm ] | ||
Post subject: | |||
for enter just use KEY_ENTER so in the code
|
Author: | Jonny Tight Lips [ Thu Feb 12, 2004 4:31 pm ] | ||
Post subject: | |||
it didn't work for some reason?
I dunno if I did something wrong or what?? PLease help. Oh and just so U know I'm using dos turing so the (KEY_ENTER) stuff doesn't work. |
Author: | Jonny Tight Lips [ Thu Feb 12, 2004 5:55 pm ] |
Post subject: | nm |
never mind I figured it out. space bar = 32 enter = 10 and that program to find all the #'s out is at http://www.compsci.ca/v2/viewtopic.php?t=3130&highlight=ord |
Author: | santabruzer [ Thu Feb 12, 2004 6:00 pm ] |
Post subject: | |
err.. it should be: chars (chr (ORD_SPACE)) |
Author: | Jonny Tight Lips [ Mon Feb 16, 2004 5:15 pm ] | ||
Post subject: | |||
ummm I'm pritty sure I got it right ....
as you can see thats they way I did it becasue I was using dos turing and I don't think it would work the other way so thats the way I did it and it works so w/e. |
Author: | mapleleafs [ Thu Feb 19, 2004 7:56 pm ] |
Post subject: | |
hey, can sumone tell me a way of achieving a 'magnetic' sort of effect. For example, I have a object/picture which is under the control of the user, and when the user presses a certain button, i want the object/picture to automatically move to a set area, say one side of the screen. |
Author: | Cervantes [ Thu Feb 19, 2004 8:49 pm ] | ||
Post subject: | |||
huh? you mean this?
|
Author: | mapleleafs [ Thu Feb 19, 2004 9:15 pm ] |
Post subject: | |
no, not like that. I want the ball to flow to the other area, not just instantaneosly appear there. |
Author: | Tony [ Thu Feb 19, 2004 9:28 pm ] |
Post subject: | |
then you use a forloop and move it pixel by pixel |
Author: | mapleleafs [ Thu Feb 19, 2004 9:42 pm ] |
Post subject: | |
i know that's how you move an object, but i'm asking how you would apply that pixel-by-pixel theory if the object was human controlled(hence, moving at unknown/random positions) , and then at a certain point in time (eg. a button is pressed) , you would get that object to flow to a certain area. cuz i can't just use a for loop and then increment the coordinates because since the object is being moved around by the user, it would be impossible (i think) for the program to know which coordinates to increase to get to the set area. sorry if this is really confusing, but help is much appreciated |
Author: | beard0 [ Fri May 28, 2004 12:11 pm ] | ||
Post subject: | |||
say you want it to flow to (px,py) and it's at (x,y):
|
Author: | The_$hit [ Wed Mar 02, 2005 10:50 pm ] |
Post subject: | |
What if i wanted to attach a picture and move that around what would I do? |
Author: | Tony [ Wed Mar 02, 2005 11:50 pm ] |
Post subject: | |
You have your position variables X/Y, just draw your picture there instead of Draw.Box/Oval/whatever |
Author: | ssr [ Thu Mar 03, 2005 11:19 pm ] | ||||||
Post subject: | |||||||
btw I think
what about
8) |
Author: | Bacchus [ Fri Mar 04, 2005 7:05 am ] |
Post subject: | |
KEY_UP_ARROW and KEY_DOWN_ARROW both work in 4.0.4, i used them b4, 4.0.5 however recognizes them as keys and turns it black to not get them confused with vars |
Author: | ssr [ Sat Mar 12, 2005 10:14 pm ] |
Post subject: | |
Bacchus wrote: KEY_UP_ARROW and KEY_DOWN_ARROW both work in 4.0.4, i used them b4, 4.0.5 however recognizes them as keys and turns it black to not get them confused with vars
so if I make a program using KEY_UP_ARROW, it will still work in Turing 4.04? 8) |
Author: | Bacchus [ Sat Mar 12, 2005 10:18 pm ] |
Post subject: | |
yes, at least they did for me b4 i got 4.0.5, .5 just turns the font black (in .4 it stays blue) that way it doesnt look like a var |
Author: | [Gandalf] [ Sun Mar 13, 2005 3:24 pm ] |
Post subject: | |
Where can I find the list of all the key names? How would I do Space Bar without using ORD_SPACE? KEY_SPACE doesn't work, and I tried a bunch of different combinations... |
Author: | Bacchus [ Sun Mar 13, 2005 10:50 pm ] | ||
Post subject: | |||
|
Author: | Cervantes [ Tue Mar 15, 2005 8:18 am ] |
Post subject: | |
Open the turing reference (F10). Expand Turing Language. Expand PRedefined modules. Select Keyboard. Also, Expand Turing Language. Select Keystroke codes. scroll down. |
Author: | [Gandalf] [ Tue Mar 15, 2005 2:17 pm ] |
Post subject: | |
Thanks a lot! Anyone know why they didn't make a KEY_SPACE - was it because it was just not neccessary? Now I can finally use the spacebar for shooting , thanks again. |
Author: | Bacchus [ Tue Mar 15, 2005 3:49 pm ] |
Post subject: | |
i would guess cause its not necissary, it has a character so |
Author: | [Gandalf] [ Sun May 15, 2005 6:35 pm ] |
Post subject: | |
Alright, here's another question which has been bothering me... I've see this done a lot, but I can't explain it. Is there a reason why you have a seperate if statement for each key pressed? Why not just do elsif? That way you can also have an 'else' if you need it... |
Author: | Cervantes [ Sun May 15, 2005 7:23 pm ] |
Post subject: | |
The reason for this is because it prevents the user from hitting two keys at once. Say you had a shooter. WASD for movement, space bar is jump, weapons are the numbers. If you had a big long if .. elsif ..... elsif .. end if structure, only the first key in that structure would regester. So if you're moving, you might not be able to change your weapon. Or maybe you can't fire while moving. Or maybe you hold up (W) and right (D) and want to go on a diagonal up and right. But instead you only go up. None of this is good... |
Author: | [Gandalf] [ Sun May 15, 2005 7:55 pm ] |
Post subject: | |
Oh oops, I figured that out yesterday, and I just used the elsif method because I only have up/down left/right, but today I only remembered the question Anways, thanks for saving me the trouble of having to figure that out again |
Author: | theguru [ Fri Oct 14, 2005 10:55 pm ] |
Post subject: | |
maybe something is wrong with my turing version but i think the oval's screwed up. i end up getting an '0'. but other than that, this tutorial and examples were really helpful! |
Author: | harishmabish [ Sat Mar 29, 2008 9:40 am ] |
Post subject: | Re: [Tutorial] Character control on screen (Input.KeyDown) |
so how would you move a picture? |
Author: | Mackie [ Sat Mar 29, 2008 10:03 am ] | ||||
Post subject: | RE:[Tutorial] Character control on screen (Input.KeyDown) | ||||
Wow, old post. Use the same method. Instead of drawing a circle, you can draw an image.
|
Author: | tianxiao [ Tue Jan 13, 2009 12:01 pm ] |
Post subject: | RE:[Tutorial] Character control on screen (Input.KeyDown) |
what is the name for spacebar in Input.KeyDown? like KEY_UP_ARROW is for up arrow key. What is it for spacebar? |
Author: | corriep [ Tue Jan 13, 2009 3:01 pm ] |
Post subject: | RE:[Tutorial] Character control on screen (Input.KeyDown) |
instead of key (KEY_UP_ARROW) just use key (' ') |
Author: | Savage Reindeer [ Tue Apr 28, 2009 2:34 pm ] |
Post subject: | RE:[Tutorial] Character control on screen (Input.KeyDown) |
I'm making a DDR game and when I hold the button down changes whether the user got a bad, good, perfect, or missed hit. How can I make it use whatever space it was in the instant that the key was pressed and no other? perhaps I should make a help topic... |
Author: | saltpro15 [ Tue Apr 28, 2009 2:39 pm ] |
Post subject: | RE:[Tutorial] Character control on screen (Input.KeyDown) |
yes, you definitely should instead of reviving old threads, put it in the Turing Help forum please |
Author: | wadler64 [ Sun Mar 03, 2013 1:07 pm ] |
Post subject: | Re: [Tutorial] Character control on screen (Input.KeyDown) |
i dont get the input.down thing |
Author: | Insectoid [ Sun Mar 03, 2013 1:22 pm ] |
Post subject: | RE:[Tutorial] Character control on screen (Input.KeyDown) |
chars is a special array of booleans that correspond to each key on the keyboard. When you call Input.KeyDown(chars), the booleans that correspond to the keys that are pressed are set to true. The unpressed keys are set to false. Remember that it doesn't update itself; it only shows the key states at the time Input.KeyDown was called. Then you can check which keys are pressed by passing the key value or a character as the array index to chars. chars('a') returns 1 if a is pressed. chars('A') returns 1 if shift-a is pressed. chars(KEY_UP_ARROW) is the up arrow key. Usually this is used in a loop. At the top of the loop, you call Input.KeyDown to get the key states. Then you reference chars to decide what to do. For example, if chars (KEY_UP_ARROW) = 1, move up 5 pixels. |
Author: | Insertname [ Sat Mar 09, 2013 2:22 pm ] |
Post subject: | RE:[Tutorial] Character control on screen (Input.KeyDown) |
*Sorry if this post shouldn't be here, anyway... Is there any way to move an object without clearing the screen constantly? For, let's say, a brickbreaker style game, or pong? If so, how would one go about doing such a thing? I've seen some people either draw over the box with the background colour or use some kind of special command that's alien to me, "Draw.Cls ()" Thanks |
Author: | Raknarg [ Sat Mar 09, 2013 6:13 pm ] | ||
Post subject: | RE:[Tutorial] Character control on screen (Input.KeyDown) | ||
Im pretty sure Draw.Cls is the same as cls. You could draw over the object if you wanted to. for instance:
However usually it's a lot faster on the machine to just clear the screen rather than calculating object drawings each time, if you have enough objects. Boxes are much easier to draw than circles. |
Author: | Insertname [ Sat Mar 09, 2013 6:47 pm ] |
Post subject: | RE:[Tutorial] Character control on screen (Input.KeyDown) |
By clearing the screen, as shown in this tutorial, it would also clear other things that aren't meant to be cleared. For instance, Player 2's pong paddle or bricks in super breakout. Would there be a way to use what was said in this tutorial, but without fully clearing the screen or redrawing everything constantly? Is there a command that only clears certain objects at a time or certain areas of the screen? I have a feeling View.UpdateArea will make an appearance. ? |
Author: | Insectoid [ Sat Mar 09, 2013 7:05 pm ] |
Post subject: | RE:[Tutorial] Character control on screen (Input.KeyDown) |
You should be re-drawing everything every frame. With well-constructed draw procedures you just need to throw it all in the main loop and forget about it. If your background is white and your character is a box or circle, it makes sense to just draw a white box/circle over where the character used to be. But if you have a more complicated scene with a background picture and lots of things moving around, this becomes impractical because A)you can't just draw a white box over the background picture. That looks terrible, and B), you'll inevitably end up drawing your white box over another moving thing, which also looks terrible. At this point, it really is much, much easier to clear everything and redraw the whole scene every frame. |
Author: | jesse kazomi [ Tue Jun 17, 2014 5:02 pm ] |
Post subject: | Re: [Tutorial] Character control on screen (Input.KeyDown) |
This is so cool |
Author: | klarakos [ Fri Aug 01, 2014 5:17 am ] |
Post subject: | RE:[Tutorial] Character control on screen (Input.KeyDown) |
hey, can sumone tell me a way of achieving a 'magnetic' sort of effect. For example, I have a object/picture which is under the control of the user, and when the user presses a certain button, i want the object/picture to automatically move to a set area, say one side of the screen. |
Author: | Insectoid [ Sun Aug 03, 2014 7:07 am ] |
Post subject: | RE:[Tutorial] Character control on screen (Input.KeyDown) |
Sure. If the user pushes that button, just set the object's coordinates to whatever spot you want. |
Author: | help [ Mon Jan 18, 2016 2:23 pm ] |
Post subject: | Re: [Tutorial] Character control on screen (Input.KeyDown) |
what would i do to make this work on something other than a mouse and keyboard since i have to make this work on a controller that i have made using a wire to connect to the back of the coputer and comments |
Author: | Insectoid [ Mon Jan 18, 2016 5:52 pm ] |
Post subject: | RE:[Tutorial] Character control on screen (Input.KeyDown) |
We're going to need some more information about your controller. Does it use the parallel port? |
Author: | ProxyKaliber [ Fri Jun 02, 2017 8:46 am ] |
Post subject: | Re: [Tutorial] Character control on screen (Input.KeyDown) |
I know this is an old topic but I'm wondering if there's a way to rotate the circle. I have an idea for a game that I'd like to try making... Inspired by this post here. |