Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Tutorial] Character control on screen (Input.KeyDown)
Index -> Programming, Turing -> Turing Tutorials
Goto page 1, 2, 3, 4, 5  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tony




PostPosted: 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:
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 Very Happy

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!!! Very Happy


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.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: 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



\/


code:
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 here
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
bizkit




PostPosted: 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
Dan




PostPosted: 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.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Tony




PostPosted: Mon Nov 25, 2002 10:02 pm   Post subject: (No 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 Very Happy

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.

code:
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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Dan




PostPosted: 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 Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
JayLo




PostPosted: Wed Dec 04, 2002 12:11 am   Post subject: (No subject)

can't you use a fork process??
Tony




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
Vicous




PostPosted: 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
Tony




PostPosted: Thu Feb 27, 2003 11:51 am   Post subject: (No 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 Wink
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Vicous




PostPosted: 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?
skatelhs




PostPosted: 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.
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




PostPosted: 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.
Tony




PostPosted: Thu May 22, 2003 6:26 pm   Post subject: (No subject)

it should be

code:

if chars (KEY_RIGHT_ARROW) and x <maxx -5 then
                x:=x+5
            end if


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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
¿§¥Jåmës¥§¿




PostPosted: 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
code:

    var spacebar : array 1 .. * of boolean
    if spacebar (ORD_SPACE) then

     end if

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 Confused
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 5  [ 63 Posts ]
Goto page 1, 2, 3, 4, 5  Next
Jump to:   


Style:  
Search: