LUA PROGRAM HELP I have the below program. I need help adding another baloon tha
ID: 3576027 • 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
if (score >=0) then
if (score < 500) then
score = score + ten
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
-- known as once the bomb is broached by the player
-- [*fr1] the score as a penalty
local operate bombTouched(event)
if ( event.phase == "began" ) then
Runtime:removeEventListener( "enterFrame", event.self )
local blast = show.newImage( "ouch.png",event.x,event.y)
physics.addBody( blast )
event.target:removeSelf()
--divides score if game still live
if (score >=0) then
if (score < 500) then
score = scientific discipline.floor(score * zero.5)
scoreText.text = score
end
end
end
end
local operate superTouched(event)
if ( event.phase == "began" ) then
Runtime:removeEventListener( "enterFrame", event.self )
local blast = show.newImage( "boom.png",event.x,event.y)
physics.addBody( blast )
event.target:removeSelf()
--adds to attain if game remains going.
if (score >=0) then
if (score < 500) then
score = score + one hundred
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 that has fallen off rock bottom of the screen
local operate offscreen(self, event)
if(self.y == nil) then
return
end
if(self.y > show.contentHeight + 50) then
Runtime:removeEventListener( "enterFrame", self )
self:removeSelf()
--takes off from score if game remains going
if (score >=0) then
if (score < 500) then
score = score - one
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 that has fallen off rock bottom of the screen
local operate offscreenbomb(self, event)
if(self.y == nil) then
return
end
if(self.y > show.contentHeight + 50) then
Runtime:removeEventListener( "enterFrame", self )
self:removeSelf()
end
end
-- Add a replacement falling balloon or bomb
local operate addNewBalloonOrBomb()
-- you'll be able to realize red_ballon.png and bomb.png within the GitHub repo
if (score >= 0) then
if (score < 500) then
local startX = scientific discipline.random(display.contentWidth*0.1,display.contentWidth*0.9)
if(math.random(1,5)==1) then
-- BOMB!
native bomb = show.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!
native super = show.newImage( "super2.png", startX, -300)
physics.addBody( super )
super.enterFrame = offscreen
Runtime:addEventListener( "enterFrame", super )
super:addEventListener( "touch", superTouched )
else
-- Balloon
native balloon = show.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 replacement balloon or bomb currently
--addNewBalloonOrBomb()
-- Keep adding a replacement balloon or bomb each zero.5 seconds
if (score >= 0) then
if (score < 500) then
timer.performWithDelay ( five hundred, addNewBalloonOrBomb, -1)
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.