Advertisement
Rakoonic

Praxis 2 - the indenting sequel

Sep 12th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.96 KB | None | 0 0
  1. display.setStatusBar(display.HiddenStatusBar);
  2.  
  3. -- Prototypes
  4. local MathSetup, ScreenSetup, NumPush, CalcIt, Reset
  5.  
  6. -- Variables
  7. local _H = display.contentHeight;
  8. local _W = display.contentWidth;
  9. local CurrentInput
  10. local result, ChosenOperator
  11. local group
  12.  
  13. --Setup Math - Randomize Variables
  14. function MathSetup(a,b,c)
  15.  
  16.     -- Create fresh group for the problem to be displayed in
  17.     group            = display.newGroup()
  18.     group.x, group.y = 0, 0
  19.  
  20.     -- Resets the input
  21.     CurrentInput = 0
  22.  
  23.     --Randomizing Problem
  24.     local a = math.random(1,9)
  25.     local b = math.random(1,9)
  26.  
  27.     -- Finding Out What Problem Is And Associating Its Integers And Symbol With Images
  28.     local NumImg1 = "assets/images/numbers/" .. tostring( NumA ) .. ".png"
  29.     local NumImg2 = "assets/images/numbers/" .. tostring( NumB ) .. ".png"
  30.  
  31.     local Operator
  32.     local operations = { "+", "-", "/", "*" }
  33.     ChosenOperation  = operations[ math.random( 4 ) ]
  34.     if ChosenOperator == "+" then
  35.         Operator = "assets/images/add.png"
  36.         result   = a + b
  37.     elseif ChosenOperator == "-" then
  38.         Operator = "assets/images/subtract.png"
  39.         result   = a - b
  40.     elseif ChosenOperator == "*" then
  41.         Operator = "assets/images/multiply.png"
  42.         result   = a * b
  43.     elseif ChosenOperator == "/" then
  44.         Operator = "assets/images/divide.png"
  45.         result   = a / b
  46.     end
  47.  
  48.     --Displaying The Problem's Images
  49.     local myImage = display.newImageRect((NumImg1), 100 ,100)
  50.     myImage.x     = _W*0.4
  51.     myImage.y     = _H*0.90
  52.     myImage:scale (.4, .4)
  53.  
  54.     local myImageOperator = display.newImageRect(Operator, 50,50)
  55.     myImageOperator.x     = _W*0.5
  56.     myImageOperator.y     = _H*0.90
  57.     myImageOperator:scale (.5, .5)
  58.  
  59.     local myImage2 = display.newImageRect((NumImg2), 100 ,100)
  60.     myImage2.x     = _W*0.6
  61.     myImage2.y     = _H*0.90
  62.     myImage2:scale (.4, .4)
  63.  
  64. end
  65.  
  66. -- Set up screen
  67. function ScreenSetup(Event)
  68.  
  69.     -- Set up background
  70.     bg   = display.newImage( "assets/images/bg.png", 1600, 1200);
  71.     bg.x = _W / 6.5
  72.     bg.y = _H / 6.5
  73.  
  74.     -- Setup numbers ( 1-9 and 0 )
  75.     local digits = {
  76.         { 1, 0.2, 0.2 },
  77.         { 2, 0.5, 0.2 },
  78.         { 3, 0.8, 0.2 },
  79.         { 4, 0.2, 0.35 },
  80.         { 5, 0.5, 0.35 },
  81.         { 6, 0.8, 0.35 },
  82.         { 7, 0.2, 0.5 },
  83.         { 8, 0.5, 0.5 },
  84.         { 9, 0.8, 0.5 },
  85.         { 0, 0.5, 0.65 },
  86.     }
  87.     for i = 1, #digits do
  88.         local digit = digits[ i ]
  89.         local num   = display.newImageRect( group, "assets/images/numbers/" .. tostring( digit[ 1 ] ) .. ".png", 100 ,100)
  90.         num.x       = _W * digit[ 2 ]
  91.         num.y       = _H * digit[ 3 ]
  92.         num:addEventListener( "tap", function() NumPush( digit[ 1 ] ) ; end )
  93.     end
  94.  
  95. end
  96.  
  97. --Event Listeners For Numbers
  98. function NumPush( value )
  99.     CurrentInput = CurrentInput * 10 + 9
  100.     CalcIt()
  101. end
  102.  
  103. -- Calculates results
  104. function CalcIt(result)
  105.  
  106.     if tostring( CurrentInput ):len() == tostring( result ):len() then
  107.         if CurrentInput == result then
  108.             print("You Won")
  109.             Reset()
  110.         else
  111.             print("You Lost")
  112.             Reset()
  113.         end
  114.     end
  115.  
  116. end
  117.  
  118. function Reset()
  119.     group:removeSelf()
  120.     MathSetup()
  121. end
  122.  
  123. ScreenSetup()
  124. MathSetup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement