Computer Science Canada

HELP!!! Making an if key pressed statement execute repeatedly after pressing the key only once!?

Author:  nick4563 [ Wed Jan 12, 2011 2:25 pm ]
Post subject:  HELP!!! Making an if key pressed statement execute repeatedly after pressing the key only once!?

What is it you are trying to achieve?
I am trying to make a conditional loop repeat several times after a key has been pressed

What is the problem you are having?
Doesnt Work!!!! (What a stupid question)


Describe what you have tried to solve this problem
Everything I Know... i need to make my bullet shoot after pressing the '1' key once i can do everything but the weird loop. Sad BooHoo Canada


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)


Turing:






Please specify what version of Turing you are using
4.1

Author:  Tony [ Wed Jan 12, 2011 2:49 pm ]
Post subject:  RE:HELP!!! Making an if key pressed statement execute repeatedly after pressing the key only once!?

You could try remembering (in some variable) if a key has been pressed in the past.

Author:  TheCatsMeow [ Wed Jan 12, 2011 5:50 pm ]
Post subject:  RE:HELP!!! Making an if key pressed statement execute repeatedly after pressing the key only once!?

You could try putting a boolean in there. and make it "True" when the key is pressed, but false when no keys are pressed.

Author:  chaos [ Wed Jan 12, 2011 6:32 pm ]
Post subject:  Re: HELP!!! Making an if key pressed statement execute repeatedly after pressing the key only once!?

Try this:

if key(1) then
capture bullet position
boolean variable=true
end if
if boolean variable=true then
move bullet
draw bullet
end if

Author:  nick4563 [ Wed Jan 12, 2011 7:24 pm ]
Post subject:  Re: HELP!!! Making an if key pressed statement execute repeatedly after pressing the key only once!?

Thanks! Very Happy Canada I never thought of using a boolean to control the loop!

Author:  nick4563 [ Thu Jan 13, 2011 1:37 pm ]
Post subject:  Re: HELP!!! Making an if key pressed statement execute repeatedly after pressing the key only once!?

I have this code:

var chars : array char of boolean
var a,b : int a:= 20 b:= 20
var shoot : boolean
drawfilloval(a,b,20,2,green)

loop
drawfilloval(a,b,20,2,green)
Input.KeyDown (chars)
if chars ('1')then
shoot:=true
else
shoot:=false
end if
if
true then
drawfilloval(a,b,20,2,green)
a+=10
delay(10)
end if
cls
end loop

Why does it shoot automatically not when the 1 key is pressed Question
I am not looking ofr code handouts only help Exclamation ...

Author:  Tony [ Thu Jan 13, 2011 2:10 pm ]
Post subject:  RE:HELP!!! Making an if key pressed statement execute repeatedly after pressing the key only once!?

What condition does your code say needs to happen, for the shot to fire?

Author:  nick4563 [ Thu Jan 13, 2011 2:18 pm ]
Post subject:  Re: HELP!!! Making an if key pressed statement execute repeatedly after pressing the key only once!?

I think it says that shhot needs to be true to fire the bulllet... At least that was what i was going for.

Author:  nick4563 [ Thu Jan 13, 2011 2:19 pm ]
Post subject:  Re: HELP!!! Making an if key pressed statement execute repeatedly after pressing the key only once!?

I think it says that shhot needs to be true to fire the bulllet... At least that was what i was going for.

Author:  nick4563 [ Thu Jan 13, 2011 2:26 pm ]
Post subject:  RE:HELP!!! Making an if key pressed statement execute repeatedly after pressing the key only once!?

if
**true then
drawfilloval(a,b,20,2,green)
a+=10
delay(10)
end if


**this is the section of code that is supposed to make the bullet appear and then shoot when shoot=true**

Author:  Tony [ Thu Jan 13, 2011 2:29 pm ]
Post subject:  Re: RE:HELP!!! Making an if key pressed statement execute repeatedly after pressing the key only once!?

nick4563 @ Thu Jan 13, 2011 2:26 pm wrote:
if
**true then
drawfilloval(a,b,20,2,green)
a+=10
delay(10)
end if

Where does this mention the "shoot" variable? Smile

Author:  DemonWasp [ Thu Jan 13, 2011 2:50 pm ]
Post subject:  RE:HELP!!! Making an if key pressed statement execute repeatedly after pressing the key only once!?

