Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

LUA PROGRAM HELP I have the below program. I need help adding another baloon tha

ID: 3575961 • Letter: L

Question

LUA PROGRAM HELP

I have the below program. I need help adding another baloon that follows the below rules:

-Add a yellow balloon (the image file name will be “yellow.png”).
-Around half of the time, when yoo click on the yellow balloon, it will add 10 points to your score, the other half it will take away 10 points. If you miss it, one point will be taken away. Have the “boom.png” appear when the points are added, and “ouch.png” when they are taken away.

*********************************

CURRENT CODE

*********************************

-- Start the physics engine
local physics = require( "physics" )
physics.start()

-- Calculate half the screen width and height
halfW = display.contentWidth*0.5
halfH = display.contentHeight*0.5

-- Set the background
local bkg = display.newImage( "sky.png", halfW, halfH )

-- Score
score =0
scoreText = display.newText(score, halfW, 10)

-- Called when the balloon is tapped by the player
-- Increase score by 10
local function balloonTouched(event)
   if ( event.phase == "began" ) then
       Runtime:removeEventListener( "enterFrame", event.self )
event.target:removeSelf()
  
--adds to score if game still in play
if (score >=0) then
if (score < 500) then        
score = score + 10
end
end
scoreText.text = score
end

if (score < 0)
then scoreText.text = "You Lose!"
end
if (score > 499)
then scoreText.text= "You Win!"
end
end

-- Called when the bomb is tapped by the player
-- Half the score as a penalty
local function bombTouched(event)
   if ( event.phase == "began" ) then
       Runtime:removeEventListener( "enterFrame", event.self )
local blast = display.newImage( "ouch.png",event.x,event.y)
physics.addBody( blast )

event.target:removeSelf()

--divides score if game still in play
if (score >=0) then
if (score < 500) then
       score = math.floor(score * 0.5)
  
       scoreText.text = score
end
end
end
end

local function superTouched(event)
   if ( event.phase == "began" ) then
       Runtime:removeEventListener( "enterFrame", event.self )
local blast = display.newImage( "boom.png",event.x,event.y)
physics.addBody( blast )
event.target:removeSelf()

--adds to score if game is still going.
if (score >=0) then
if (score < 500) then
  
       score = score + 100
end
end
         
       scoreText.text = score
end
if (score < 0)
then scoreText.text = "You Lose!"
end
if (score > 499)
then scoreText.text= "You Win!"
end
end

-- Delete objects which has fallen off the bottom of the screen
local function offscreen(self, event)
   if(self.y == nil) then
       return
   end
   if(self.y > display.contentHeight + 50) then
       Runtime:removeEventListener( "enterFrame", self )
       self:removeSelf()

--takes away from score if game is still going
if (score >=0) then
if (score < 500) then
score = score - 1

end
end
scoreText.text = score
if (score < 0)
then scoreText.text = "You Lose!"
end
if (score > 499)
then scoreText.text= "You Win!"
   end
end
end

-- Delete objects which has fallen off the bottom of the screen
local function offscreenbomb(self, event)
   if(self.y == nil) then
       return
   end
   if(self.y > display.contentHeight + 50) then
       Runtime:removeEventListener( "enterFrame", self )
       self:removeSelf()

   end
end

-- Add a new falling balloon or bomb
local function addNewBalloonOrBomb()
   -- You can find red_ballon.png and bomb.png in the GitHub repo
if (score >= 0) then  
if (score < 500) then

local startX = math.random(display.contentWidth*0.1,display.contentWidth*0.9)
   if(math.random(1,5)==1) then
       -- BOMB!
       local bomb = display.newImage( "bomb2.png", startX, -300)
       physics.addBody( bomb )
       bomb.enterFrame = offscreenbomb
       Runtime:addEventListener( "enterFrame", bomb )
       bomb:addEventListener( "touch", bombTouched )
else

if(math.random(1,5)==2) then
       -- super!
       local super = display.newImage( "super2.png", startX, -300)
       physics.addBody( super )
       super.enterFrame = offscreen
       Runtime:addEventListener( "enterFrame", super )
       super:addEventListener( "touch", superTouched )

   else
       -- Balloon
       local balloon = display.newImage( "green_balloon.png", startX, -300)
       physics.addBody( balloon )
       balloon.enterFrame = offscreen
       Runtime:addEventListener( "enterFrame", balloon )
       balloon:addEventListener( "touch", balloonTouched )
   end
end
end
end
end

-- Add a new balloon or bomb now
--addNewBalloonOrBomb()

-- Keep adding a new balloon or bomb every 0.5 seconds
if (score >= 0) then  
if (score < 500) then


timer.performWithDelay ( 500, addNewBalloonOrBomb, -1)


end
end

Explanation / Answer

In the loop we are randomly calculating the value and displaying new ballon or bomb. In that loop I have added one more condition to print yellow baloon along with green baloon too...

Start physics engine
local physics = require ( "physics" )
physics.start()
-- Calculate the half screen width and height
halfW = display.content Width*0.5
halfH = display.content Height*0.5
-- Set background
local bkg = display.newImage( "sky.png", halfW, halfH )
-- Score
score =0
score Text = display.new Text(score, halfW, 10)
-- Called when balloon is tapped by the player
-- Increase the score by 10
local function balloonTouched(event)
if ( event.phase == "began" ) then
Runtime:remove Event Listener( "enterFrame", event.self )
event.target:removeSelf()
  
--adds to the score if game still in play
if (score >=0) then
if (score < 500) then
score = score + 10
end
end
score Text.text = score
end
if (score < 0)
then score Text.text = "You Lose!"
end
if (score > 499)
then score Text.text= "You Win!"
end
end
-- Called when bomb is tapped by the player
-- Half the score as a penalty
local function bombTouched (event)
if ( event.phase == "began" ) then
Runtime:remove Event Listener( "enterFrame", event.self )
local blast = display.new Image( "ouch.png",event.x,event.y)
physics.add Body( blast )
event.target:removeSelf()
--divides score if game still in play
if (score >=0) then
if (score < 500) then
score = math.floor(score * 0.5)
  
score Text.text = score
end
end
end
end
local function super Touched(event)
if ( event.phase == "began" ) then
Runtime:remove Event Listener( "enterFrame", event.self )
local blast = display.new Image( "boom.png",event.x,event.y)
physics.addBody( blast )
event.target:remove Self()

--adds to the score if game is still going.
if (score >=0) then
if (score < 500) then
  
score = score + 100
end
end

score Text.text = score
end
if (score < 0)
then score Text.text = "You Lose!"
end
if (score > 499)
then score Text.text= "You Win!"
end
end
-- Delete objects which has fallen off to the bottom of the screen
local function off screen(self, event)
if(self.y == nil) then
return
end
if(self.y > display.content Height + 50) then
Runtime:remove EventListener( "enterFrame", self )
self:remove Self()
--takes away from score if game is still going
if (score >=0) then
if (score < 500) then
score = score - 1
end
end
score Text.text = score
if (score < 0)
then score Text.text = "You Lose!"
end
if (score > 499)
then scoreText.text= "You Win!"
end
end
end
-- Delete the objects which has fallen off to the bottom of the screen
local function off screenbomb(self, event)
if(self.y == nil) then
return
end
if(self.y > display.contentHeight + 50) then
Runtime:remove EventListener( "enterFrame", self )
self:removeSelf()
end
end
-- Add a new falling balloon or bomb
local function addNewBalloonOrBomb()
-- You can find red_ballon.png and bomb.png in the GitHub repo
if (score >= 0) then
if (score < 500) then
local startX = math.random(display.contentWidth*0.1,display.contentWidth*0.9)
if(math.random(1,5)==1) then
-- BOMB!
local bomb = display.new Image( "bomb2.png", startX, -300)
physics.add Body( bomb )
bomb.enter Frame = offscreenbomb
Runtime:add EventListener( "enterFrame", bomb )
bomb:add EventListener( "touch", bombTouched )
else
if(math.random(1,5)==2) then
-- super!
local super = display.new Image( "super2.png", startX, -300)
physics.add Body( super )
super.enter Frame = offscreen
Runtime:add Event Listener( "enterFrame", super )
super:add Event Listener( "touch", superTouched )
else
if(math.random(1,5)==3) then
-- yellow Balloon
local balloon = display.newImage( "yellow.png", startX, -300)
physics.addBody( balloon )
balloon.enterFrame = offscreen
Runtime:add EventListener( "enterFrame", balloon )
balloon:add EventListener( "touch", balloonTouched )  
  
else
-- green Balloon
local balloon = display.new Image( "green_balloon.png", startX, -300)
physics.addBody( balloon )
balloon.enter Frame = offscreen
Runtime:add EventListener( "enterFrame", balloon )
balloon:add EventListener( "touch", balloonTouched )
end
end
end
end
end
-- Add new balloon or bomb now
--add New Balloon Or Bomb()

-- Keep adding a new balloon or bomb every 0.5 seconds
if (score >= 0) then
if (score < 500) then

timer.performWithDelay ( 500, addNewBalloonOrBomb, -1)

end
end