Advertisement
Rakoonic

Praxis #6 - The Workening

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