background = Image.load("background.jpg")
battery1 = Image.load("battery0.jpg")
battery2 = Image.load("battery1.jpg")
battery3 = Image.load("battery2.jpg")
battery4 = Image.load("battery3.jpg")
rouge=Color.new(255,0,0)
blanc = Color.new(255,255,255)
 
balle = Image.createEmpty(3,3)
balle:clear(rouge)
 
oldpad = Controls.read()
 
p1currentBullet = 0
statut = "droite"
p1positionY = 40
p1positionX = 30
x = 0
y = 0
p1barredevie = {}
p1barredevie.y = 10
p1barredevie.vie = 100 
 
p2currentBullet = 0
statut = "droite"
p2positionY = 70
p2positionX = 400
p2x = 0
p2y = 0
p2barredevie ={}
p2barredevie.y = 10
p2barredevie.vie = 100
 
vaisseauhauteur = 45
vaisseaulargeur = 61
 
p1infotire = {}
for a = 1,20 do
p1infotire[a] = { pic = balle , firing = false, statut = "droite", x = x + 32,y =y + 16 }
end
 
p2infotire = {}
for a = 1,20 do
p2infotire[a] = { pic = balle , firing = false, statut = "droite", x = p2x - 32,y =p2y + 16 }
end
 
function p1bulletSetup()
if p1currentBullet < 20 then
p1currentBullet = p1currentBullet + 1
else
p1currentBullet = 1
end
if statut == "gauche" then
p1infotire[p1currentBullet].x = p1positionX
p1infotire[p1currentBullet].y = p1positionY + 16
end
if statut == "droite" then
p1infotire[p1currentBullet].x = p1positionX + 32
p1infotire[p1currentBullet].y = p1positionY + 16
end
if statut == "haut" then
p1infotire[p1currentBullet].x = p1positionX + 16
p1infotire[p1currentBullet].y = p1positionY
end
if statut == "bas" then
p1infotire[p1currentBullet].x = p1positionX + 16
p1infotire[p1currentBullet].y = p1poxitionY + 32
end
 
p1infotire[p1currentBullet].statut = statut
p1infotire[p1currentBullet].firing = true
end
 
function p2bulletSetup()
if p2currentBullet < 20 then
p2currentBullet = p2currentBullet + 1
else
p2currentBullet = 1
end
if statut == "gauche" then
p2infotire[p2currentBullet].x = p2positionX
p2infotire[p2currentBullet].y = p2positionY + 16
end
if statut == "droite" then
p2infotire[p2currentBullet].x = p2positionX - 32
p2infotire[p2currentBullet].y = p2positionY + 16
end
if statut == "haut" then
p2infotire[p2currentBullet].x = p2positionX - 16
p2infotire[p2currentBullet].y = p2positionY
end
if statut == "bas" then
p2infotire[p2currentBullet].x = p2positionX - 16
p2infotire[p2currentBullet].y = p2poxitionY + 32
end
 
p2infotire[p2currentBullet].statut = statut
p2infotire[p2currentBullet].firing = true
end
 
function p1bulletFire()
for i = 1,20 do
if p1infotire[i].firing == true then
if p1infotire[i].statut == "droite" then p1infotire[i].x = p1infotire[i].x + 10 end
if p1infotire[i].statut == "gauche" then p1infotire[i].x = p1infotire[i].x - 10 end
if p1infotire[i].statut == "haut" then p1infotire[i].y = p1infotire[i].y - 10 end
if p1infotire[i].statut == "bas" then p1infotire[i].y = p1infotire[i].y + 10 end
screen:blit(p1infotire[i].x,p1infotire[i].y,p1infotire[i].pic)
end
if p1infotire[i].x < 0 or p1infotire[i].x > 480 or p1infotire[i].y < 0 or p1infotire[i].y > 272 then p1infotire[i].firing = false end
end
end
 
function p2bulletFire()
for i = 1,20 do
if p2infotire[i].firing == true then
if p2infotire[i].statut == "droite" then p2infotire[i].x = p2infotire[i].x - 10 end
if p2infotire[i].statut == "gauche" then p2infotire[i].x = p2infotire[i].x + 10 end
if p2infotire[i].statut == "haut" then p2infotire[i].y = p2infotire[i].y + 10 end
if p2infotire[i].statut == "bas" then p2infotire[i].y = p2infotire[i].y - 10 end
screen:blit(p2infotire[i].x,p2infotire[i].y,p2infotire[i].pic)
end
if p2infotire[i].x < 0 or p2infotire[i].x > 480 or p2infotire[i].y < 0 or p2infotire[i].y > 272 then p2infotire[i].firing = false end
end
end

