Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- display.setStatusBar(display.HiddenStatusBar);
- -- Prototypes
- local MathSetup, ScreenSetup, NumPush, CalcIt, Reset
- -- Variables
- local _H = display.contentHeight;
- local _W = display.contentWidth;
- local CurrentInput
- local result
- local group
- --Setup Math - Randomize Variables
- function MathSetup()
- -- Create fresh group for the problem to be displayed in
- group = display.newGroup()
- group.x, group.y = 0, 0
- -- Resets the input
- CurrentInput = 0
- --Randomizing Problem
- local a = math.random(1,9)
- local b = math.random(1,9)
- -- Finding Out What Problem Is And Associating Its Integers And Symbol With Images
- local NumImg1 = "assets/images/numbers/" .. tostring( a ) .. ".png"
- local NumImg2 = "assets/images/numbers/" .. tostring( b ) .. ".png"
- local Operator
- local operations = { "+", "-", "/", "*" }
- local ChosenOperator = operations[ math.random( 4 ) ]
- if ChosenOperator == "+" then
- Operator = "assets/images/add.png"
- result = a + b
- elseif ChosenOperator == "-" then
- Operator = "assets/images/subtract.png"
- result = a - b
- elseif ChosenOperator == "*" then
- Operator = "assets/images/multiply.png"
- result = a * b
- elseif ChosenOperator == "/" then
- Operator = "assets/images/divide.png"
- result = a / b
- end
- --Displaying The Problem's Images
- local myImage = display.newImageRect((NumImg1), 100 ,100)
- group:insert( myImage )
- myImage.x = _W*0.4
- myImage.y = _H*0.90
- myImage:scale (.4, .4)
- local myImageOperator = display.newImageRect(Operator, 50,50)
- group:insert( myImageOperator )
- myImageOperator.x = _W*0.5
- myImageOperator.y = _H*0.90
- myImageOperator:scale (.5, .5)
- local myImage2 = display.newImageRect((NumImg2), 100 ,100)
- group:insert( myImage2 )
- myImage2.x = _W*0.6
- myImage2.y = _H*0.90
- myImage2:scale (.4, .4)
- end
- -- Set up screen
- function ScreenSetup(Event)
- -- Set up background
- local bg = display.newImage( "assets/images/bg.png", 1600, 1200);
- bg.x = _W / 6.5
- bg.y = _H / 6.5
- -- Setup numbers ( 1-9 and 0 )
- local digits = {
- { 1, 0.2, 0.2 },
- { 2, 0.5, 0.2 },
- { 3, 0.8, 0.2 },
- { 4, 0.2, 0.35 },
- { 5, 0.5, 0.35 },
- { 6, 0.8, 0.35 },
- { 7, 0.2, 0.5 },
- { 8, 0.5, 0.5 },
- { 9, 0.8, 0.5 },
- { 0, 0.5, 0.65 },
- }
- for i = 1, #digits do
- local digit = digits[ i ]
- local num = display.newImageRect( "assets/images/numbers/" .. tostring( digit[ 1 ] ) .. ".png", 100 ,100)
- num.x = _W * digit[ 2 ]
- num.y = _H * digit[ 3 ]
- num:addEventListener( "tap", function() NumPush( digit[ 1 ] ) ; end )
- end
- end
- --Event Listeners For Numbers
- function NumPush( value )
- CurrentInput = CurrentInput * 10 + value
- CalcIt()
- end
- -- Calculates results
- function CalcIt(result)
- if tostring( CurrentInput ):len() == tostring( result ):len() then
- if CurrentInput == result then
- print("You Won")
- Reset()
- else
- print("You Lost")
- Reset()
- end
- end
- end
- function Reset()
- group:removeSelf()
- MathSetup()
- end
- ScreenSetup()
- MathSetup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement