Exiting and re-entering multiple loops
Author |
Message |
Hack.saw
|
Posted: Tue May 09, 2006 2:57 pm Post subject: Exiting and re-entering multiple loops |
|
|
This is my first post... so be nice Anyway im in a grade 10 TIK class where you use very very basic code. I went a little bit further and used the help menu to figure out how to use getch (key) to move a character.
But now I have a problem my teacher can't help me with... the loop used to keep getting input from the key board of course never ends so nothing else can run. Is it possible to leave a loop using exit when nothing is being touched/pressed down and then re-enter the loop if something is pressed down? Is it possible for two loops to run at once?
(I will attach my file below when I get back on my school comp for some constructive critisism)
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: Tue May 09, 2006 3:23 pm Post subject: (No subject) |
|
|
Welcome to CompSci.ca, Hack.saw!
To directly answer your question, yes. It is possible to have two loops running simultaneously. However, you have to realize that they're not actually running simultaneously. Your processor can only do one thing at a time. Thus, unless you have a dual processor machine, only one loop at a time can run. To imitate two loops running simultaneously, we use something called threads. This is handled by the operating system (Windows, in this case). Turing uses a different word than "thread": turing calls them processes. You can probably solve your problem like this.
HOWEVER, processes in Turing are implemented very poorly. They don't actually run code in a 1:1 ratio. It's rather random. Unless you want to go very far and implement a system that steadies and controls how the processes run, I highly recommend you don't use processes. In fact, this has been said so many times that I wrote an article about it.
So, if you're not going to use processes, what are you going to do? The thing you need to realize is that you don't need two loops running simultaneously. Yes, getch pauses the program for input, and that's probably a bad thing. There are two things you can do to fix this.
- You can wrap the getch line in a if hasch then statement. hasch is a function that returns true if a key has been pressed (if a key is in the keyboard buffer). If a key hasn't been pressed (keyboard buffer is empty) then it returns false.
code: |
loop
if hasch then
getch (key)
end if
% Do other stuff
end loop
|
This won't stall your program because it's getch that's stalling the program. Wrapped inside this if statement, the getch line is only reached when there is a key in the keyboard buffer. And if there is a key in the keyboard buffer, then getch immediately grabs that key, no waiting in line.
Alternatively, you can use Input.KeyDown. This is probably the better choice, especially if you're doing something for gaming. Input.KeyDown allows you to check whether a certain key(s) is pressed, rather than returning the key that is pressed. So I could check if the CTRL key is pressed as well as the 'c' key, or whatever. More information in Tony's tutorial.
|
|
|
|
|
![](images/spacer.gif) |
Hack.saw
|
Posted: Tue May 09, 2006 6:04 pm Post subject: (No subject) |
|
|
O.K. thanks for help,I used the code from the tutorial and someof your hasch to make a simple program that creates a growing box at the bottom of the screen and allows you to move a little red circle at the same time. Tommorow I'll have to completely redo that part of my game because Input.KeyDown works waaaayyyyy better than getch.
code: | var c : int
c :=1
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
c := c + 1
drawfillbox (20 + c, 20, 40, 40, blue)
View.Update
drawfillbox (20 + c, 20, 40 + c, 40, white)
end loop
drawfillbox (c, 20, 40, 40, blue)
|
|
|
|
|
|
![](images/spacer.gif) |
Hack.saw
|
Posted: Tue May 09, 2006 6:09 pm Post subject: (No subject) |
|
|
Also, I was just wondering if I can keep posting on this thread with new problems that may not be related to the first or should I make a new topic all together?
|
|
|
|
|
![](images/spacer.gif) |
Hack.saw
|
Posted: Tue May 09, 2006 6:10 pm Post subject: (No subject) |
|
|
Also, I was just wondering if I can keep posting on this thread with new problems that may not be related to the first or should I make a new topic all together?
|
|
|
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: Tue May 09, 2006 6:12 pm Post subject: (No subject) |
|
|
Hack.saw wrote: Also, I was just wondering if I can keep posting on this thread with new problems that may not be related to the first or should I make a new topic all together? ![Confused Confused](http://compsci.ca/v3/images/smiles/icon_confused.gif)
Do what you think is best.
If your next question is related to this (getch/Input.KeyDown/looping), by all means, post here. If you think it's rather different, make a new topic.
|
|
|
|
|
![](images/spacer.gif) |
Hack.saw
|
Posted: Tue May 09, 2006 6:54 pm Post subject: (No subject) |
|
|
I've started a new mini demo program to test my new looping abilities and am making a maze that a colour changing oval must go through. Now I know I've seen this before on a help thread but I couldn't find it. I need the oval to not be able ot go through the first green wall I have made. So far I have whats the then that should come after this?
P.S. Sorry if I have a few questions right now, my TIK class isn't very good so I am uber newb right now
|
|
|
|
|
![](images/spacer.gif) |
Hack.saw
|
Posted: Tue May 09, 2006 7:00 pm Post subject: (No subject) |
|
|
oops, if c=>219 or <221 is supposed to be x > 219 and I guess you can't have > and < in same boolean
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: Tue May 09, 2006 7:32 pm Post subject: (No subject) |
|
|
If you place that if statement correctly (before updating the x and y positions based on velocity), you could set the velocity of the oval to 0. If you're not using a velocity, the easiest thing to do would be to reset the x or y coordinates to some known value (just outside where the wall is).
You're using whatdotcolour, I take it?
|
|
|
|
|
![](images/spacer.gif) |
Hack.saw
|
Posted: Tue May 09, 2006 8:06 pm Post subject: (No subject) |
|
|
Nope have no clue what whatdotcolour is lol... so far I have code: | if chars (KEY_RIGHT_ARROW) then
x := x + 5
if x > 194 and y < 400 then %...checks if player is hitting first wall
x := x - 5
if y > 385 then %...checks if player can go through first space
x := x + 5
end if
if x > 200 and y > 400
then
x := x - 5
end if
end if
end if |
Never mind the second if x > 200 and y > 400
then
x := x - 5
cause I was just screwing around and it dosn't work (atleast i don't think so). So I have it so that you can't pass the first green wall but that leaves it so you can only pass the next black wall at the same place. Is there anyway to stop using the first "if" when the oval passes the first wall?
Description: |
|
![](http://compsci.ca/v3/pafiledb/images/icons/clip.gif) Download |
Filename: |
demogame.t |
Filesize: |
1012 Bytes |
Downloaded: |
56 Time(s) |
|
|
|
|
|
![](images/spacer.gif) |
Clayton
![](http://compsci.ca/v3/uploads/user_avatars/1718239683472e5c8d7e617.jpg)
|
Posted: Tue May 09, 2006 8:46 pm Post subject: (No subject) |
|
|
you could make a boolean variable to check the balls position so you can check to see what area of the screen you need to check using your ifs. now, about that for loop that is looping through colors 1..200, wow, that slows it down so much, if you are using it like i think you are trying to (idk for sure) which is making the ball a different color for each time you go through the loop, the way you have it it doesnt quite work that way, if you want to have it do something like that you should have something like this
[syntax="Turing & Pseudo"]
var culor:int:=1
var counter:int:=0
loop
draw ball (x,y,culor)
check for collision
change coordinates accordingly
change culor number
if culor= maxcolor then
culor:=1
end if
end loop
[/syntax]
apply that to your program ( if what i think is right...)
|
|
|
|
|
![](images/spacer.gif) |
Hack.saw
|
Posted: Wed May 10, 2006 9:09 am Post subject: (No subject) |
|
|
wow... this game is running terribly slow on the school computers, at home it was so smooth i couldnt believe it. How do you check for collsions? I read the tutorial but I didn't really understand it.
|
|
|
|
|
![](images/spacer.gif) |
Hack.saw
|
Posted: Wed May 10, 2006 9:29 am Post subject: (No subject) |
|
|
The culor thing worked great and sped up the game so much i had to put in dealys still not sure how to do the hit detection though so far i have code: | loop
Input.KeyDown (chars)
if chars (KEY_UP_ARROW) then
y := y + 5
delay (10)
end if
if chars (KEY_DOWN_ARROW) then
y := y - 5
delay (10)
end if
if chars (KEY_RIGHT_ARROW) then
x := x + 5
delay (10)
if x > 194 and y < 380 then
x := x - 5
end if
end if
if chars (KEY_LEFT_ARROW) then
x := x - 5
delay (10)
end if
drawfilloval (x, y, 10, 10, culor)
View.Update
culor := culor + 1
if culor = maxcolour then
culor := 1
back
end if
end loop
|
|
|
|
|
|
![](images/spacer.gif) |
do_pete
![](http://i38.photobucket.com/albums/e112/do_pete/1943.gif)
|
Posted: Wed May 10, 2006 11:43 am Post subject: (No subject) |
|
|
You'll need something along the lines of:
code: | x := max (lowerbound, min (x, upperbound)) |
or
code: | if x > upperbound then
x := upperbound
elsif x < lowerbound then
x := lowerbound
end if
|
|
|
|
|
|
![](images/spacer.gif) |
Hack.saw
|
Posted: Fri May 12, 2006 9:26 am Post subject: (No subject) |
|
|
... Well I used all the stuff from this thread in my adventure game and it worked perfectly, thanks guys I only had one problem with extreme flashiness but I think it was just becasue I missed a few View.Updates >< But unfortunatley my schools network went down yesterday and when I saved my game it saved all blacnk files over top of everything or something like that. All I know is my game is gone (including my backups for some reason) so I'll be starting over. Atleast now i know all this stuff for when I re-make it Thanks again for all your help
|
|
|
|
|
![](images/spacer.gif) |
|
|