# Prototype math flash card app

A while back I was looking into an app to help kids learn their math more easily. I stopped work on it because to put out an apple app you need an Apple dev license. But I would hate to see my work forgotten so anyone who wants to play around with it here it is

Not every thing is polished or completed, but the core of it works like a charm. It even can square and cube things.
It accounts for answers to the “flash cards” which might be a negative number and allows you to leave negative answers. Have fun.

https://gist.github.com/anonymous/6ff55c4a6f1b305a6b90

A shoutout to this awesome code teacher. This tutorial series has some really good stuff.
The manner in which I finally got actual buttons incorporated comes from there.

http://codeatuts.blogspot.com/2012/06/tutorial-1-getting-started.html

My code will be left in comments because of character limit in body.

This code segment needs to be in a tab named ‘Button’

``````Button = class()

function Button:init(displayName)
-- you can accept and set parameters here
self.displayName = displayName

self.pos = vec2(0,0)
self.size = vec2(0,0)
self.action = nil
self.color = color(255, 0, 33, 255)
end

function Button:draw()
-- Codea does not automatically call this method
pushStyle()
fill(self.color)

font("ArialRoundedMTBold")
fontSize(22)

-- use longest sound name for size
local w,h = textSize(self.displayName)
w = w + 40
h = h + 25

roundRect(self.pos.x - w/2,
self.pos.y - h/2,
w,h,15)

self.size = vec2(w,h)

textMode(CENTER)
fill(54, 65, 96, 255)
text(self.displayName,self.pos.x+2,self.pos.y-2)
fill(255, 255, 255, 255)
text(self.displayName,self.pos.x,self.pos.y)

popStyle()
end

function Button:hit(p)
local l = self.pos.x - self.size.x/2
local r = self.pos.x + self.size.x/2
local t = self.pos.y + self.size.y/2
local b = self.pos.y - self.size.y/2
if p.x > l and p.x < r and
p.y > b and p.y < t then
return true
end

return false
end

function Button:touched(touch)
-- Codea does not automatically call this method
if touch.state == ENDED and
self:hit(vec2(touch.x,touch.y)) then
if self.action then
self.action()
end
end
end
``````

This goes in the ‘Main’ tab

``````-- Prototype

-- Use this function to perform your initial setup
function setup()
supportedOrientations(PORTRAIT)
parameter.number("NumberOne",-9999999999, math.huge, 0)
parameter.number("FactorOne",0, 12, math.random(0, 12))
parameter.number("FactorTwo",0, 12, math.random(0, 12))
parameter.number("Score", 0, math.huge, 0)
parameter.boolean("CanDecimal", true)

button9 = Button("9")
button9.action = function()
if decimal < 1 then
NumberOne = (NumberOne * 10) + 9
lastDiget = 9 elseif
decimal >=1 then
NumberOne = NumberOne + (9 * decimalplacemultiplier)
lastDiget = .9
decimal = decimal + 1
decimalplacemultiplier = decimalplacemultiplier*.1
end
end

button8 = Button("8")
button8.action = function()
if decimal < 1 then
NumberOne = (NumberOne * 10) + 8
lastDiget = 8 elseif
decimal >= 1 then
NumberOne = NumberOne + (8 * decimalplacemultiplier)
lastDiget = .8
decimal = decimal + 1
decimalplacemultiplier = decimalplacemultiplier*.1
end
end

button7 = Button("7")
button7.action = function()
if decimal < 1 then
NumberOne = (NumberOne * 10) + 7
lastDiget = 7 elseif
decimal >= 1 then
NumberOne = NumberOne + (7 * decimalplacemultiplier)
lastDiget = .7
decimal = decimal + 1
decimalplacemultiplier = decimalplacemultiplier*.1
end
end

button6 = Button("6")
button6.action = function()
if decimal < 1 then
NumberOne = (NumberOne * 10) + 6
lastDiget = 6 elseif
decimal >= 1 then
NumberOne = NumberOne + (6 * decimalplacemultiplier)
lastDiget = .6
decimal = decimal + 1
decimalplacemultiplier = decimalplacemultiplier*.1
end
end

button5 = Button("5")
button5.action = function()
if decimal < 1 then
NumberOne = (NumberOne * 10) + 5
lastDiget = 5 elseif
decimal >= 1 then
NumberOne = NumberOne + (5 * decimalplacemultiplier)
lastDiget = .5
decimal = decimal + 1
decimalplacemultiplier = decimalplacemultiplier*.1
end
end

button4 = Button("4")
button4.action = function()
if decimal < 1 then
NumberOne = (NumberOne * 10) + 4
lastDiget = 4 elseif
decimal >= 1 then
NumberOne = NumberOne + (4 * decimalplacemultiplier)
lastDiget = .4
decimal = decimal + 1
decimalplacemultiplier = decimalplacemultiplier*.1
end
end

