Sound.loadBank("lua/son/soundbank.bin")
-- Load all mods and sfx in ram
Sound.loadMod(4)
Sound.startMod(4, PLAY_ONCE)

img = Image.load("lua/images/Sol DS.PNG", 0, 0, VRAM)

-- Personnage 1
score = 0
player = {}
player.y = 110
player.x = 0
playerWidth = 32
playerHeight = 39



spr = Sprite.new("lua/images/agumon run2.PNG", 27, 32, RAM)

spr:addAnimation({0,1,2,1}, 300) -- Walk up
spr:addAnimation({3,4,5,4}, 300) -- Walk right

direction = 1

rings = Sprite.new("lua/images/Egg.PNG", 17, 20, RAM)

rings:addAnimation({0,1,2,1}, 300) -- Walk up

direction2 = 1


rings.x = 100
rings.y = 0
rings.height = 16
rings.width = 16

score = 0

minuteur = Timer.new()
minuteur:start()

function movePlayer()
-----------------Contrôles-----------------
	if Keys.held.Right then 
	direction = 1
	player.x = player.x + 3
	end

	if Keys.held.Left then 
	direction = 2
	player.x = player.x - 3
	end
	
	end

function collisionCheck(rings)
if (player.x + playerWidth > rings.x) and (player.x < rings.x + rings.width) and (player.y + playerHeight > rings.y) and (player.y < rings.y + rings.height) then
score = score + 1
player.x = oldx
player.y = oldy
rings.x = math.random(0, 256)
rings.y = 0
end
end


while true do
Controls.read()
startDrawing()

currentTime = minuteur:time()


rings.y = rings.y + 3

if rings.y > 192 then
rings.x = math.random(0, 256)
rings.y = 0
end
-- Dégradé
screen.drawFillRect(SCREEN_UP,256,192,0,0,Color.new(8,8,24))
screen.drawFillRect(SCREEN_DOWN,256,192,0,0,Color.new(8,8,24))
screen.print (SCREEN_UP, 0, 0, "Ramasse 8 Digitamamon en 10 sec !")

screen.blit(SCREEN_DOWN, 0, 128, img)
rings:playAnimation(SCREEN_DOWN, rings.x, rings.y, direction2)
spr:playAnimation(SCREEN_DOWN, player.x, player.y, direction)

oldx = player.x
oldy = player.y
movePlayer()
--check collision for each block
collisionCheck(rings)
	
if currentTime > 10000 then
Image.destroy(img)
img = nil
rings.x = nil
rings.y = nil
player.x = nil
player.y = nil
oldx = nil
oldy = nil
score = nil
playerWidth = nil
playerHeight = nil
rings.height = nil
rings.width = nil
score = nil
direction = nil
direction2 = nil
currentTime = nil
minuteur = nil
Sound.unloadMod(4)
Sound.unloadBank()
dofile("lua/Lose.lua")
end


if score > 7 then
Image.destroy(img)
img = nil
rings.x = nil
rings.y = nil
player.x = nil
player.y = nil
oldx = nil
oldy = nil
score = nil
playerWidth = nil
playerHeight = nil
rings.height = nil
rings.width = nil
score = nil
direction = nil
direction2 = nil
currentTime = nil
minuteur = nil
Sound.unloadMod(4)
Sound.unloadBank()

dofile("lua/win.lua")

end


stopDrawing()
end 
Image.destroy(img)
img = nil
rings:destroy()
rings = nil
spr:destroy()
spr = nil
rings.x = nil
rings.y = nil
player.x = nil
player.y = nil
oldx = nil
oldy = nil
score = nil
playerWidth = nil
playerHeight = nil
rings.height = nil
rings.width = nil
score = nil
direction = nil
direction2 = nil
currentTime = nil
minuteur = nil
Sound.unloadMod(4)
Sound.unloadBank()

