
-----------------------------------
DanShadow
Sun Jan 25, 2004 2:37 am

[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: 

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.

-----------------------------------
santabruzer
Sun Jan 25, 2004 12:40 pm


-----------------------------------
simple and effective.. good job... (just delay the keydown a bit though)

-----------------------------------
DanShadow
Sun Jan 25, 2004 4:55 pm


-----------------------------------
tx...and it doesnt matter too much about the delay...im just giving an example, doesnt have to be perfect, heh :lol:

-----------------------------------
shorthair
Sun Jan 25, 2004 5:09 pm


-----------------------------------
Its good , well done , you have put alot of effor into making this ( that i can see ) , keep up the good code

-----------------------------------
Paul
Sun Feb 01, 2004 2:47 pm


-----------------------------------
Wow, that IS really well done, it taught a person like me to move stuff with keyboard and mouse in 2 minutes, thanks!

-----------------------------------
Cervantes
Sun Feb 01, 2004 2:54 pm


-----------------------------------
necessities for making a great game! 
Paulbian make a game now and post it! be as creative as you can! :D

(well, if you want to :eh:)

-----------------------------------
Paul
Sun Feb 01, 2004 2:57 pm


-----------------------------------
ahem... does a moving circle which grows when you press enter and shrinks when you press backspace count as a game... :oops:

-----------------------------------
Cervantes
Sun Feb 01, 2004 3:04 pm


-----------------------------------
well that's certainly creative, wouldn't think its that much fun though.  :eh:

don't post it yet, make it really good :)

-----------------------------------
jonos
Sun Feb 01, 2004 3:57 pm


-----------------------------------
ill post a game cervantes, i just need to look at the tutr.l

-----------------------------------
Cervantes
Sun Feb 01, 2004 4:06 pm


-----------------------------------
did you know this stuff before? / have you already used this stuff?

-----------------------------------
jonos
Sun Feb 01, 2004 4:22 pm


-----------------------------------
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;

-----------------------------------
Cervantes
Sun Feb 01, 2004 4:30 pm


-----------------------------------
awesome. go for it :) 

be creative!

-----------------------------------
Paul
Sun Feb 01, 2004 4:32 pm


-----------------------------------
Hey, you think I can use whatdotcolor for collision detection?

-----------------------------------
Cervantes
Sun Feb 01, 2004 4:45 pm


-----------------------------------
yes that's one way, ask dodge and its the only way :)

-----------------------------------
Andy
Sun Feb 01, 2004 4:55 pm


-----------------------------------
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

-----------------------------------
Paul
Sun Feb 01, 2004 4:58 pm


-----------------------------------
dodge, you should do a tutorial on whatdotcolor collision detection. It sounds like you've mastered it.

-----------------------------------
Andy
Sun Feb 01, 2004 5:02 pm


-----------------------------------
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

-----------------------------------
jonos
Sun Feb 01, 2004 5:17 pm


-----------------------------------
i think i just did the whatdotcolor collisions detection in the help forum under collision detection, i fixed it htough something is still wrong...

-----------------------------------
TheXploder
Sun Feb 01, 2004 5:54 pm


-----------------------------------
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?

-----------------------------------
Cervantes
Sun Feb 01, 2004 6:01 pm


-----------------------------------
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 :)  basically any funky program :D

-----------------------------------
Andy
Sun Feb 01, 2004 6:29 pm


-----------------------------------
thats more like it, +10 bits

-----------------------------------
Paul
Sun Feb 01, 2004 6:32 pm


-----------------------------------
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. :oops:

-----------------------------------
Andy
Sun Feb 01, 2004 6:35 pm


-----------------------------------
man, if everybody learned whatdotcolor before all things, the world will be a much better place +5 bits

-----------------------------------
DanShadow
Fri Feb 06, 2004 2:20 pm


-----------------------------------
Wow...this got kinda popular. Im gonna go write a very simple tutorial of collision detection using whatdotcolor.Hail Whatdotcolor

-----------------------------------
djlenny_3000
Tue Mar 23, 2004 5:43 pm


-----------------------------------
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

-----------------------------------
jonos
Tue Mar 23, 2004 8:51 pm


-----------------------------------
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.

-----------------------------------
Cervantes
Tue Mar 23, 2004 9:48 pm


-----------------------------------
correct.
but don't forget to put it in brackets.

if chars ('w') then


-----------------------------------
djlenny_3000
Tue Mar 23, 2004 10:01 pm


-----------------------------------
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

-----------------------------------
jonos
Tue Mar 23, 2004 10:17 pm


-----------------------------------
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.

-----------------------------------
DanShadow
Fri Jul 30, 2004 9:04 pm


-----------------------------------
here is the answer to the moving on an angle question:

if key(KEY_UP_ARROW) and key(KEY_RIGHT_ARROW) then
x+=1
y+=1
elsif ....%%

end if

etc. etc. very easy stuff

-----------------------------------
Terror Byte
Tue Sep 13, 2005 7:53 pm

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 :D (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.



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.

-----------------------------------
Cervantes
Tue Sep 13, 2005 8:05 pm


-----------------------------------
Welcome to the forums!

A couple of things:

We prefer if questions are posted in the Help forum, rather than in the Tutorial thread corresponding to the topic in question.

There is no need to put the "setscreen ("offscreenonly")" within your loop.  It need only be stated once.

If you want your circle to move, you need to put the code to draw the circle inside the loop.  That is crucial. :wink:

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:

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. :)

-----------------------------------
Terror Byte
Tue Sep 13, 2005 9:18 pm


-----------------------------------
Ah well that makes sense :D

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.

-----------------------------------
[Gandalf]
Tue Sep 13, 2005 9:27 pm


-----------------------------------
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.

-----------------------------------
gameover
Sun Dec 10, 2006 11:27 pm


-----------------------------------
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 =.=' :!:

-----------------------------------
ericfourfour
Mon Dec 11, 2006 12:57 am


-----------------------------------
Are you trying to use View.Update by any chance? Remember, its case sensitive.

-----------------------------------
Cervantes
Mon Dec 11, 2006 1:02 am


-----------------------------------
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.

-----------------------------------
gameover
Tue Dec 12, 2006 7:25 pm


-----------------------------------
I am using turing 4.0 :? so it wont work? :?:

-----------------------------------
Cervantes
Tue Dec 12, 2006 7:57 pm


-----------------------------------
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.

-----------------------------------
gameover
Wed Dec 13, 2006 6:08 pm


-----------------------------------
Update isnt on the export list... so what do i do about it :?:

-----------------------------------
Clayton
Wed Dec 13, 2006 8:27 pm


-----------------------------------
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) :?

-----------------------------------
gameover
Thu Dec 14, 2006 8:12 pm


-----------------------------------
oh wow =.=' thanks lol i gotta find some other way to learn how to use the mouse commands :shock:

-----------------------------------
frank26080115
Tue Dec 26, 2006 2:25 pm


-----------------------------------
what if i wanted to use W A S D for movement control?

-----------------------------------
Cervantes
Tue Dec 26, 2006 4:13 pm


-----------------------------------
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
if keys (KEY_UP_ARROW) then
to
if keys ('w') then
