Getting a timer working
Author |
Message |
Mustiko
|
Posted: Sat Apr 19, 2008 2:16 pm Post subject: Getting a timer working |
|
|
Hey everyone, I am having problems with getting this timer to work. I have a whack a mole program and want to make it so that the mole will disappear after a certain amount of time e.g. 1 second and I have tried many ways with time.elapsed but it either only works for the first mole or it flickers and finishes when u click or something its pretty messed up but I have a feeling that the problem si very obviously and I can't figure it out so if any of you could give me a hand then I would appreciate it thanks
Oh by the way heres the code:
Turing: |
%VARIABLES
var done, hit, nohit : boolean := false % controls hit loop and game loop
var mole_x, mole_y, h, v, z, button, count : int := 0 % location of mouse and boxes
var m_width : int := 174 % width of mole
var m_height : int := 161 % height of mole
var st, ft : real % timer variables
var mole : int
var m_location : int
var colorfont : int := Font.New ("Arial:12")
mole := Pic.FileNew ("mole.jpg")
function Pt_In_Rect (x, y, x1, y1, x2, y2 : int) : boolean
result (x > x1 ) and (x < x2 ) and (y > y1 ) and (y < y2 )
end Pt_In_Rect
%-----------------------------------------------------------------------------------------
%SCREEN/BACKGROUND
setscreen ("graphics: 560, 520")
drawfillbox (0, 0, maxx, maxy, black)%background
drawfillbox (10, 10, 183, 169, white)%bottom left
drawfillbox (193, 179, 366, 339, white)%middle
drawfillbox (376, 349, 549, 509, white)%top right
drawfillbox (193, 10, 366, 169, white)%bottom middle
drawfillbox (376, 10, 549, 169, white)%bottom right
drawfillbox (10, 179, 183, 339, white)%middle left
drawfillbox (376, 179, 549, 339, white)%middle right
drawfillbox (10, 349, 183, 509, white)%top left
drawfillbox (193, 349, 366, 509, white)%top middle
%--------------------------------------------------------------------------------------------
%MAIN LOOP
%WAITING FOR USER INPUT
buttonwait ("down", h, v, z, z )
%TIMING FROM HERE
st := Time.Elapsed
loop
%RESET HIT EACH TURN
hit := false
randint (m_location, 1, 9)
%DRAW MOLE IN RANDOM BOX
if m_location = 1 then
Pic.Draw (mole, 10, 10, picCopy)%bottom left
elsif m_location = 2 then
Pic.Draw (mole, 193, 179, picCopy)%middle
elsif m_location = 3 then
Pic.Draw (mole, 376, 349, picCopy)%top right
elsif m_location = 4 then
Pic.Draw (mole, 193, 10, picCopy)%bottom middle
elsif m_location = 5 then
Pic.Draw (mole, 376, 10, picCopy)%bottom right
elsif m_location = 6 then
Pic.Draw (mole, 10, 179, picCopy)%middle left
elsif m_location = 7 then
Pic.Draw (mole, 376, 179, picCopy)%middle right
elsif m_location = 8 then
Pic.Draw (mole, 10, 349, picCopy)%top left
elsif m_location = 9 then
Pic.Draw (mole, 193, 349, picCopy)%top middle
end if
%DEFINING HIT VARIABLES
if m_location = 1 then
mole_x := 10
mole_y := 10
elsif m_location = 2 then
mole_x := 193
mole_y := 179
elsif m_location = 3 then
mole_x := 376
mole_y := 349
elsif m_location = 4 then
mole_x := 193
mole_y := 10
elsif m_location = 5 then
mole_x := 376
mole_y := 10
elsif m_location = 6 then
mole_x := 10
mole_y := 179
elsif m_location = 7 then
mole_x := 376
mole_y := 179
elsif m_location = 8 then
mole_x := 10
mole_y := 349
elsif m_location = 9 then
mole_x := 193
mole_y := 349
end if
%CHECKING IF MOLE IS HIT
loop
mousewhere (h, v, button )
if Pt_In_Rect (h, v, mole_x, mole_y, mole_x + m_width, mole_y + m_height ) and (button = 1) then
hit := true
elsif (Time.Elapsed - st ) div 1000 = 1 then
nohit := true
end if
exit when hit or nohit
end loop
%ERASES MOLE
drawfillbox (mole_x, mole_y, mole_x + m_width, mole_y + m_height, white)
%COUNTS MOLES HIT
count := count + 1
if count = 10 then
done := true
end if
exit when done
end loop
%CALCULATE THE TIME
ft := (Time.Elapsed - st ) / 1000
%OUTPUTS THE TIME
put ft : 0 : 2
%REMARKS
if ft < 5 then
put "Speed Master!"
else
put "Not fast enough!"
end if
|
Thanks again |
|
|
|
|
|
Sponsor Sponsor
|
|
|
syntax_error
|
Posted: Sat Apr 19, 2008 3:58 pm Post subject: RE:Getting a timer working |
|
|
If the time.elapsed is giving you trouble simply have a for looping counting seconds?
then you can control the time when the for loop exits it will reach the not fast enough ?
I think that would be the simplest way to get around using time.elapsed no?
Anyone got better idea[s]? |
|
|
|
|
|
Nick
|
Posted: Sat Apr 19, 2008 4:09 pm Post subject: RE:Getting a timer working |
|
|
how about using Time.DelaySinceLast, forcing each loop run to be x milliseconds (example 10) then you want the mole to be there for 1 second (1 second = 1000 milliseconds) so 1000/10 loop runs until you make the mole dissapear |
|
|
|
|
|
Mustiko
|
Posted: Sun Apr 20, 2008 2:28 pm Post subject: Re: Getting a timer working |
|
|
ill try the for loop thing and for the Time.DelaySinceLast one where would I place that in the code? in the main loop or the loop inside the loop? |
|
|
|
|
|
LaZ3R
|
Posted: Sun Apr 20, 2008 9:57 pm Post subject: RE:Getting a timer working |
|
|
Really? None of you see his problem?
The reason it only works for the first mole is because look at where you are giving st a value...
OUTSIDE THE LOOP!!!!!!
Every time a new mole appears, THAT'S when you set st:= Time.Elapsed |
|
|
|
|
|
Nick
|
Posted: Sun Apr 20, 2008 10:27 pm Post subject: RE:Getting a timer working |
|
|
I never look at code |
|
|
|
|
|
syntax_error
|
Posted: Sun Apr 20, 2008 10:47 pm Post subject: RE:Getting a timer working |
|
|
nor did I, takes too long. |
|
|
|
|
|
LaZ3R
|
Posted: Mon Apr 21, 2008 10:02 pm Post subject: RE:Getting a timer working |
|
|
Such a simple problem like his though usually results from a single error in the code. It took 2 seconds for me to spot so rather than making things more complicted for him, wouldn't it be easier to just glance at the code for a second? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Mustiko
|
Posted: Wed Apr 23, 2008 9:37 am Post subject: Re: Getting a timer working |
|
|
Thank you laZ3R and I also found out that I stupidly forgot to declare that hit and nohit are false before the loop inside the loop. Thanks again and good job guyz! |
|
|
|
|
|
|
|