Computer Science Canada

[tutorial] movement (getch,keydown,mouse)

Author:  DanShadow [ Sun Jan 25, 2004 2:37 am ]
Post subject:  [tutorial] movement (getch,keydown,mouse)

This tutorial is kind of a response to the on in:
http://www.compsci.ca/v2/viewtopic.php?t=3270
Getch
Getch is the command for "get char"...which waits until the user inputs(presses) a button (character) on the keyboard, then it saves it under a variable name. This can be used for buttons too, like (KEY_UP_ARROW), etc.
Input.KeyDown
This does similarly the same thing...except it can handle multiple characters (enabling 2+ player programs). These extra characters are held in a boolean array, thats why the variable is declared like so:
code:

var chars:array char of boolean

Mousewhere
The mouse is an uncommonly used tool for movement. There are easy ways this is done though, like 4 if statements, checking the mouses x and y co-ordinates relative to the players. Then if the player clicks, and (for example) the x and y are greater than the sprites x and y, the sprites x and y increase as a movement step.

I have included a program showing you how each of these movement types work.

Author:  santabruzer [ Sun Jan 25, 2004 12:40 pm ]
Post subject: 

simple and effective.. good job... (just delay the keydown a bit though)

Author:  DanShadow [ Sun Jan 25, 2004 4:55 pm ]
Post subject: 

tx...and it doesnt matter too much about the delay...im just giving an example, doesnt have to be perfect, heh Laughing

Author:  shorthair [ Sun Jan 25, 2004 5:09 pm ]
Post subject: 

Its good , well done , you have put alot of effor into making this ( that i can see ) , keep up the good code

Author:  Paul [ Sun Feb 01, 2004 2:47 pm ]
Post subject: 

Wow, that IS really well done, it taught a person like me to move stuff with keyboard and mouse in 2 minutes, thanks!

Author:  Cervantes [ Sun Feb 01, 2004 2:54 pm ]
Post subject: 

necessities for making a great game!
Paulbian make a game now and post it! be as creative as you can! Very Happy

(well, if you want to Eh)

Author:  Paul [ Sun Feb 01, 2004 2:57 pm ]
Post subject: 

ahem... does a moving circle which grows when you press enter and shrinks when you press backspace count as a game... Embarassed

Author:  Cervantes [ Sun Feb 01, 2004 3:04 pm ]
Post subject: 

well that's certainly creative, wouldn't think its that much fun though. Eh

don't post it yet, make it really good Smile

Author:  jonos [ Sun Feb 01, 2004 3:57 pm ]
Post subject: 

ill post a game cervantes, i just need to look at the tutr.l

Author:  Cervantes [ Sun Feb 01, 2004 4:06 pm ]
Post subject: 

did you know this stuff before? / have you already used this stuff?

Author:  jonos [ Sun Feb 01, 2004 4:22 pm ]
Post subject: 

never used it before, my class only learned stuff to arrays. im gonna learn it, i just need to learn collision detection then you wil have your gaem;

Author:  Cervantes [ Sun Feb 01, 2004 4:30 pm ]
Post subject: 

awesome. go for it Smile

be creative!

Author:  Paul [ Sun Feb 01, 2004 4:32 pm ]
Post subject: 

Hey, you think I can use whatdotcolor for collision detection?

Author:  Cervantes [ Sun Feb 01, 2004 4:45 pm ]
Post subject: 

yes that's one way, ask dodge and its the only way Smile

Author:  Andy [ Sun Feb 01, 2004 4:55 pm ]
Post subject: 

cervantes -5 bits to you...
of course u can use whatdotcolor! everybody should use whatdotcolor for the perfect collision detection... and i'm not kidding about perfect... if u think really hard about it, you'll get plus as a bonus you'll love whatdotcolor

Author:  Paul [ Sun Feb 01, 2004 4:58 pm ]
Post subject: 

dodge, you should do a tutorial on whatdotcolor collision detection. It sounds like you've mastered it.

Author:  Andy [ Sun Feb 01, 2004 5:02 pm ]
Post subject: 

hmmm good idea... i'll do it one of these days when i'm not so busy wit everytin... but i can tell u the basic idea of it... basicly you have a picture or an array of pictures since there is View.UpdateArea of each object and its surroundings... and instead of having them multicolored, you have each object as one color, then you just use whatdotcolor to see if something touched... but each time something moves, you'll have to re update the pictures

Author:  jonos [ Sun Feb 01, 2004 5:17 pm ]
Post subject: 

i think i just did the whatdotcolor collisions detection in the help forum under collision detection, i fixed it htough something is still wrong...

Author:  TheXploder [ Sun Feb 01, 2004 5:54 pm ]
Post subject: 

Arrays could work much better if you use coulored tiles...

for every 1 in the array collide...

but whatdotcolour is easier to use when your making a helicopter fly through the cave type of game, or worms, where your terrain could be all over the place... right Dodge?

Author:  Cervantes [ Sun Feb 01, 2004 6:01 pm ]
Post subject: 

whatdotcolour would be the easiest way for the helicopter game... It would be VERY hard to do it without whatdotcolour, you'd have to use arrays to hold the position of the blocks that are in your path.

whatdotcolour is also useful in cool programs where you have funky colours all over the place and something bouncing around those infront of those colours and changing whenever it hits a colour Smile basically any funky program Very Happy

Author:  Andy [ Sun Feb 01, 2004 6:29 pm ]
Post subject: 

thats more like it, +10 bits

Author:  Paul [ Sun Feb 01, 2004 6:32 pm ]
Post subject: 

Actually whatdotcolor is the only way I understand how to use, in a program like the one I proposed with manuvering thru a tube with different width. Whatdotcolor is the greatest. Embarassed

Author:  Andy [ Sun Feb 01, 2004 6:35 pm ]
Post subject: 

man, if everybody learned whatdotcolor before all things, the world will be a much better place +5 bits

Author:  DanShadow [ Fri Feb 06, 2004 2:20 pm ]
Post subject: 

Wow...this got kinda popular. Im gonna go write a very simple tutorial of collision detection using whatdotcolor.Hail Whatdotcolor

Author:  djlenny_3000 [ Tue Mar 23, 2004 5:43 pm ]
Post subject: 

question: in key down is it possible to use 2 buttons so for example the ball could go on an angle

and also what are the inputs in keydown for letters such as "w" or "s"

thnx

Author:  jonos [ Tue Mar 23, 2004 8:51 pm ]
Post subject: 

ok to your first question: yes. just make all the separate key presses in a separate if statement. don't use elsif's, just make new if statements.

as to your second question: yes. i believe that it is 'character' (so the key you want pressed in single quotes). im not sure on that one, someone may correct it if it is wrong.

Author:  Cervantes [ Tue Mar 23, 2004 9:48 pm ]
Post subject: 

correct.
but don't forget to put it in brackets.
code:

if chars ('w') then

Author:  djlenny_3000 [ Tue Mar 23, 2004 10:01 pm ]
Post subject: 

right thnx now i can finally finish my pac man game for my compsci teacher
BTW
which is probably better using 2 arrows to make a diagonal movement
or designate letters as the controls and use the coresponding ones as diagonals

Author:  jonos [ Tue Mar 23, 2004 10:17 pm ]
Post subject: 

you could use the numpad, but i think that using 2 keys would be best. its the easiest to remember and many people use the combonations of keys in other games.

Author:  DanShadow [ Fri Jul 30, 2004 9:04 pm ]
Post subject: 

here is the answer to the moving on an angle question:
code:

if key(KEY_UP_ARROW) and key(KEY_RIGHT_ARROW) then
x+=1
y+=1
elsif ....%%

end if

etc. etc. very easy stuff

Author:  Terror Byte [ Tue Sep 13, 2005 7:53 pm ]
Post subject:  My program's not working..

Hello guys.

I'm new here, first post actually. I'm new to Turing, in Grade 10 right now. I started learning Turing before, but then I gave up.. Anyways, I've taught myself a bunch of things with the help of the tutorials here Very Happy (arrays, draw commands, loops, ifs, cases, etc.)

I was trying to do movement with keydown and I wrote the code and all and it ran but I after whatever I pressed, my circle didn't move.

code:


var chars : array char of boolean
var ballx, bally : int := 300

color (0)
colorback (7)
drawfill (0,0,7,7)
drawfilloval (ballx,bally,18,18,55)

locate (1,25)
put "Ball Movement.      By: Adeel Syed."

loop
    setscreen ("offscreenonly")
    View.Update
   
    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW)
        then bally := bally + 2
    elsif chars (KEY_DOWN_ARROW)
        then bally := bally - 2
    elsif chars (KEY_LEFT_ARROW)
        then ballx := ballx - 2
    elsif chars (KEY_RIGHT_ARROW)
        then ballx := ballx + 2
    elsif chars (KEY_RIGHT_ARROW) and chars (KEY_UP_ARROW)
        then ballx := ballx + 2
        bally := bally + 2
    elsif chars (KEY_RIGHT_ARROW) and chars (KEY_DOWN_ARROW)
        then ballx := ballx + 2
        bally := bally - 2
    elsif chars (KEY_LEFT_ARROW) and chars (KEY_UP_ARROW)
        then ballx := ballx - 2
        bally := bally + 2
    elsif chars (KEY_LEFT_ARROW) and chars (KEY_DOWN_ARROW)
        then ballx := ballx - 2
        bally := bally - 2
    end if
end loop


That's what I wrote, very noobish indeed. And it won't work. Please don't flame me... Just point out the stupid mistake(s) I am making.

Thank you,

Terror Byte.

Author:  Cervantes [ Tue Sep 13, 2005 8:05 pm ]
Post subject: 

Welcome to the forums!

A couple of things:

  1. We prefer if questions are posted in the Help forum, rather than in the Tutorial thread corresponding to the topic in question.

  2. There is no need to put the "setscreen ("offscreenonly")" within your loop. It need only be stated once.

  3. If you want your circle to move, you need to put the code to draw the circle inside the loop. That is crucial. Wink

  4. Instead of using a long if .. elsif .. end if structure to account for diagonals, use four if .. end if statements. There are two reasons you need to do this. First, the diagonal if statements will never be reached, the way you've got it set up. If you are pressing up and left, then the test far above that specific test will be passed, because you are pressing the up arrow. Thus, up and left will never be tested. You could reverse the order of things, if you wanted. Or to make things easier, you do this:
    code:

    if chars (KEY_UP_ARROW) then
        bally += 2  % same as bally := bally + 2
    end if
    if chars (KEY_DOWN_ARROW) then
       bally -= 2
    end if
    if chars (KEY_RIGHT_ARROW) then
       ballx += 2
    end if
    if chars (KEY_LEFT_ARROW) then
       ballx -= 2
    end if

    This will account for everything, and will also solve the problem of hitting up and down resulting in moving up.


If you have further questions, post them here (for this specific case only!) and I or another mod will move these posts to the Turing Help forum so they can have their own thread. Smile

Author:  Terror Byte [ Tue Sep 13, 2005 9:18 pm ]
Post subject: 

Ah well that makes sense Very Happy

The circle is refreshed every time inside the loop..

There's still a problem though. The circle leaves a trail, isn't View.Update supposed to take care of that?

EDIT: Drawfill, isn't that supposed to go over it every time in the loop?

It's still not working for me.

Author:  [Gandalf] [ Tue Sep 13, 2005 9:27 pm ]
Post subject: 

I'm not quite sure what your drawfill does, but to remove the trail you have to have a cls (clear screen) inside your loop.

Also, much of your code above the loop is very pointless and removable.

Author:  gameover [ Sun Dec 10, 2006 11:27 pm ]
Post subject: 

i cant run it =.=' it says "update is not in the export list of view"
someone help i rly need to learn this mouse clicking stuff by next week my isu is due =.=' Exclamation

Author:  ericfourfour [ Mon Dec 11, 2006 12:57 am ]
Post subject: 

Are you trying to use View.Update by any chance? Remember, its case sensitive.

Author:  Cervantes [ Mon Dec 11, 2006 1:02 am ]
Post subject: 

Older versions of Turing don't have View.Update, so if you're running a rather old version (I think pre 4.x) then you won't be able to use it.

Author:  gameover [ Tue Dec 12, 2006 7:25 pm ]
Post subject: 

I am using turing 4.0 Confused so it wont work? Question

Author:  Cervantes [ Tue Dec 12, 2006 7:57 pm ]
Post subject: 

No, if you're on version 4.0, it should work. Double check your code to make sure you're using it properly. If you are, try going to C:\program files\Turing\Support\predefs\View.tu and looking to see if Update is in the export list.

Author:  gameover [ Wed Dec 13, 2006 6:08 pm ]
Post subject: 

Update isnt on the export list... so what do i do about it Question

Author:  Clayton [ Wed Dec 13, 2006 8:27 pm ]
Post subject: 

You'll have to purchase a newer copy yourself, or get a newer copy from your school (if they have it). This is one of the main problems with Turing, it costs money, so as soon as its depreciated, you have to pay another $80+ for a new copy (if your school doesn't have it) Confused

Author:  gameover [ Thu Dec 14, 2006 8:12 pm ]
Post subject: 

oh wow =.=' thanks lol i gotta find some other way to learn how to use the mouse commands Shocked

Author:  frank26080115 [ Tue Dec 26, 2006 2:25 pm ]
Post subject: 

what if i wanted to use W A S D for movement control?

Author:  Cervantes [ Tue Dec 26, 2006 4:13 pm ]
Post subject: 

frank26080115 wrote:
what if i wanted to use W A S D for movement control?

What if you want to do that? I don't forsee any difficulty. It's the same code as using the arrow pad, except you change
code:
if keys (KEY_UP_ARROW) then

to
code:
if keys ('w') then


: