Spel37: Binär Addition

binaryAdditionTable

Vi programmerar en app för binär addition där flera binära tal väljs genom att ändra från 0 till 1 genom att klicka på skärmen, och talen sedan adderas genom att symboler parvis sammanförs genom dragning. Vi använder en Codea Helperclass för knappar och touch  (se här). Appen lämplig från 3 år.

Titta på ett exempel här. Jämför med Counting på App Store.

Template:

–Helperclass

N=4
I=7
bitimageg = {}
bitimageg[1] =readImage(“Dropbox:1bitg”)
bitimageg[2] =readImage(“Dropbox:2bitg”)
bitimageg[3] =readImage(“Dropbox:4bitg”)
bitimageg[4] =readImage(“Dropbox:8bitg”)
bitimageg[5] =readImage(“Dropbox:16bitg”)
bitimageg[6] =readImage(“Dropbox:32bitg”)
bitimageg[7] =readImage(“Dropbox:64bitg”)
local bit={}
for n=1,N+1 do
bit[n]={}
end
local aButton1,aButton2

function setup()

supportedOrientations(LANDSCAPE_ANY)
font(“AmericanTypewriter”)
fontSize(30)

aButton1 = Button(“Cargo Bot:Fast Button Active”,vec2(100,1000))
aButton2 = Button(“Cargo Bot:Fast Button Active”,vec2(500,1000))

for n=1,N do
for i=1,I do
bit[n][i] = SpriteObject(bitimageg[i],vec2(800-100*i,200*n+100),0)
end
end
for i=1,I do
bit[N+1][i] = SpriteObject(bitimageg[i],vec2(800-100*i,100),0)
end
sound(“A Hero’s Quest:Door Open”)
sum={}
init=0
speech.rate=0.01
end

function touched(touch)

aButton1:touched(touch)
if (aButton1.selected==true) then
init=1
speech.say(“be-stem taal”)
sound(“Game Sounds One:Knock 1”)
end

aButton2:touched(touch)
if (aButton2.selected==true) then
init=0
speech.say(“addeeraa taal”)
sound(“Game Sounds One:Knock 2”)
end

for n=1,N+1 do
for i=1,I do
bit[n][i]:touched(touch)
end
end

finger=vec2(touch.x,touch.y)

if init==1 then

for n=1,N do
for i=1,I-2 do
d=(bit[n][i].objectStartLocation-finger):len()
if bit[n][i].ID==0 and d<50 then
bit[n][i].ID=1
sound(“Game Sounds One:Assembly 6”)
end
end
end

end

for n=2,N do
for i=1,I-1 do
for m=1,n-1 do
if bit[n][i].ID*bit[m][i].ID>0 then
if (bit[n][i]:isTouching(bit[m][i])==true) then
bit[n][i].ID=0
bit[m][i].ID=0
bit[N+1][i+1].ID=bit[N+1][i+1].ID+1
sound(“Game Sounds One:Assembly 2”)
end
end
end
end
end

for i=1,I-1 do
for n=1,N do
if bit[N+1][i].ID*bit[n][i].ID>0 then
if (bit[N+1][i]:isTouching(bit[n][i])==true) then
bit[N+1][i].ID=0
bit[n][i].ID=0
bit[N+1][i+1].ID=bit[N+1][i+1].ID+1
sound(“Game Sounds One:Assembly 2”)
end
end
end
end

for i=1,I-1 do
if bit[N+1][i].ID>1 then
bit[N+1][i].ID=bit[N+1][i].ID-2
bit[N+1][i+1].ID=bit[N+1][i+1].ID+1
sound(“Game Sounds One:Assembly 2”)
end
end

for n=1,N do
for i=1,I do
if bit[n][i].ID>0 then
if (bit[n][i]:isTouching(bit[N+1][i])==true) then
bit[n][i].ID = 0
bit[N+1][i].ID=bit[N+1][i].ID+1
sound(“Game Sounds One:Assembly 2”)
end
end
end
end

end
function draw()

background(40, 40, 50)
fill(255, 255, 255, 255)
for n=2,N+1 do
rect(10,200*n-150,800,100)
end
fill(218, 84, 84, 255)
rect(10,50,800,100)
for n=1,N+1 do
for i=1,I do
bit[n][i]:draw()
end
end

fill(0, 76, 255, 255)
for n=1,N+1 do
for i=1,I do
text(bit[n][i].ID,800-100*i,100+200*n)
end
end
for i=1,I do
text(bit[N+1][i].ID,800-100*i,100)
aButton1:draw()
aButton2:draw()
text(“Bestäm tal”,200,1000)
text(“Addera tal”,630,1000)
end

end

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s