Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Time Limit and Key Press Help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Zeppelin




PostPosted: Fri Nov 30, 2007 10:44 pm   Post subject: Time Limit and Key Press Help

I'm currently making an RPG for my Grade 10 ISP. One of the things I'm making is a turn based battle system similar to the one in Paper Mario. The way it works is that you have to preform a certain action and if you do it at the right time or quick enough you do more damage and vice versa.

I have two problems with it though.

1) I'm making a system where you need to repeadedly press a button to power up an attack. I made a counter that would go up by one every time the button was pressed but it could easily be exploited by just holding the button down. I've tried using both the Imput.KeyDown and getch commands but they don't work the way I wanted. What command do I have to use to make it work properly?

2) The second is that I can't find a command that allows me to keep track of how much time has passed by. What I want is from the time the action start the timer would start and when it reached a certain point (Such as 5 seconds) the program wouldn't let the user continue the action. What command would be best for this?

Thanks
Sponsor
Sponsor
Sponsor
sponsor
Nick




PostPosted: Fri Nov 30, 2007 10:50 pm   Post subject: RE:Time Limit and Key Press Help

hmm for #1 try having a boolean expression for the button so if the user hits the button and the boolean = false then the boolean turns to true and the counter gets added to then when the button is no longer being pressed the boolean turns back to false

for #2 you could look up Delay.SinceLast or Time.Elapsed
chrisciscoioio




PostPosted: Fri Nov 30, 2007 10:50 pm   Post subject: Re: Time Limit and Key Press Help

For the first question, post what code you have tryed.

As for the second in pseudo code.

code:
If Key Pressed Equal to 'A'  // Whatever key you are using

If Power Greater then or Equal to MaxPower

Power Becomes Power plus One

Else

// Do Nothing

End If


You Place that piece of code in your Game Loop, and then if it is still pressed repeat.

You can also add a timer in there, but that stated in the post above, no need for it twice.
Zeppelin




PostPosted: Fri Nov 30, 2007 11:24 pm   Post subject: Re: Time Limit and Key Press Help

Ok I got the timer working but I now have a second problem. If I would want to play the game a second time is it possible to reset the Time.Elapsed back to 0?
Nick




PostPosted: Fri Nov 30, 2007 11:44 pm   Post subject: RE:Time Limit and Key Press Help

the only way i would know would be to declare the timer varible within a procedure or loop (so not a global varible) that way every time the loop (or procedure) repeats it resets the varible and therefore the timer
Zeppelin




PostPosted: Sat Dec 01, 2007 12:02 am   Post subject: Re: Time Limit and Key Press Help

I tried what you suggested but it's not working

code:

var Counter : real
var A : string
var chars : array char of boolean

loop
Counter := 0

loop
var timeRunning : int
timeRunning := Time.Elapsed
            Input.KeyDown (chars)
            locate (1, 1)
           
            if chars ('a') then
                Counter := Counter + 1
                put Counter
                delay (25)
                if Counter > 100 then
                Counter := 0
                end if
                cls
            else
                Counter := Counter - 0.005
                if Counter < 0 then
                Counter := 0
                end if
            end if
            put Counter
            if timeRunning >= 5000 then
            exit
            end if
            end loop
put "Finish"
put "Play Again (y/n)"
get A
exit when A = "n"
end loop


What did I do wrong?
Nick




PostPosted: Sat Dec 01, 2007 12:06 am   Post subject: RE:Time Limit and Key Press Help

that SHOULD work ill try it out then edit this post with my result

EDIT: i tried the current code i tried a record and i tried a flexible array and none of them worked sorry
Zeppelin




PostPosted: Sat Dec 01, 2007 12:47 am   Post subject: Re: Time Limit and Key Press Help

Thanks for the help. I appreciate it. Also I managed to get it to work by using a counter instead of Time.Elapsed so it looks like this.

code:

var Counter : real
var A : string
var chars : array char of boolean
var Timing : int

loop
Counter := 0
Timing := 0

loop

            Input.KeyDown (chars)
            locate (1, 1)
           
            if chars ('a') then
                Counter := Counter + 1
                put Counter
                delay (25)
                if Counter > 100 then
                Counter := 0
                end if
                cls
            else
                Counter := Counter - 0.005
                if Counter < 0 then
                Counter := 0
                end if
            end if
            Timing := Timing + 1
            put Counter
            if Timing >= 10000 then
            exit
            end if
            end loop
put "Finish"
put "Play Again (y/n)"
get A
exit when A = "n"
end loop
Sponsor
Sponsor
Sponsor
sponsor
Nick




PostPosted: Sat Dec 01, 2007 12:50 am   Post subject: RE:Time Limit and Key Press Help

ahh a counter y didnt i think of that good work on getting that working !
Clayton




PostPosted: Sat Dec 01, 2007 8:26 am   Post subject: RE:Time Limit and Key Press Help

If you're still interested in using Time.Elapsed, just have a variable that has the time stored from a fixed point (using Time.Elapsed of course). Let's call it time_start. From there, whenever you need to check how much time has passed, give the value of Time.Elapsed to a second variable, which we'll call time_passed. Any time you need to check how much time has passed since that starting point, you just need to take the difference between time_passed and time_start.
Zeppelin




PostPosted: Sat Dec 01, 2007 12:21 pm   Post subject: Re: Time Limit and Key Press Help

I think I'll try using Time.Elapsed now but I was wondering, could you post a quick example so I know how it works?
Clayton




PostPosted: Sat Dec 01, 2007 3:17 pm   Post subject: RE:Time Limit and Key Press Help

Turing:

var time_start := Time.Elapsed
var time_elapse := Time.Elapsed

put time_elapse - time_start " milliseconds since the start of the program have elapsed."

for i : 1 .. 10
    time_elapse := Time.Elapsed
    put time_elapse - time_start " milliseconds since start of program have elapsed."
    Time.Delay (100)
end for
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: