Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

enemy reaction time

Status
Not open for further replies.

shanley06

Programmer
Nov 26, 2002
101
US
i have a game i'm working on and i have the ai in it but the ai reacts to quickly to dodge it. everytime you are on the same floor level as the enemy the enemy automatically shoots out the laser giving you no chance to dodge it. i was wondering if someone could tell me how to add in a reaction time so that the enemy doesn't automatically shoot. thanks in advance
 
use TIMER to delay the reaction


t = TIMER
DO
IF TIMER > t + 1 THEN shootlaser
LOOP

it will give you one second to move
 
Actually... it needs to be more like...

delay = x 'where x = seconds... 1 = 1 second... .5 = 1/2 second...
t = timer 'initalize timer
Do
If timer => t + delay Then
ShootLaser 'execute the laser routine
t = timer 'reset timer
End If
Loop Until inkey$ <> &quot;&quot; '*(I hate endless loops)

That should get you a little better results...
or even put a delay for the entire program loop...

delay = x 'Set Delay to X seconds
t = timer 'initalize timer
Do
Loop Until timer => t + delay 'wait until the delay has completed

...this will give you better results in the long run by regulating the flow of the program
By doing this the game should run the same on any computer that meets or exceeds the requirements to run the game
Be sure to set the delay LOW... like .01 to .1:
.017 is about 60 frames per second
.033 is about 30 frames per second
or just set the delay by saying: Delay = frames / 1
Delay = 30 / 1 'for 30fps
Delay = 60 / 1 'for 60fps

One last thing to think about...
TIMER returns the number of seconds past midnight (data-type: Double)
So, there is a rare case when the clock ticks between 11:59pm and 12:00am and the timer is reset to 0 (from 86399)
Unless you realy want your program to delay for at least 24 hours... You can trap this by doing the following:
replace Loop Until timer => t + delay with Loop Until ABS(timer - t) => delay
this way if the clock does happen to click over in the middle of your loop it will read...
abs(0 - 86399) => delay (86399 => delay)
instead of 0 => 86399 + delay
...which could easily end up being an endless loop if t + delay > 86400 making it impossible for the timer to exceed, and causing your program to lock up

Probably the best way to use a delay is like this...

delay = frames / 1 'Set Delay (fps)
do 'start main loop
t = timer 'initalize timer before main loop code

'Your main loop code goes here
'This way the processing time is included in the delay and not added to it

Do
Loop Until abs(timer - t) => delay 'if the delay has not been exceeded, finish delay
Loop Until inkey$ <> &quot;&quot; 'I still hate endless loops

This should give you a few things to think about
-Josh
Sometimes... the BASIC things in life are the best...
cheers.gif

or at least the most fun ;-)
-Josh Stribling
 
cubeE101, I have been thinking of writing a faq for using delays that will work on all computers. I have two or three different methods, but your idea is another way. Would you mind if I used some of your code in my faq. Sorry shanley06 for using your thread for this question, but he used his example here. Hope you don't mind.
 
sure... But I just noticed a typo...

Delay = Frame / 1
(Delay = 30 / 1 would still be 30... kinda pointless :-|)

needs to be

Delay = 1 / Frame
(Delay = 1 / 30 would be .033 ... the real target value)

sorry... (I wrote this between 4 and 5am... I was kinda tired)

thanks,
Josh Sometimes... the BASIC things in life are the best...
cheers.gif

or at least the most fun ;-)
-Josh Stribling
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top