Spel41: Binära Tal med Fysik

IMG_0344

Vi programmerar en app för förskolebarn som visar hur räkning med binära tal går till. Man klickar på molnet och räknar regndropparna. Mycket instruktivt!

Programmet använder Codeas fysikmotor via physics.body().

Titta på ett exempel här.

Template:

— Bit Physics

function setup()

print(“Klicka på molnet för att skapa regndroppar”)
print(“Räkna regndroppar”)

font(“AmericanTypewriter”)
fontSize(80)

— Table to store our physics bodies

bitimager = {}
bitimager[1] =readImage(“Dropbox:1bitr”)
bitimager[2] =readImage(“Dropbox:2bitr”)
bitimager[3] =readImage(“Dropbox:4bitr”)
bitimager[4] =readImage(“Dropbox:8bitr”)
bitimager[5] =readImage(“Dropbox:16bitr”)
bitimager[6] =readImage(“Dropbox:32bitr”)
bitimager[7] =readImage(“Dropbox:64bit”)
I=7

bodies = {}
bits={}
things={}
for i=1,I do
bits[i]={}
end

— Create some static boxes (not effected by gravity or collisions)
— local left = makeBox(WIDTH/4, HEIGHT/2, WIDTH/3, 15, -30)
— left.type = STATIC

— local right = makeBox(WIDTH – WIDTH/4, HEIGHT/2, WIDTH/3, 15, 30)
— right.type = STATIC

local floor = makeBox(350,100,680,20,0)
floor.type = STATIC

local wall1= makeBox(50,150,20,200, 0)
wall1.type = STATIC

local wall2= makeBox(350,150,20,200, 0)
wall2.type = STATIC

local wall3= makeBox(650,150,20,200, 0)
wall3.type = STATIC

table.insert(bodies, left)
table.insert(bodies, right)
table.insert(bodies, floor)
table.insert(bodies, wall1)
table.insert(bodies, wall2)
table.insert(bodies, wall3)

N=0
end

— This function gets called once every frame
function draw()
— This sets a dark background color
background(16, 175, 228, 255)
sprite(“Platformer Art:Cloud 1”, 500,800,300)
sprite(“Platformer Art:Battor Flap 1”,300,700,30)
sprite(“Dropbox:landscape2”,300,200,1000)
fontSize(30)
text(“Klicka och räkna”,500,800)
— Draw all our physics bodies
fill(255, 0, 14, 255)

for k,body in pairs(bodies) do
drawBody(body)
end

for i=1,I do
for k,body in pairs(bits[i]) do
drawBody(body)
sprite(bitimager[i],body.x,body.y,2^(i/2)*10)
end
fontSize(80)
imax=table.maxn(bits[i])
if imax==1 then
text(1,500-50*i,900)
sprite(bitimager[i],500-50*i,950,40)
else
text(0,500-50*i,900)
end
end

for k,body in pairs(things) do
drawBody(body)
sprite(“Small World:Raindrop”,body.x,body.y,30)
end

text(N,650,350)

fill(14, 14, 14, 255)
rect(10,90,680,20)
rect(40,50,20,200)
rect(340,50,20,200)
rect(640,50,20,200)

end

function touched(touch)
if touch.state == BEGAN then
sound(“A Hero’s Quest:Rain”)
N=N+1
if touch.x>400 and touch.x<700 then
if touch.y>700 and touch.y<800 then
table.insert(bits[1],makeBox(touch.x-300,touch.y,20,20,0))
table.insert(things,makeBox(touch.x,touch.y,15,15,0))
for i=1,I-1 do
if math.floor(N/2^i)==N/2^i then
nmax=table.maxn(bits[i])
table.insert(bits[i+1],makeBox(bits[i][nmax].x,bits[i][nmax].y,2^((i-1)/2)*20,2^((i-1)/2)*20,0))
if nmax>1 then
bits[i][nmax]:destroy()
bits[i][nmax-1]:destroy()
table.remove(bits[i],nmax)
table.remove(bits[i],nmax-1)
end
end
end
end
end
end
end

function makeBox(x,y,w,h,r)
— Points are defined in counter-clockwise order

local body = physics.body(POLYGON,vec2(-w/2, h/2),
vec2(-w/2, -h/2), vec2(w/2, -h/2), vec2(w/2, h/2))

— Set the body’s transform (position, angle)
body.x = x
body.y = y
body.angle = r
body.restitution=0.5

— Make movement smoother regardless of framerate
body.interpolate = true

return body
end

— Helper function to draw a physics body
function drawBody(body)
— Push style and transform matrix so we can restore them after
pushStyle()
pushMatrix()

translate(body.x, body.y)
rotate(body.angle)
stroke(12, 13, 13, 255)
strokeWidth(50)

— Draw body based on shape type
if body.shapeType == POLYGON then
strokeWidth(3)
local points = body.points
for j = 1,#points do
a = points[j]
b = points[(j % #points)+1]
line(a.x, a.y, b.x, b.y)
end
elseif body.shapeType == CHAIN or body.shapeType == EDGE then
strokeWidth(3.0)
local points = body.points
for j = 1,#points-1 do
a = points[j]
b = points[j+1]
line(a.x, a.y, b.x, b.y)
end
elseif body.shapeType == CIRCLE then
strokeWidth(0)
line(0,0,body.radius-3,0)
ellipse(0,0,body.radius*2)
end

— Restore style and transform
popMatrix()
popStyle()
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