LUA PROGRAM HELP I have the below program. I need help adding another baloon tha
ID: 3575959 • 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 loop we are randomly calculating value and displaying new ballon or bomb. In that loop I added one more condition to print yellow baloon along with green baloon too... Modified code written in bold.. please verufy.
-- 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
if(math.random(1,5)==3) then
-- yellow Balloon
local balloon = display.newImage( "yellow.png", startX, -300)
physics.addBody( balloon )
balloon.enterFrame = offscreen
Runtime:addEventListener( "enterFrame", balloon )
balloon:addEventListener( "touch", balloonTouched )
else
-- green 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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.