button3 = Button("3")
button3.action = function()
if decimal < 1 then
NumberOne = (NumberOne * 10) + 3
lastDiget = 3 elseif
decimal >= 1 then
NumberOne = NumberOne + (3 * decimalplacemultiplier)
lastDiget = .3
decimal = decimal + 1
decimalplacemultiplier = decimalplacemultiplier*.1
end
end

button2 = Button("2")
button2.action = function()
if decimal < 1 then
NumberOne = (NumberOne * 10) + 2
lastDiget = 2 elseif
decimal >= 1 then
NumberOne = NumberOne + (2 * decimalplacemultiplier)
lastDiget = .2
decimal = decimal + 1
decimalplacemultiplier = decimalplacemultiplier*.1
end
end

button1 = Button("1")
button1.action = function()
if decimal < 1 then
NumberOne = (NumberOne * 10) + 1
lastDiget = 1 elseif
decimal >= 1 then
NumberOne = NumberOne + (1 * decimalplacemultiplier)
lastDiget = .1
decimal = decimal + 1
decimalplacemultiplier = decimalplacemultiplier*.1
end
end

button0 = Button("0")
button0.action = function()
if decimal < 1 then
NumberOne = (NumberOne * 10)
lastDiget = 0 elseif
decimal >= 1 then
lastDiget = 0
decimal = decimal + 1
decimalplacemultiplier = decimalplacemultiplier*.1
end
end

buttonBackspace = Button("x")
buttonBackspace.action = function()
NumberOne = 0
decimal = 0
lastDiget = 0
decimalplacemultiplier = .1
isCorrect = 0
end
decimalKey = Button(".")
decimalKey.action = function()
if decimal == 0 then
decimal = decimal + 1
end
end
SubmitKey.action = function()
if opperation == 1 and
NumberOne == FactorOne + FactorTwo then
print("correct")
isCorrect = 1
sound(SOUND_PICKUP, 16197)
correctInRow = correctInRow + 1
Score = Score + 10
Score = Score * correctInRow

elseif
opperation == 1 and
NumberOne ~= FactorOne + FactorTwo then
print("Incorrect")
isCorrect = 2
sound(SOUND_POWERUP, 35111)
correctInRow = 0
saveLocalData("scoreX", 0)
end
if opperation == 2 and
NumberOne == FactorOne - FactorTwo then
print("correct")
isCorrect = 1
sound(SOUND_PICKUP, 16197)
correctInRow = correctInRow + 1
Score = Score + 10
Score = Score * correctInRow
elseif
opperation == 2 and
NumberOne ~= FactorOne - FactorTwo then
print("Incorrect")
isCorrect = 2
sound(SOUND_POWERUP, 35111)
correctInRow = 0
saveLocalData("scoreX", 0)
end
if opperation == 3 and
NumberOne == FactorOne * FactorTwo then
print("correct")
isCorrect = 1
sound(SOUND_PICKUP, 16197)
correctInRow = correctInRow + 1
Score = Score + 10
Score = Score * correctInRow
elseif
opperation == 3 and
NumberOne ~= FactorOne * FactorTwo then
print("Incorrect")
isCorrect = 2
sound(SOUND_POWERUP, 35111)
correctInRow = 0
saveLocalData("scoreX", 0)
end
end
negativeKey = Button("-")
negativeKey.action = function() NumberOne = NumberOne * -1 end
positiveKey = Button("+")
positiveKey.action = function() NumberOne = NumberOne * 1 end
NextKey = Button("Next Question")
NextKey.action = function() restart() end
Squarekey = Button("x*x")
Squarekey.action = function() NumberOne = NumberOne*NumberOne end
cubekey = Button("x*x*x")
cubekey.action = function() NumberOne = NumberOne*NumberOne*NumberOne end
displayMode(FULLSCREEN)
lastDiget = 0
decimal = 0
decimalplacemultiplier = .1
opperation = math.random(1,3)
isCorrect = 0

end

``````

This is the rest of the ‘Main’ tab

``````-- This function gets called once every frame
function draw()
-- This sets a dark background color
pushStyle()
background(40, 40, 50)

-- This sets the line thickness
strokeWidth(5)

fontSize(75)
text(FactorOne, 550 - (textSize(FactorOne)/2), 800)
text(FactorTwo, 550 - (textSize(FactorTwo)/2), 700)
text(NumberOne, 550 - (textSize(NumberOne)/2), 540)
textAlign()
fill(127, 127, 127, 255)
stroke(127, 127, 127, 255)
rect(300, 600, 250,10 )
drawButton9()
drawButton8()
drawButton7()
drawButton6()
drawButton5()
drawButton4()
drawButton3()
drawButton2()
drawButton1()
drawButton0()
drawButtonBackspace()
drawDecimalKey()
drawSubmitKey()
drawNextKey()
drawNegativeKey()
drawPositiveKey()
drawSquarekey()
drawcubekey()
if opperation == 1 then
text("+", 320, 640) elseif
opperation == 2 then
text("-", 320, 640) elseif
opperation == 3 then
text("x", 320, 640)
end
if isCorrect == 1 then
strokeWidth(7)
stroke(0, 255, 33, 255)
line(80, 600, 110, 550)
line(110, 550, 140, 650)
elseif
isCorrect == 2 then
strokeWidth(7)
stroke(255, 2, 0, 255)
line(80, 650, 140, 570)
line(140, 650, 80, 570)
end
popStyle()
text(Score, 200, 900)
saveLocalData("highscore", Score)
saveLocalData("scoreX", correctInRow)

end

function drawButton9()
button9.pos = vec2(275, 300)
button9:draw()
end
function drawButton8()
button8.pos = vec2(330, 300)
button8:draw()
end
function drawButton7()
button7.pos = vec2(385, 300)
button7:draw()
end
function drawButton6()
button6.pos = vec2(275, 245)
button6:draw()
end
function drawButton5()
button5.pos = vec2(330, 245)
button5:draw()
end
function drawButton4()
button4.pos = vec2(385, 245)
button4:draw()
end
function drawButton3()
button3.pos = vec2(275, 190)
button3:draw()
end
function drawButton2()
button2.pos = vec2(330, 190)
button2:draw()
end
function drawButton1()
button1.pos = vec2(385, 190)
button1:draw()
end
function drawButton0()
button0.pos = vec2(275, 135)
button0:draw()
end
function drawButtonBackspace()
buttonBackspace.pos = vec2(330, 135)
buttonBackspace:draw()
end
function drawDecimalKey()
decimalKey.pos = vec2(385, 135)
decimalKey:draw()
end
function drawSubmitKey()
SubmitKey.pos = vec2(550, 300)
SubmitKey:draw()
end
function drawNextKey()
NextKey.pos = vec2(550, 360)
NextKey:draw()
end
function drawNegativeKey()
negativeKey.pos = vec2(220, 135)
negativeKey:draw()
end
function drawPositiveKey()
positiveKey.pos = vec2(220, 190)
positiveKey:draw()
end
function drawSquarekey()
Squarekey.pos = vec2(150, 130)
Squarekey:draw()
end
function drawcubekey()
cubekey.pos = vec2(150, 80)
cubekey:draw()
end
function touched(touch)
button9:touched(touch)
button8:touched(touch)
button7:touched(touch)
button6:touched(touch)
button5:touched(touch)
button4:touched(touch)
button3:touched(touch)
button2:touched(touch)
button1:touched(touch)
button0:touched(touch)
buttonBackspace:touched(touch)
decimalKey:touched(touch)
SubmitKey:touched(touch)
NextKey:touched(touch)
negativeKey:touched(touch)
positiveKey:touched(touch)
Squarekey:touched(touch)
cubekey:touched(touch)
end

``````

This one goes in a tab named ‘RoundRect’

``````function roundRect(x,y,w,h,r)
pushStyle()

insetPos = vec2(x+r,y+r)
insetSize = vec2(w-2*r,h-2*r)

--Copy fill into stroke
local red,green,blue,a = fill()
stroke(red,green,blue,a)

noSmooth()
rectMode(CORNER)
rect(insetPos.x,insetPos.y,insetSize.x,insetSize.y)

if r > 0 then
smooth()
lineCapMode(ROUND)
strokeWidth(r*2)

line(insetPos.x, insetPos.y,
insetPos.x + insetSize.x, insetPos.y)
line(insetPos.x, insetPos.y,
insetPos.x, insetPos.y + insetSize.y)
line(insetPos.x, insetPos.y + insetSize.y,
insetPos.x + insetSize.x, insetPos.y + insetSize.y)
line(insetPos.x + insetSize.x, insetPos.y,
insetPos.x + insetSize.x, insetPos.y + insetSize.y)
end
popStyle()
end
``````

@Archimedes - it is easier for people if you put long code like this into a gist (or something similar)and post the link here.

To get the code to paste into separate tabs,

1. to copy it, go out to the main Codea screen with the project list, press on your project until you get the copy option. This copies the whole project and tags each tab.

2. paste into gist (or whatever)

3. other users can then copy it from there. If they press and hold the Add new project button, they will be given the option to paste into project. This splits the code into all the tabs.

Ok thanks.

im addicted to math?especially highschool and college math

@firewolf, i have some math homework here if you want it! Lol, jk =))