Customizing a countdown clock

I'm making a game that uses a countdown. I have the countdown working but I want to change the countdown based on the room the player is in. For example, in the first room, the countdown is 60 seconds, in the next room the timer is 30 seconds.

I'm not sure how to set this variable in my current setup, I've tried to create a custom event but then the timer decreases too fast. Any help is appreciated.

Here's the timer code so far

on load do
	restore
	
	// clock
	startTime = datetime.timestamp
	
	// score
	score = 0
	// player orientation
	jumped = 0
	ground = 0
end

// does not work because loop occurs at each frame. Happens too fast.
// on loop do
// clock--
// if clock == 0 then
// fin "game over"
// end
// end
// timer loop
on loop do
	// Get The Current Time
	currentTime = datetime.timestamp
	
	// Set the Clock Time
	clock = 60
	
	// Calculate how much time has passed since the game started
	elapsedTime = currentTime
	elapsedTime -= startTime
	clock -= elapsedTime
	
	// Finish Game when time ends
	if clock==-1 then
		fin "Game Over!  Score: {score}"
	end
	// If more than 60sec have passed, increase the minute counter
	// And reset the seconds counter to 0
end

You could use event.room when entering a room to change the timer depending on the room.

on enter do
	if event.room == "room 1" then
		clock = 60
	elseif event.room == "room 2" then
		clock = 30
	end
end

or you could just add it to the room's script instead

on enter do
	clock = 30
end
1 Like

As you noticed the game's loop event is called every frame. Pulp runs at 20 frames per second so your code was counting down too quickly.

One approach to fix that is to count in frames rather than seconds. If you want the timer to be 60 seconds, that's 1200 frames.

When setting the clock variable (I would do it in the room script like @MintFerret suggests!) you can either set it to the number of frames directly:

on enter do
  clock = 1200
end

or if you find it easier to think in terms of seconds, set it to the number of seconds then convert it to frames by multiplying by 20:

on enter do
  clock = 60
  clock *= 20
end

If you want to display the time in seconds to the player you can convert back to seconds by doing something like this:

on draw do
  seconds = clock
  seconds /= 20
  seconds = floor seconds
  label "{seconds}s" at 0,0
end