-- MathGame
supportedOrientations(LANDSCAPE_ANY)
--displayMode(FULLSCREEN)
-- Use this function to perform your initial setup
function setup()
value = 0
create()
noFill()
noSmooth()
noStroke()
pushStyle()
-- Sprites in variables
right=0
wrong=0
clearButton = "Project:clearButton"
backspaceButton = "Project:backspaceButton"
enterButton = "Project:enterButton"
professor = "Project:professor"
speechBubble = "Project:speechBubble"
-- List of positions for the keypad
local pos = {vec2(-250,0),vec2(-150,0),vec2(-50,0),vec2(-250,-100),vec2(-150,-100),ve c2(-50,-100),vec2(-250,-200),vec2(-150,-200),vec2(-50,-200)}
pos[0]=vec2(-150,-300)
buttons["clear"],buttons["backspace"],buttons["enter"]={},{},{}
buttons={}
for i=0,9 do
buttons[i]={}
buttons[i].img="Project:button"..i
buttons[i].pos=vec2(WIDTH/2,HEIGHT/2)+pos[i]
end
end
-- List of positions for the clear, backspace and enter buttons
buttons["clear"].pos=vec2(WIDTH/2-250, HEIGHT/2-300, 75)
buttons["clear"].img="Project:clearButton"
buttons["backspace"].pos=vec2(WIDTH/2-50, HEIGHT-300, 75)
buttons["backspace"].img="Project:backspaceButton"
buttons["enter"].pos=vec2(WIDTH/2+50, HEIGHT+150, 75)
buttons["enter"].img="Project:enterButton"
function create()
a={}
ans={}
str={}
choice=math.random(4)
c=math.random(4) -- + - x /
for z=1,4 do
a[z]=vec2(math.random(99),math.random(99))
if c==1 then
str[z]=string.format("%d + %d = ",a[z].x,a[z].y)
ans[z]=math.tointeger(a[z].x+a[z].y)
elseif c==2 then
str[z]=string.format("%d - %d = ",a[z].x,a[z].y)
ans[z]=math.tointeger(a[z].x-a[z].y)
elseif c==3 then
str[z]=string.format("%d x %d = ",a[z].x,a[z].y)
ans[z]=math.tointeger(a[z].x*a[z].y)
elseif c==4 then
str[z]=string.format("%d / %d = ",a[z].x,a[z].y)
ans[z]=a[z].x/a[z].y
end
end
end
-- This function gets called once every frame
function touched(t)
if t.state==ENDED then --wait for touch to end
local touch=vec2(t.x,t.y)
for i=0,9 do
if touch:dist(buttons[i].pos)<75 then
keyTouched=i
value=value*10+i
return
end
if touch:dist(buttons["clear"].pos)<75 then -- clear
value=0
end
if touch:dist(buttons["backspace"].pos)<75 then -- backspace
value=value//10
end
end
end
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
-- This displays the 0-9 keypad
for i=0,9 do
sprite(buttons[i].img,buttons[i].pos.x,buttons[i].pos.y,75)
end
-- This displays the input rectangle correlating with the keypad
rect(WIDTH/2-100, HEIGHT/2-300)
-- This displays the backspace, clear button and enter button
sprite(clearButton, WIDTH/2-250, HEIGHT/2-300, 75)
sprite(backspaceButton, WIDTH/2-50, HEIGHT/2-300, 75)
sprite(enterButton, WIDTH/2+50, HEIGHT/2+130, 75)
-- This displays the "Right" and "Wrong" at the top of the screen and the increments
fill(255)
fontSize(48)
text("Right "..right,WIDTH/2-100,HEIGHT-50) -- "Right 0"
text("Wrong "..wrong,WIDTH/2+100,HEIGHT-50) -- "Wrong 0"
rect(WIDTH/2-300, HEIGHT/2+100, 300, 50) -- Input bar
for i,b in pairs (buttons) do
sprite(b.img,b.pos.x,b.pos.y,75)
end
fill(0)
text(value, WIDTH/2-150, HEIGHT/2+120)
-- This displays the professor sprite and his speech bubble
sprite(professor, WIDTH/2+225,HEIGHT/2-100, 350, 600)
sprite(speechBubble, WIDTH/2+75, HEIGHT/2+225, 250, 150)
fill(0, 0, 0, 255)
text(str[choice],WIDTH/2+75,HEIGHT/2+230)
end