Input.KeyDown Problems... Seems to keep the value of the key until something happens...
Author |
Message |
Shadow456732
|
Posted: Tue Dec 18, 2012 7:48 pm Post subject: Input.KeyDown Problems... Seems to keep the value of the key until something happens... |
|
|
What is it you are trying to achieve?
This program is essentially like one of those asteroid dodge programs. You are the ball, avoid all in coming objects. Though, the problem is, that when you lose, the game ends.
I want to make it so clicking enter will bring you back to the main menu. And then you click enter once more to start the game.
What is the problem you are having?
When i click enter in the end to restart the loop, it seems to start where the game starts... So it dosen't show the intructions, and it dosen't wait for the input.
Describe what you have tried to solve this problem
Tried putting delay's in a few places to see wether or not it actually did go through that code. It seemed to, but it would assume that enter was clicked when it wasn't.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing: |
setscreen ("graphics:500;700")
View.Set ("nobuttonbar")
View.Set ("offscreenonly")
var jumpheight, flor, radius, endcolor, score, button, mx, my, font : int
var x, y, xpixelchange, ypixelchange, distance : real
var jump, buffer, fall, endgame : boolean
var left, right, up, down : char
var key : array char of boolean
var asteroid, aradius, acolor : array 1 .. 100 of int
var aspeed, ax, ay : array 1 .. 100 of real
var num : int
loop
font := Font.New ("serif:12")
num := 10
flor := 100
jumpheight := flor + 150
radius := 15
x := (maxx / 2)
y := flor + radius
xpixelchange := 0. 5
ypixelchange := 0. 6
left := 'a'
right := 'd'
up := (chr (ORD_SPACE ))
down := 's'
buffer := true
jump := false
fall := false
endgame := false
score := 0
endgame := false
for i : 1 .. num
ax (i ) := Rand.Int (0, maxx)
aradius (i ) := Rand.Int (15, 20)
aspeed (i ) := Rand.Int (2, 9) / 10
ay (i ) := maxy + aradius (i )
acolor (i ) := Rand.Int (0, 244)
end for
Draw.FillBox(1, 1, 10, 10, black)
Font.Draw ("- Welcome to balloon dodge! Your Objective Is To Dodge Oncoming Ballons!", 1, round (maxy / 2), font, red)
Font.Draw ("- Use A and D to move left and right. Spacebar is to jump", 1, round (maxy / 2) - 15, font, red)
Font.Draw ("- Click p to pause the game ,click any keyboard key to resume", 1, round (maxy / 2) - 30, font, red)
Font.Draw ("- Hit The Enter Key To Begin!", 1, round (maxy / 2) - 45, font, red)
View.Update
loop
Input.KeyDown (key )
if key (KEY_ENTER ) then
exit
else
buffer := true
end if
end loop
loop
cls
Draw.FillBox (0, flor, maxx, maxy, gray)
Draw.FillBox (0, 0, maxx, flor, green)
Draw.FillBox (0, flor - 10, maxx, flor, red)
Input.KeyDown (key )
if key (left ) and x >= radius then
x := x - xpixelchange
elsif key (right ) and x <= maxx - radius then
x := x + xpixelchange
end if
Input.KeyDown (key )
if key (up ) and buffer = true then
jump := true
buffer := false
elsif jump = true and y < jumpheight then
y := y + ypixelchange
if round (y ) >= jumpheight then
jump := false
fall := true
end if
elsif fall = true and y >= flor + radius then
y := y - ypixelchange
if y <= flor + radius then
fall := false
end if
end if
Input.KeyDown (key )
if key (up ) then
buffer := false
else
if round (y ) <= flor + radius then
buffer := true
end if
end if
Draw.FillOval (round (x ), round (y ), radius, radius, blue)
for i : 1 .. num
Draw.FillOval (round (ax (i )), round (ay (i )), aradius (i ), aradius (i ), acolor (i ))
Draw.Oval (round (ax (i )), round (ay (i )), aradius (i ), aradius (i ), black)
ay (i ) := ay (i ) - aspeed (i )
if ay (i ) <= flor + aradius (i ) then
ax (i ) := Rand.Int (0, maxx)
aradius (i ) := Rand.Int (15, 20)
aspeed (i ) := Rand.Int (2, 9) / 10
ay (i ) := maxy + aradius (i )
acolor (i ) := Rand.Int (0, 244)
score := score + 1
end if
distance := sqrt ((ax (i ) - x ) ** 2 + (ay (i ) - y ) ** 2)
if distance <= aradius (i ) + radius then
endgame := true
for l : 1 .. 150
delay (10)
View.Update
Draw.FillOval (round (ax (i )), round (ay (i )), aradius (i ), aradius (i ), acolor (i ))
Draw.Oval (round (ax (i )), round (ay (i )), aradius (i ), aradius (i ), black)
endcolor := acolor (i )
aradius (i ) := aradius (i ) + 5
end for
end if
end for
Input.KeyDown (key )
if key ('p') then
loop
Draw.FillBox (0, 0, maxx, maxy, black)
Font.Draw ("Paused! Click Any Key To Resume Game", 10, round (maxy / 2), font, red)
View.Update
exit when hasch
end loop
end if
if endgame = true then
exit
end if
View.Update
end loop
colorback (endcolor )
cls
Font.Draw ("YOU LOSE! Your Score Was: " + intstr (score ), 10, round (maxy / 2), font, whatdotcolor (1, 1) + 10)
Font.Draw ("Click Enter To Replay", 10, round (maxy / 2) - 15, font, whatdotcolor (1, 1) + 10)
View.Update
loop
Input.KeyDown (key )
if key (KEY_ENTER ) then
exit
end if
end loop
end loop
|
Please specify what version of Turing you are using
The second newest. As the newest has a bug with making stand alone programs lol. Thanks for any help p.s I would like to avoid using getch/hasch as i find it somewhat slow. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
TerranceN
|
Posted: Tue Dec 18, 2012 9:20 pm Post subject: RE:Input.KeyDown Problems... Seems to keep the value of the key until something happens... |
|
|
Since you have inner loops that wait for you to press enter at the beginning and the end of the outer loop, you essentially have a situation like this:
Turing: |
var keys : array char of boolean
put "a"
loop % this would be for the 'you lose' screen
Input.KeyDown(keys )
exit when keys (KEY_ENTER )
end loop
put "b"
loop % and this would be for the instructions
Input.KeyDown(keys )
exit when keys (KEY_ENTER )
end loop
put "c"
|
If you run that, you will find that the program runs so fast that after pressing enter to get out of the 'you lose' loop, it goes through the instructions loop before you can take your finger off enter.
One way to fix this problem is to keep track of the keys that were down the previous iteration of the loop, then only exit when enter is down but wasn't before (i.e. right when it is pressed). This gives the desired behaviour of having to press enter twice to get through both loops:
Turing: |
var keys : array char of boolean
var preKeys : array char of boolean
put "a"
loop
preKeys := keys
Input.KeyDown(keys )
exit when keys (KEY_ENTER ) and ~preKeys (KEY_ENTER )
end loop
put "b"
loop
preKeys := keys
Input.KeyDown(keys )
exit when keys (KEY_ENTER ) and ~preKeys (KEY_ENTER )
end loop
put "c"
|
|
|
|
|
|
|
Shadow456732
|
Posted: Tue Dec 18, 2012 9:33 pm Post subject: RE:Input.KeyDown Problems... Seems to keep the value of the key until something happens... |
|
|
Hmm, sounds good, though could i just change the value of key before the check? I'll try that out as well thanks
EDIT: Also, is there a way to "clear" the value of Input.KeyDown?? |
|
|
|
|
|
Shadow456732
|
Posted: Tue Dec 18, 2012 9:40 pm Post subject: RE:Input.KeyDown Problems... Seems to keep the value of the key until something happens... |
|
|
Alright! After trying it out, it seems to work. Though for future reference, what does the "~" do in this situation? |
|
|
|
|
|
TerranceN
|
Posted: Tue Dec 18, 2012 9:43 pm Post subject: RE:Input.KeyDown Problems... Seems to keep the value of the key until something happens... |
|
|
The problem with changing the value of key is you need to continuously keep updating key using Input.KeyDown to determine when enter is pressed. So even if you change the value of key, the next Input.KeyDown will overwrite your change.
'~' is just shorthand for 'not'. |
|
|
|
|
|
Shadow456732
|
Posted: Tue Dec 18, 2012 9:47 pm Post subject: RE:Input.KeyDown Problems... Seems to keep the value of the key until something happens... |
|
|
Makes sense. Thanks a bunch , works like a charm now! |
|
|
|
|
|
Shadow456732
|
Posted: Tue Dec 18, 2012 9:50 pm Post subject: RE:Input.KeyDown Problems... Seems to keep the value of the key until something happens... |
|
|
Lol, sorry, final thing. I found out that if you do
"and not= prekey(KEY_ENTER) then"
instead of
"and ~prekey(KEY_ENTER) then"
dosen't seem to work.... Also, putting the not=
in between prekey and (KEY_ENTER), it dosen't work either. How would i do it other than with a ~ if possible haha |
|
|
|
|
|
TerranceN
|
Posted: Tue Dec 18, 2012 10:00 pm Post subject: RE:Input.KeyDown Problems... Seems to keep the value of the key until something happens... |
|
|
'~' is a shorthand for the negation operator 'not' (i.e. 'not false' is 'true'), not 'not='.
If you do "and not prekey(KEY_ENTER) then" it should work. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|