os.cpu(333)

dofile("GAMES/DK/src/variables.lua")
dofile("GAMES/DK/src/funciones.lua")
dofile("GAMES/DK/src/niveles.lua")



intervalo=timer.new()

tiempo_juego=timer.new()

atacando=timer.new()

function DKGAME(noload)

if not noload then
	liberar(8)
end

motor()

for i=1,30 do
b[i]={x=d.x,y=-100,w=14,h=12,d=0,c=0,i=bar[1],t=timer.new()}
b[i].t:start()
end

intervalo:start()
girar:start()

tiempo_juego:start()


while true do
controls.read()
esce[nivel_actual]:blit(0,0)
jump.h=jump.i:height() 
jump.w=jump.i:width() 

if jump.x>465 then jump.x=465 end
if jump.y>250 then jump.y=250 end
if jump.x<0 then jump.x=0 end
if jump.y<0 then jump.y=0 end


if intervalo:time()>1000 and intervalo:time()<2000 then d.t:start() else d.t:reset() d.t:stop() end

if intervalo:time()>2500 then 

if nivel_actual!=4 then b[actual].d=math.random(1,2) else b[actual].d=2 end
if b[actual].d==1 then
b[actual].x=d.x-20
end
if b[actual].d==2 then
b[actual].x=d.x+45
end
b[actual].y=d.y 
if b[actual].d==2 then d.sprites_dk=dki[5] end
if b[actual].d==1 then d.sprites_dk=dki[4] end

actual=actual+1 intervalo:reset()  end


if actual>19 then actual=1 end

for i=1,#sup do

for z=1,30 do
if col(b[z],jump) then perder() end

if nivel_actual!=4 and nivel_actual!=5 then
if b[z].d==1 then b[z].x=b[z].x+0.1 end
if b[z].d==2 then b[z].x=b[z].x-0.1 end
elseif nivel_actual==4 then
if b[z].d==1 then b[z].x=b[z].x+0.4 end
if b[z].d==2 then b[z].x=b[z].x-0.4 end
elseif nivel_actual==5 then
if b[z].d==1 then b[z].x=b[z].x+0.25 end
if b[z].d==2 then b[z].x=b[z].x-0.25 end
end

if b[z].d>0 then b[z].y=b[z].y+gravedad/4 end
if b[z].d>2 then b[z].d=1 end

if b[z].x>466 then b[z].d=2 end 
if b[z].x<0 then b[z].d=1 end


if col(b[z],sup[i]) then b[z].y=b[z].y-gravedad*2 b[z].t:reset() 
if b[z].c!=i then b[z].c=i b[z].d=b[z].d+1 end
end

if not col(b[z],sup[i]) and b[z].t:time()>200 then b[z].i=bar[5] end



end


if controls.press("cross") and col(jump,sup[i]) and col(jump,inf[i])==false and col(jump,der[i])==false and col(jump,izq[i])==false then 
movimiento_d:reset() movimiento_d:stop() jump.t:start()
if salto:playing()==false then salto:play(2) end
flying=true
end
if controls.left() and col(jump,sup[i]) and jump.t:time()<10 then 
jump.x=jump.x-2 jump.d="izq" movimiento_d:start() 
if caminar:playing()==false then caminar:play(1) end end

if controls.right() and col(jump,sup[i]) and jump.t:time()<10 then 
jump.x=jump.x+2 jump.d="der" movimiento_d:start() 
if caminar:playing()==false then caminar:play(1) end end


if col(jump,sup[i]) then jump.y=jump.y-gravedad end



if col(jump,inf[i]) and jump.d!="aba" then jump.y=jump.y+3 jump.t:reset() jump.t:stop() end
end

for i=1,#e do
if controls.up() and col(jump,e[i]) and jump.t:time()<10 then jump.y=jump.y-1.8 jump.d="aba" movimiento_d:start() end
if controls.down() and col(jump,e[i]) and jump.t:time()<10 then jump.y=jump.y+1 jump.d="aba" movimiento_d:start() end
end

d.sprites_dk:blit(d.x,d.y)
municion:blit(d.x-30,d.y-3)

for i=1,30 do
b[i].i:blit(b[i].x,b[i].y)
end

if jump.t:time()<10 then jump.y=jump.y+gravedad end 

if jump.t:time()>10 and jump.t:time()<2000 then 
if jump.d=="der" then jump.i=sprites_dk[10] end
if jump.d=="izq" then jump.i=sprites_dk[11] end
if jump.d=="" then
if jump.d2=="der" then jump.i=sprites_dk[10] end
if jump.d2=="izq" then jump.i=sprites_dk[11] end
end
movimiento_d:reset()
movimiento_d:stop()
end

if flying==false and jump.t:time()<10 and not controls.up() and not controls.down() and not controls.left() and not controls.right() then  
if jump.d=="der" then jump.i=sprites_dk[1] jump.d2="der" jump.d="" end
if jump.d=="izq" then jump.i=sprites_dk[4] jump.d2="izq" jump.d="" end
if jump.d=="aba" then jump.i=sprites_dk[8] end
end


for i=1,#sup do
if col(jump,sup[i]) then flying=false end
if flying then
if jump.d=="der" then jump.i=sprites_dk[10] end
if jump.d=="izq" then jump.i=sprites_dk[11] end
end
end


if col(jump,d) and nivel_actual==5 then 
ganas:play(3)
score=300-math.floor(tiempo_juego:time()/1000)
os.message("Has ganado. Puntuación: "..score)
nivel_actual=1
end

animacion_donkey2()
saltar()
caminando()
barriles()
if nivel_actual!=5 then rescate() end
jump.i:blit(jump.x,jump.y)

if controls.press("start") then sistema() end
screen.flip()
end
end
