Advertisement
Rakoonic

Praxis #4 - The Revenge of Typo

Sep 12th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.04 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
  11. local group
  12.  
  13. --Setup Math - Randomize Variables
  14. function MathSetup()
  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( a ) .. ".png"
  29.     local NumImg2 = "assets/images/numbers/" .. tostring( b ) .. ".png"
  30.  
  31.     local Operator
  32.     local operations       = { "+", "-", "/", "*" }
  33.     local 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.     group:insert( myImage )
  51.     myImage.x     = _W*0.4
  52.     myImage.y     = _H*0.90
  53.     myImage:scale (.4, .4)
  54.  
  55.     local myImageOperator = display.newImageRect(Operator, 50,50)
  56.     group:insert( myImageOperator )
  57.     myImageOperator.x     = _W*0.5
  58.     myImageOperator.y     = _H*0.90
  59.     myImageOperator:scale (.5, .5)
  60.  
  61.     local myImage2 = display.newImageRect((NumImg2), 100 ,100)
  62.     group:insert( myImage2 )
  63.     myImage2.x     = _W*0.6
  64.     myImage2.y     = _H*0.90
  65.     myImage2:scale (.4, .4)
  66.  
  67. end
  68.  
  69. -- Set up screen
  70. function ScreenSetup(Event)
  71.  
  72.     -- Set up background
  73.     local bg = display.newImage( "assets/images/bg.png", 1600, 1200);
  74.     bg.x     = _W / 6.5
  75.     bg.y     = _H / 6.5
  76.  
  77.     -- Setup numbers ( 1-9 and 0 )
  78.     local digits = {
  79.         { 1, 0.2, 0.2 },
  80.         { 2, 0.5, 0.2 },
  81.         { 3, 0.8, 0.2 },
  82.         { 4, 0.2, 0.35 },
  83.         { 5, 0.5, 0.35 },
  84.         { 6, 0.8, 0.35 },
  85.         { 7, 0.2, 0.5 },
  86.         { 8, 0.5, 0.5 },
  87.         { 9, 0.8, 0.5 },
  88.         { 0, 0.5, 0.65 },
  89.     }
  90.     for i = 1, #digits do
  91.         local digit = digits[ i ]
  92.         local num   = display.newImageRect( "assets/images/numbers/" .. tostring( digit[ 1 ] ) .. ".png", 100 ,100)
  93.         num.x       = _W * digit[ 2 ]
  94.         num.y       = _H * digit[ 3 ]
  95.         num:addEventListener( "tap", function() NumPush( digit[ 1 ] ) ; end )
  96.     end
  97.  
  98. end
  99.  
  100. --Event Listeners For Numbers
  101. function NumPush( value )
  102.     CurrentInput = CurrentInput * 10 + value
  103.     CalcIt()
  104. end
  105.  
  106. -- Calculates results
  107. function CalcIt(result)
  108.  
  109.     if tostring( CurrentInput ):len() == tostring( result ):len() then
  110.         if CurrentInput == result then
  111.             print("You Won")
  112.             Reset()
  113.         else
  114.             print("You Lost")
  115.             Reset()
  116.         end
  117.     end
  118.  
  119. end
  120.  
  121. function Reset()
  122.     group:removeSelf()
  123.     MathSetup()
  124. end
  125.  
  126. ScreenSetup()
  127. MathSetup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement