Simple score / countdown timer and health bar.

I am now at the stage where I need to add a scoring system and countdown timer to my game…

I can get a basic scoring system to work but I am having trouble coding a health bar and tying it togethor with a countdown timer.
Player collects objects; on collision score is increased.
Player hits enemy health bar decreases.
Game has a countdown timer.

I have seen examples and understand quite a bit but I am struggling; the “basic defense game” code here:
http://www.tandgapps.co.uk/2012/04/12/tutorial-defense-game-in-200-lines/
seems to be nearly what I am after but it is confusing me.

Also how do i end the game code wise? do I just nil out he timers ?

Can anyone point me at some other examples please or give a little advice.
[import]uid: 127675 topic_id: 30766 reply_id: 330766[/import]

well, you have a nice set of problems here :wink:
so let’s start with a little

  • countdown
local countTimer  
local countSeconds = 10  
local function countDown ()  
 countSeconds = countSeconds - 1  
 print ("Seconds: "..countSeconds)  
 if countSeconds == 0 then  
 timer.cancel(countTimer)  
 print ("Horaaay!")  
 end  
end  
countTimer = timer.performWithDelay(1000, countDown, 0)  

health bar: http://howto.oz-apps.com/2011/06/make-bobbing-bar.html

  • hit test:
------------------  
-- HIT TEST Circle --  
-----------------  
-- example: hitTestCircle (obj1, obj2, [distance in pixels, default=30])  
-- returns: true/false  
-----------------  
hitTestCircle = function (obj1, obj2, dist)   
 if obj1.x ~= nil and obj2.x ~= nil then -- do they even exist?  
 local dist = dist or 30  
 local sqrt = math.sqrt  
 local dx = obj1.x - obj2.x  
 local dy = obj1.y - obj2.y  
 local distance = math.sqrt(dx\*dx + dy\*dy) -- pythagoras FTW!  
 if distance \< dist then   
 return true  
 else  
 return false  
 end  
 else  
 return false  
 end   
end  
  
------------------  
-- HIT TEST Square --  
-----------------  
-- example: hitTestSquare (obj1, obj2)  
-- returns: true/false  
-----------------  
hitTestSquare = function( obj1, obj2 )   
 if obj1.x \> obj2.x + obj2.width/2 - 1 or  
 obj1.y \> obj2.y + obj2.height/2 - 1 or  
 obj2.x \> obj1.x + obj1.width/2 - 1 or  
 obj2.y \> obj1.y + obj1.height/2 - 1 then  
 return false  
 else  
 print ("HIT!")  
 return true  
 end  
end  
  
-- example:  
obj1 = display.newCircle( 100, 100, 100 )  
obj2 = display.newCircle( 150, 150, 100 )  
if hitTestCircle (obj1, obj2, 100) then  
 print ("BOOM!")  
end  

as for the end of the game think of something like this

local gameOver = false  
  
function gameLoop()   
 if gameOver == false then  
 -- move character and stuff like that  
 if heroHP == 0 then  
 gameOver = true  
 end  
 else   
 -- show game over screen  
 end  
end  

hope that helps :wink:

-finefin [import]uid: 70635 topic_id: 30766 reply_id: 123195[/import]

well, you have a nice set of problems here :wink:
so let’s start with a little

  • countdown
local countTimer  
local countSeconds = 10  
local function countDown ()  
 countSeconds = countSeconds - 1  
 print ("Seconds: "..countSeconds)  
 if countSeconds == 0 then  
 timer.cancel(countTimer)  
 print ("Horaaay!")  
 end  
end  
countTimer = timer.performWithDelay(1000, countDown, 0)  

health bar: http://howto.oz-apps.com/2011/06/make-bobbing-bar.html

  • hit test:
------------------  
-- HIT TEST Circle --  
-----------------  
-- example: hitTestCircle (obj1, obj2, [distance in pixels, default=30])  
-- returns: true/false  
-----------------  
hitTestCircle = function (obj1, obj2, dist)   
 if obj1.x ~= nil and obj2.x ~= nil then -- do they even exist?  
 local dist = dist or 30  
 local sqrt = math.sqrt  
 local dx = obj1.x - obj2.x  
 local dy = obj1.y - obj2.y  
 local distance = math.sqrt(dx\*dx + dy\*dy) -- pythagoras FTW!  
 if distance \< dist then   
 return true  
 else  
 return false  
 end  
 else  
 return false  
 end   
end  
  
------------------  
-- HIT TEST Square --  
-----------------  
-- example: hitTestSquare (obj1, obj2)  
-- returns: true/false  
-----------------  
hitTestSquare = function( obj1, obj2 )   
 if obj1.x \> obj2.x + obj2.width/2 - 1 or  
 obj1.y \> obj2.y + obj2.height/2 - 1 or  
 obj2.x \> obj1.x + obj1.width/2 - 1 or  
 obj2.y \> obj1.y + obj1.height/2 - 1 then  
 return false  
 else  
 print ("HIT!")  
 return true  
 end  
end  
  
-- example:  
obj1 = display.newCircle( 100, 100, 100 )  
obj2 = display.newCircle( 150, 150, 100 )  
if hitTestCircle (obj1, obj2, 100) then  
 print ("BOOM!")  
end  

as for the end of the game think of something like this

local gameOver = false  
  
function gameLoop()   
 if gameOver == false then  
 -- move character and stuff like that  
 if heroHP == 0 then  
 gameOver = true  
 end  
 else   
 -- show game over screen  
 end  
end  

hope that helps :wink:

-finefin [import]uid: 70635 topic_id: 30766 reply_id: 123195[/import]