Spel43: Räkna Steg – Gissa Vinnare

IMG_0430

Som tillämpning av räkning av antal samt animering programmerar vi en tävling mellan tre löpare där man gissar vinnare och/eller räknar antalet steg. Löparnas hastigheter ändras slumpmässigt.

Titta på ett race här och här.

Template:

–Animation

— Use this function to perform your initial setup
function setup()

parameter.integer(“frameDelay”, 1,10,1)
parameter.integer(“frameSpeed3”,1,1000,1)
parameter.integer(“frameSpeed4”,1,1000,1)
parameter.integer(“frameSpeed5”,1,1000,1)

animation3 = Animation({“Dropbox:runman4″,”Dropbox:runman5″,”Dropbox:runman6”,
“Dropbox:runman8″,”Dropbox:runman7″,”Dropbox:runman9”,
“Dropbox:runman10″,”Dropbox:runman12″,”Dropbox:runman11”,
“Dropbox:runman13″,”Dropbox:runman15″,”Dropbox:runman16”,
“Dropbox:runman18″,”Dropbox:runman18″,”Dropbox:1”,
“Dropbox:runman20″,”Dropbox:runman22″,”Dropbox:runman21”,
“Dropbox:runman23″,”Dropbox:runman24″,”Dropbox:runman26”,
“Dropbox:runman28″,”Dropbox:runman29″,”Dropbox:runman30”},1,1)

animation4 = Animation({“Dropbox:runman21″,”Dropbox:runman23”,
“Dropbox:runman24″,”Dropbox:runman26″,”Dropbox:runman28”,
“Dropbox:runman29″,”Dropbox:runman30″,”Dropbox:runman4”,
“Dropbox:runman5″,”Dropbox:runman6″,”Dropbox:runman8”,
“Dropbox:runman7″,”Dropbox:runman9″,”Dropbox:runman10”,
“Dropbox:runman12″,”Dropbox:runman11″,”Dropbox:runman13”,
“Dropbox:runman15″,”Dropbox:runman16″,”Dropbox:runman18”,
“Dropbox:runman18″,”Dropbox:1″,”Dropbox:runman20”,
“Dropbox:runman22”},1,1)

animation5 = Animation({“Dropbox:runman16″,”Dropbox:runman18”,
“Dropbox:runman18″,”Dropbox:1″,”Dropbox:runman20”,
“Dropbox:runman22″,”Dropbox:runman21″,”Dropbox:runman23”,
“Dropbox:runman24″,”Dropbox:runman26″,”Dropbox:runman28”,
“Dropbox:runman29″,”Dropbox:runman30″,”Dropbox:runman4”,
“Dropbox:runman5″,”Dropbox:runman6″,”Dropbox:runman8”,
“Dropbox:runman7″,”Dropbox:runman9″,”Dropbox:runman10”,
“Dropbox:runman12″,”Dropbox:runman11″,”Dropbox:runman13”,
“Dropbox:runman15”},1,1)

I=5
bit={}
for i=1,I do
bit[i]=0
end
n=0
m=0
N=0

frameSpeed3=100
frameSpeed4=100
frameSpeed5=100

end

— This function gets called once every frame
function draw()
frameSpeed3=frameSpeed3+math.random(-1,2)
frameSpeed4=frameSpeed4+math.random(-1,2)
frameSpeed5=frameSpeed5+math.random(-1,2)

for i=1,I do
print(bit[i])
end

background(28, 38, 245, 255)

animation3:setFrameDelay(frameDelay)
animation3:setFrameSpeed3(frameSpeed3)
animation3:draw(200, 800)

animation4:setFrameDelay(frameDelay)
animation4:setFrameSpeed4(frameSpeed4)
animation4:draw(200, 500)

animation5:setFrameDelay(frameDelay)
animation5:setFrameSpeed5(frameSpeed5)
animation5:draw(200, 200)

font(“AmericanTypewriter”)
fontSize(50)
fill(255, 0, 28, 255)
rect(10,50,N*10,20)
text(math.floor(N),500,900)
for i=1,I do
text(bit[i],700-50*i,1000)
end
noFill()
strokeWidth(5)
rect(0,50,700,20)
fill(0, 61, 255, 255)
rect(0,200,frameSpeed3,20)
fill(0, 255, 43, 255)
rect(0,220,frameSpeed4,20)
fill(255, 0, 24, 255)
rect(0,240,frameSpeed5,20)
end

Animation = class()

function Animation:init(frames,delay,speed)
self.frames = frames — table of sprite names
self.currentFrame = 1 — the current frame
self.frameDelay = delay — how many iterations of main draw loop before changing frame
self.count = 1 — keeps track of iterations passed
self.frameSpeed = speed
end

function Animation:draw(x,y)

print(m)
— Increment the counter
self.count = self.count + 1
— Check if frame should be updated
if self.count > self.frameDelay then
— Reset count and increment current frame
self.count = 1
self.currentFrame = self.currentFrame + 1
— Check if we’ve reached the last frame
if self.currentFrame > table.maxn(self.frames) then
— Back to first frame
self.currentFrame = 1
end
end
m=m+1/80

— Draw correct frame
sprite(self.frames[self.currentFrame], x+self.frameSpeed, y,200)
end

function Animation:setFrameDelay(newDelay)
self.frameDelay = newDelay
end

function Animation:setFrameSpeed3(newSpeed)
self.frameSpeed = newSpeed
end

function Animation:setFrameSpeed4(newSpeed)
self.frameSpeed = newSpeed
end

function Animation:setFrameSpeed5(newSpeed)
self.frameSpeed = newSpeed
end

function touched(touch)

if CurrentTouch.state==ENDED then
N=N+1
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

 

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