function collisionp1ballevaisseau()
for i = 1,20 do
if p1infotire[i].firing == true then
if (p2positionX + vaisseaulargeur > p1infotire[i].x) and (p2positionX < p1infotire[i].x +3)
and (p2positionY + vaisseauhauteur > p1infotire[i].y) and (p2positionY < p1infotire[i].y + 3) 
then 
p2barredevie.vie = p2barredevie.vie - 2
p1infotire[i].firing = false
end  
end
end
end

function collisionp2ballevaisseau()
for i = 1,20 do
if p2infotire[i].firing == true then
if (p1positionX + vaisseaulargeur > p2infotire[i].x) and (p1positionX < p2infotire[i].x +3)
and (p1positionY + vaisseauhauteur > p2infotire[i].y) and (p1positionY < p2infotire[i].y + 3) 
then 
p1barredevie.vie = p1barredevie.vie - 2
p2infotire[i].firing = false
end  
end
end
end
 
function p1deplacementvaisseau()
pad = Controls.read()
if pad:left() then
statut = "droite"
x = x+5
if p1positionX>10 then
p1positionX=p1positionX-5
end end
 
if pad:right() then
statut = "droite"
x =x-5
if p1positionX<179 then
p1positionX=p1positionX+5
end end
 
if pad:up() then
statut = "droite"
y = y+5
if p1positionY>10 then
p1positionY=p1positionY-5
end end
 
if pad:down() then
statut = "droite"
y = y-5
if p1positionY<255 then
p1positionY=p1positionY+5
end end
end
 
function p2deplacementvaisseau()
pad = Controls.read()
if pad:square() then
statut = "droite"
p2x = p2x+5
if p2positionX>301 then
p2positionX=p2positionX-5 
end end
 
if pad:circle() then
statut = "droite"
p2x =p2x-5
if p2positionX<470 then
p2positionX=p2positionX+5
end end
 
if pad:triangle() then
statut = "droite"
p2y = p2y+5
if p2positionY>10 then
p2positionY=p2positionY-5
end end
 
if pad:cross() then
statut = "droite"
p2y = p2y-5
if p2positionY<255 then
p2positionY=p2positionY+5
end end
end
 
while true do
pad = Controls.read()
screen:clear()
screen:blit(0, 0, background)
 
p2deplacementvaisseau()
collisionp2ballevaisseau()
p1deplacementvaisseau()
collisionp1ballevaisseau()
 
if pad:l() and oldpad:l() ~= pad:l() then
p1bulletSetup()
end
 
if pad:r() and oldpad:r() ~= pad:r() then
p2bulletSetup()
end
 
p1bulletFire()
 
p2bulletFire()
 
if choixvaisseaup1 == 1 then
vaisseaup1 = Image.load("vaisseau.jpg")
end
 
if choixvaisseaup1 == 2 then
vaisseaup1 = Image.load("vaisseau2.jpg")
end
 
if choixvaisseaup1 == 3 then
vaisseaup1 = Image.load("vaisseau3.jpg")
end

if choixvaisseaup2 == 1 then
vaisseaup2 = Image.load("vaisseaub.jpg")
end
 
if choixvaisseaup2 == 2 then
vaisseaup2 = Image.load("vaisseau2b.jpg")
end
 
if choixvaisseaup2 == 3 then
vaisseaup2 = Image.load("vaisseau3b.jpg")
end

if p1barredevie.vie == 0 then
dofile("gagnerp2.lua")
end
 
if p2barredevie.vie == 0 then
dofile("gagnerp1.lua")
end

screen:blit(p1positionX,p1positionY,vaisseaup1)
screen:blit(p2positionX,p2positionY,vaisseaup2)
screen:fillRect(60,5,p1barredevie.vie,p1barredevie.y,rouge)
screen:fillRect(390,5,p2barredevie.vie,p2barredevie.y,rouge)
screen:print(70,6,p1barredevie.vie,blanc)
screen:print(400,6,p2barredevie.vie,blanc)
 
if System.powerGetBatteryLifePercent() >= 0 then batteryL = battery1 end
if System.powerGetBatteryLifePercent() > 25 then batteryL = battery2 end
if System.powerGetBatteryLifePercent() > 50 then batteryL = battery3 end
if System.powerGetBatteryLifePercent() > 75 then batteryL = battery4 end
 
screen:blit(5,5,batteryL)
 
screen.waitVblankStart()
screen.flip()
oldpad = pad
end
