Spel19: Bungy Jump

bungee_jumping

Vi programmerar bungy jump där hoppet startas genom touch för startposition av lina och hoppare. Vi lär oss hur en lista kan fyllas på genom tillägg av nytt element genom touch.

Template 1: (Titta på ett simulerat hopp och jämför med ett verkligt hopp)

— Bungy Jump
function setup()

print(“Bungy Jump”)
print(“Touch upprepade gånger för att skapa en lina och hoppa”)

links = {}
linkvels = {}
dt = 0.01
force = vec2(0,0)
G=vec2(0,-100)
N=0
F=5
m=1
M=2
splash=0

end

function draw()

if N==0 then
background(40, 40, 50)
sprite(“Platformer Art:Water”,10,200,500)
sprite(“Platformer Art:Water”,510,200,500)
sprite(“Platformer Art:Guy Jump”,100,800,50)
sprite(“Small World:Observatory”,50,800,50)
end

if N>0 then
background(40, 40, 50)
sprite(“Platformer Art:Water”,10,200,500)
sprite(“Platformer Art:Water”,510,200,500)
end

for k,link in pairs(links) do
fill(0, 255, 27, 255)
strokeWidth(0)
ellipse(links[k].x,links[k].y,10)

if k>1 then
stroke(0, 255, 66, 255)
strokeWidth(5)
line(links[k].x,links[k].y,links[k-1].x,links[k-1].y)
end

if k>1 and k<N then
force =F*(links[k-1] – links[k])+F*(links[k+1]-links[k])+G
linkvels[k]=(1-0.2*dt)*linkvels[k]+force*dt/m
links[k]=links[k]+linkvels[k]*dt
end

if k>1 then
force=F*(links[N-1]-links[N])+G*M
linkvels[N]=(1-0.2*dt)*linkvels[N]+force*dt/M
links[N]=links[N]+linkvels[N]*dt
sprite(“Platformer Art:Guy Jump”,links[N].x,links[N].y,50)
sprite(“Small World:Observatory”,links[1].x,links[1].y,50)
end
end

if N>1 then
if links[N].y<100 then
if splash==1 then
sound(“Game Sounds One:Crowd Sad”)
end
splash=splash+1
end
end

end

function touched(touch)

if touch.state == BEGAN then
N=N+1
table.insert(links, makeLink(touch.x,touch.y))
table.insert(linkvels,makeLinkVel())
if N==3 then
sound(“Game Sounds One:Female Cheer 1”)
end
end
if touch.state == ENDED then
links[N].x = touch.x
links[N].y = touch.y
end

end

function makeLink(x,y)
local link=vec2()
link.x = x
link.y = y
return link
end

function makeLinkVel()
local linkvel=vec2(0,0)
return linkvel
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