Advertisement
CodingIsLife

Math Game(Codea's local images)

Nov 16th, 2015
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.34 KB | None | 0 0
  1. supportedOrientations(LANDSCAPE_ANY)
  2. displayMode(FULLSCREEN)
  3.  
  4. function setup()
  5.     lastProb="" --previous problem
  6.     tries,value,right,wrong,size=0,0,0,0,30
  7.     offset=vec2(WIDTH/2,HEIGHT/2)
  8.     b={"0️⃣","1️⃣","2️⃣","3️⃣","4️⃣","5️⃣","6️⃣","7️⃣","8️⃣","9️⃣","❌","🔙","☑️"}
  9.     pos={vec2(-150,-300),vec2(-250,0),vec2(-150,0),vec2(-50,0),vec2(-250,-100),
  10.         vec2(-150,-100),vec2(-50,-100),vec2(-250,-200),vec2(-150,-200),vec2(-50,-200),
  11.         vec2(-250,-300),vec2(-50,-300),vec2(80,115)}
  12.     create()
  13. end
  14.  
  15. function create()    
  16.     a,ans,str={},{},{}
  17.     choice=math.random(4)
  18.     c=math.random(4)
  19.     for z=1,4 do
  20.         if c==1 then    --  add
  21.             a[z]=vec2(math.random(99),math.random(99))
  22.             str[z]=string.format("%d + %d = ",a[z].x,a[z].y)
  23.             ans[z]=math.tointeger(a[z].x+a[z].y)
  24.         elseif c==2 then    --  subtract
  25.             a1=math.random(99)
  26.             a2=math.random(99)
  27.             a[z]=vec2(math.max(a1,a2),math.min(a1,a2))  -- prevent negative answer
  28.             str[z]=string.format("%d - %d = ",a[z].x,a[z].y)
  29.             ans[z]=math.tointeger(a[z].x-a[z].y)
  30.         elseif c==3 then    -- multiply
  31.             a[z]=vec2(math.random(99),math.random(99))
  32.             str[z]=string.format("%d x %d = ",a[z].x,a[z].y)
  33.             ans[z]=math.tointeger(a[z].x*a[z].y)
  34.         elseif c==4 then    -- divide
  35.             a1=math.random(30)
  36.             a2=math.random(50,99)//a1
  37.             a[z]=vec2(a1*a2,a1) -- prevent fractional answer
  38.             str[z]=string.format("%d / %d = ",a[z].x,a[z].y)
  39.             ans[z]=a[z].x/a[z].y
  40.         end        
  41.     end
  42. end
  43.  
  44. function touched(t)
  45.     if t.state==ENDED then --wait for touch to end
  46.         for i=0,9 do
  47.             if vec2(t.x,t.y):dist(pos[i+1]+offset)<size then
  48.                 value=value*10+i
  49.                 break
  50.             end
  51.         end
  52.         if vec2(t.x,t.y):dist(pos[11]+offset)<size then        -- clear
  53.             value=0
  54.         elseif vec2(t.x,t.y):dist(pos[12]+offset)<size then    -- backspace
  55.             value=value//10
  56.         elseif vec2(t.x,t.y):dist(pos[13]+offset)<size then    -- enter
  57.             if value==ans[choice] then
  58.                 corr=true
  59.                 lastProb=str[choice]..math.tointeger(ans[choice])
  60.                 create()
  61.                 right=right+1
  62.                 tries=0
  63.             else
  64.                 wrong=wrong+1
  65.                 tries=tries+1
  66.                 if tries>2 then  -- allow 3 tries
  67.                     corr=false
  68.                     lastProb=str[choice]..math.tointeger(ans[choice])
  69.                     tries=0
  70.                     create()
  71.                 end
  72.             end
  73.             value=0
  74.         end        
  75.     end
  76. end
  77.  
  78. function draw()
  79.     --background(40, 40, 50, 255)
  80.     sprite("SpaceCute:Background", WIDTH/2, HEIGHT/2, 1024, 768)
  81.  
  82.     stroke(0, 0, 0, 255) -- will give an outline to the rectangles
  83.     strokeWidth(3)
  84.  
  85.     -- draws the keypad background
  86.     fill(255, 255, 255, 255)
  87.     rect(offset.x-300, offset.y-345,300,400)
  88.     fill(255)
  89.     fontSize(80)
  90.  
  91.     for i=1,13 do -- these are the buttons being drawn
  92.         text(b[i],offset.x+pos[i].x,offset.y+pos[i].y)
  93.     end
  94.    
  95.     -- this displays the "Right" and "Wrong" at the top of the screen and their values
  96.     fontSize(48)
  97.     text("Right "..right,offset.x-100,HEIGHT-50) -- "Right 0"
  98.     text("Wrong "..wrong,offset.x+100,HEIGHT-50) -- "Wrong 0"
  99.    
  100.     -- this show the keyed value
  101.     rect(offset.x-300, offset.y+90, 300, 50) -- Input bar
  102.     fill(0, 0, 0, 255)
  103.     text(value,offset.x-150,offset.y+115) --keyed value
  104.    
  105.     -- this displays the professor sprite and his speech bubble
  106.     sprite("Planet Cute:Character Boy", offset.x+225,offset.y-100, 350, 600) -- sprite on the right side of screen
  107.     sprite("Platformer Art:Cloud 3", offset.x+240, offset.y+80, 250, 150)   -- cloud sprite
  108.    
  109.     -- this displays the problem to solve
  110.     fill(0, 0, 0, 255)
  111.     text(str[choice],offset.x+255,offset.y+50) --problem placed in/on top of the cloud
  112.    
  113.     -- this displays the last problem, green was right, red was wrong
  114.     if corr then
  115.         fill(0,255,0)
  116.     else
  117.         fill(255, 0, 0, 255)
  118.     end
  119.     text(lastProb,WIDTH-300,50) --displays the correct answer in the bottom right corner under the sprite
  120. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement