Spel44: Stapla Klossar och Räkna

Jenga_distorted

Som tillämpning på Codeas fysikmotor och binär räkning programmerar vi ett spel som går ut på att stapla så många klossar som möjligt på varann och samtidigt räkna antalet klossar binärt.

Följ ett försök här. Jämför med Mechanics2 på App Store.

Template:

— Stapla klossar och räkna
function setup()
print(“Hello Physics!”)

print(“Touch the screen to make boxes”)
bodies = {}

 

local floor = makeBox(WIDTH/2, 50, 1100, 70, 0)
floor.type = STATIC

table.insert(bodies, floor)

I=6
bit={}
for i=1,I do
bit[i]=0
end
font(“AmericanTypewriter”)
fontSize(50)
n=0
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:64bitr”)
end
function draw()

background(179, 222, 220, 255)
for k,body in pairs(bodies) do
drawBody(body)
end
print(n)
fill(255, 0, 0, 255)
for i=1,I-1 do
text(bit[i],600-50*i,650)
if bit[i]>0 then
sprite(bitimager[i],600-50*i,600,50)
end
end
fontSize(30)
text(“Stapla klossar genom att klicka på skärmen.”,350,900)
text(“Hur många kan Du stapla?.”,350,800)
fill(0, 255, 17, 255)
text(n,500,500)

end

function touched(touch)
— When you touch the screen, create a random box
if touch.state == BEGAN then
n=n+1
table.insert(bodies, makeBox(touch.x, touch.y, math.random(25, 50), math.random(25, 50), 0))
bit[1]=bit[1]+1
for i=1,I do
if i<I then
if bit[i]==2 then
bit[i]=0
bit[i+1]=bit[i+1]+1
end
end
end

end
end

— Helper function to create a box using a polygon body
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()

stroke(0, 14, 255, 255)
fill(0, 37, 255, 255)
translate(body.x, body.y)
rotate(body.angle)
fill()

— Draw body based on shape type
if body.shapeType == POLYGON then
strokeWidth(10.0)
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(3.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