Vi programmerar ett spel som går ut på att med en viss given energimängd genomföra ett maratonlopp över kuperad terräng så snabbt som möjligt. Displayen visar hastighet, momentan energiförbrukning, total energiförbrukning och att man kan styra hastigheten genom framåt och bakåt knappar. Om den tillgängliga energimängden är förbrukad innan målgång kollapsar man och får bryta. Titta på ett sådant fall här. Man kan här få hands-on erfarenhet av begrepp som hastighet, kraft, arbete, energi och effekt = energi/tidsenhet.
Jämför med Running1 på App Store.
Template:
–Micky Mouse Marathon
displayMode(FULLSCREEN)
local aButton1,aButton2
function setup()
parameter.integer(“frameDelay”, 1,10,1)
animation={}
animation[1] = Animation({“Dropbox:MMicky1″,”Dropbox:MMicky2″,”Dropbox:MMicky3″,”Dropbox:MMicky4″,”Dropbox:MMicky5″,”Dropbox:MMicky6″,”Dropbox:MMicky7″,”Dropbox:MMicky8″,”Dropbox:MMicky9″,”Dropbox:MMicky10″,”Dropbox:MMicky11″,”Dropbox:MMicky12″,”Dropbox:MMicky13″,”Dropbox:MMicky14”},1)
animation[2] = Animation({“Dropbox:MMicky15″,”Dropbox:MMicky16″,”Dropbox:MMicky17″,”Dropbox:MMicky18″,”Dropbox:MMicky19″,”Dropbox:MMicky20″,”Dropbox:MMicky21″,”Dropbox:MMicky22″,”Dropbox:MMicky23”},10)
aButton1=Button(“Cargo Bot:Command Right”,vec2(600,100),10)
aButton2=Button(“Cargo Bot:Command Left”,vec2(500,100))
aButton3=Button(“Cargo Bot:Play Button”,vec2(100,100))
aButton4=Button(“Cargo Bot:Stop Button”,vec2(300,100))
aButton5=Button(“Cargo Bot:Replay Button”,vec2(800,100))
track={}
slope={}
c1=math.random(5,5)
c2=math.random(2,3)
c3=math.random(2,3)
c4=math.random(1,2)
c5=math.random(3,4)
c6=math.random(3,4)
n=0
m=0
N=0
x=1
vel=30
force=1
work=0
dt=0.01
start=0
stop=1
newtrack=0
t=0
delay=0
end
function draw()
background(170,200, 255, 255)
if stop==0 then
if delay<100 then
animation[2]:draw(x-5*slope[xfloor]+110,y-50)
delay=delay+1
else
sprite(“Dropbox:Micky23”,x+110,y-50,100)
fill(7, 6, 6, 255)
for i=1,1024 do
ellipse(i+100,50*track[i]+120,10)
end
end
end
if start==1 then
fill(12, 12, 12, 255)
text(math.floor(t),500,650)
if stop==1 then
if newtrack ==1 then
c1=math.random(5,5)
c2=math.random(2,3)
c3=math.random(2,3)
c4=math.random(1,2)
c5=math.random(3,4)
c6=math.random(3,4)
newtrack=0
end
for i=1,1024 do
track[i]=c1*math.sin(i*math.pi/1024)+c2*math.sin(2*i*math.pi/ 1024)+c3*math.sin(3*i*math.pi/1024)+c4*math.sin(4*i*math.pi/1024)
+c5*math.sin(5*i*math.pi/1024)+c6*math.sin(6*i*math.pi/1024)
end
for i=1,1024 do
slope[i]=c1*math.cos(i*math.pi/1024)+c2*math.cos(2*i*math.pi/1024)+c3*math.cos(3*i*math.pi/1024)+c4*math.cos(4*i*math.pi/1024)
+c5*math.cos(5*i*math.pi/1024)+c6*math.cos(6*i*math.pi/1024)
end
t=t+5*dt
fontSize(40)
fill(13, 15, 17, 255)
text(math.floor(t),500,650)
if x<950 then
x=x+vel*dt
xfloor=math.floor(x)
y=track[xfloor]*50+200
force=0.002*vel^2+slope[xfloor]
work=work+force*vel*dt
animation[1]:draw(x-5*slope[xfloor]+110,y-50)
end
if 0.05*work>300 then
animation[2]:draw(x-5*slope[xfloor]+110,y-50)
stop=0
end
fill(7, 6, 6, 255)
for i=1,1024 do
ellipse(i+100,50*track[i]+120,10)
end
fill(255, 0, 9, 255)
text(“Totalt arbete”,800,350)
rect(850,400,50,0.05*work)
fill(255, 0, 252, 255)
rect(800,400,50,0.2*force*vel)
text(“Arbete/tidsenhet=Effekt”,800,300)
fill(0, 216, 255, 255)
text(“Hastighet”,800,150)
rect(700,400,50,3*vel)
fill(0, 23, 255, 255)
text(“Kraft”,800,250)
if force>0 then
rect(750,400,50,5*force)
else
rect(750,400+10*force,50,-10*force)
end
fill(50, 255, 0, 255)
text(“Återstående Energi”,800,200)
rect(900,400,50,300-0.05*work)
end
end
aButton1:draw()
aButton2:draw()
aButton3:draw()
aButton4:draw()
aButton5:draw()
sprite(“Small World:Flag”,1000,300,100)
end
Animation = class()
function Animation:init(frames, delay)
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
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, y,100)
end
function Animation:setFrameDelay(newDelay)
self.frameDelay = newDelay
end
function touched(touch)
if CurrentTouch.state==ENDED then
aButton1:touched(touch)
if (aButton1.selected==true) then
vel=vel+1
sound(“Game Sounds One:Knock 1”)
end
aButton2:touched(touch)
if (aButton2.selected==true) then
vel=vel-1
sound(“Game Sounds One:Knock 2”)
end
end
aButton3:touched(touch)
if (aButton3.selected==true) then
start=1
sound(“Game Sounds One:Knock 1”)
end
aButton4:touched(touch)
if (aButton4.selected==true) then
stop=0
sound(“Game Sounds One:Knock 2”)
end
aButton5:touched(touch)
if (aButton5.selected==true) then
newtrack=1
sound(“Game Sounds One:Knock 2”)
end
end