You never use the value of the shoot variable to do anything. You need to, depending on that value, either shoot or not shoot.

Author:  nick4563 [ Fri Jan 14, 2011 1:34 pm ]
Post subject:  Re: HELP!!! Making an if key pressed statement execute repeatedly after pressing the key only once!?

Here is my new code for the bullet...
am using the values x2 and y1 for a and b so that the bullet shoots from the players box (I know using a box for a character is LAME but im a noob)


The bullet goes ten pixels in front of the character and then follows him, How do i get rid of that.

Turing:

    var a : int := x2
    var b : int := y1
   
    if chars ('1') then %bullet shoots only when shoot = true
        shoot := true
    end if
    if shoot = true then
        a += 10
        drawfilloval (a, b, 10, 10, red)
        delay (5)
    end if
    if a > 389 then % sets bullet back after reaching the end of the screen
        shoot := false
    end if


%I also have this seperate bullet code that works
Turing:

var shoot : boolean
shoot := false
var chars : array char of boolean

var a : int := 20
var b : int := 20

loop
Input.KeyDown (chars)
    if chars ('1') then
        shoot := true
    end if
    if shoot = true then
        a +=10
        drawfilloval (a, b, 20, 2, green)
        delay (10)
        cls
    end if

end loop

Author:  Tony [ Fri Jan 14, 2011 1:54 pm ]
Post subject:  Re: HELP!!! Making an if key pressed statement execute repeatedly after pressing the key only once!?

nick4563 @ Fri Jan 14, 2011 1:34 pm wrote:

The bullet goes ten pixels in front of the character and then follows him, How do i get rid of that.

I would imagine that you are using your character's position to figure out the bullets position; which doesn't make sense after the bullet has been fired.

Author:  nick4563 [ Fri Jan 14, 2011 6:37 pm ]
Post subject:  RE:HELP!!! Making an if key pressed statement execute repeatedly after pressing the key only once!?

How would I be able to have the bullet originate at the characters position but progress away once shot??

Author:  Tony [ Fri Jan 14, 2011 6:53 pm ]
Post subject:  RE:HELP!!! Making an if key pressed statement execute repeatedly after pressing the key only once!?

What information do you need to describe the location and direction of a bullet? How does it change over time?

Author:  chaos [ Fri Jan 14, 2011 11:12 pm ]
Post subject:  Re: HELP!!! Making an if key pressed statement execute repeatedly after pressing the key only once!?

to answer your question, try this:

%THIS IS PURPOSELY IN TEXT GRAPHICS, YOU HAVE FIGURE OUT THE REST YOURSELF IF YYOU WANT IT IN PIXEL GRAPHICS

View.Set("offscreenonly")
var bulletvelocity:int:=%set speed of bullet%
var playerx,playery:int:=20
var bulletx, bullety:int
var chars : array char of boolean
var bullet:boolean:=false
loop
Input.KeyDown

%
put movement code here
%

if chars(' ') then
bulletx:=playerx %When the fire key is pressed the bullet coordinates will be the player's current position
bullety:=playery
bullet:=true %this allows one bullet per player on screen; prevents spamming
end if
if bullet=true then
bulletx:=bulletx+bulletvelocity %This example will shoot the bullet right
if bulletx>maxx then
locate(bulletx,maxcol)
put" " %Clears the bullet if bullet is about to go off screen
else
locate(bullety,bulletx)
put"-" %Draws the bullet if it is not off screen
end if
delay(50)
View.Update
end loop

Author:  TokenHerbz [ Sat Jan 15, 2011 12:49 am ]
Post subject:  RE:HELP!!! Making an if key pressed statement execute repeatedly after pressing the key only once!?

spawn at character --> move to destination... die when destination is reached....

Author:  nick4563 [ Sat Jan 15, 2011 9:57 am ]
Post subject:  RE:HELP!!! Making an if key pressed statement execute repeatedly after pressing the key only once!?

Thank you all Very Happy bullet velocity was my missing factor thank you!!!

Author:  Dragon20942 [ Tue Jan 18, 2011 7:59 pm ]
Post subject:  RE:HELP!!! Making an if key pressed statement execute repeatedly after pressing the key only once!?

I'm currently making a shooting game for my culminating. Create another variable for the initial Y position of the bullet. Moving the bullet is just basic loop stuff.


: