green=Color.new(0,255,0)
white = Color.new(255,255,255)
blue = Color.new(0,0,255)
red = Color.new(255,0,0)
playerimg = Image.load("images/player1.png")
player2img = Image.load("images/player2.png")
bg = Image.load("images/gras.png")
h1 = Image.load("images/house1.png")
h2 = Image.load("images/house2.png")
road = Image.load("images/road.png")
lake1 = Image.load("images/lake1.png")
lake2 = Image.load("images/lake2.png")
bridge = Image.load("images/bridge.png")
hole = Image.load("images/hole.png")
shoot = Sound.load("sound/shoot.wav", false)
bu = Sound.load("sound/bu.wav", false)
explode = Image.load("images/explode.png")
--Create a blank image and fill it with green. This will be our bullet.
bullet = Image.createEmpty(4,4)
bullet:clear(blue)
bulletWidth = 4
bulletHeight = 4
--Variables
oldpad = Controls.read()
currentBullet = 0
direction = "right"
--player array---
healthbar = {}
healthbar.y = 10
healthbar2 = {}
healthbar2.y = 10

Player = { x = 30, y = 100 }
player = {}
player.health = 100

Player2 = { x = 400, y = 100 }
player2 = {}
player2.health = 100

--Create Array for Bullets
BulletInfo = {}
for a = 1,5 do
BulletInfo[a] = { pic = bullet , firing = false, direction = "right", x = Player.x + 32, 
y = Player.y + 16 }
end
BulletInfo2 = {}
for a = 1,5 do
BulletInfo2[a] = { pic = bullet , firing = false, direction2 = "right", x = Player2.x + 32, 
y = Player2.y + 16 }
end
--bullet Functions p1----

function bulletSetup()
--Increase the current bullet by one, or reset it to 1
if currentBullet < 5 then
currentBullet = currentBullet + 1
else
currentBullet = 1
end
if direction == "left" then 
BulletInfo[currentBullet].x = Player.x 
BulletInfo[currentBullet].y = Player.y + 16
end
if direction == "right" then 
BulletInfo[currentBullet].x = Player.x + 32 
BulletInfo[currentBullet].y = Player.y + 16 
end
if direction == "up" then 
BulletInfo[currentBullet].x = Player.x + 16 
BulletInfo[currentBullet].y = Player.y 
end
if direction == "down" then 
BulletInfo[currentBullet].x = Player.x + 16
BulletInfo[currentBullet].y = Player.y + 32 
end

BulletInfo[currentBullet].direction = direction
BulletInfo[currentBullet].firing = true
end
--end bullet func p1-----
--bullet Functions p2----

function bulletSetup2()
--Increase the current bullet by one, or reset it to 1
if currentBullet < 5 then
currentBullet = currentBullet + 1
else
currentBullet = 1
end
if direction == "left" then 
BulletInfo2[currentBullet].x = Player2.x 
BulletInfo2[currentBullet].y = Player2.y + 16
end
if direction == "right" then 
BulletInfo2[currentBullet].x = Player2.x + 32 
BulletInfo2[currentBullet].y = Player2.y + 16 
end
if direction == "up" then 
BulletInfo2[currentBullet].x = Player2.x + 16 
BulletInfo2[currentBullet].y = Player2.y 
end
if direction == "down" then 
BulletInfo2[currentBullet].x = Player2.x + 16
BulletInfo2[currentBullet].y = Player2.y + 32 
end

BulletInfo2[currentBullet].direction = direction2
BulletInfo2[currentBullet].firing = true
end
--buletfire
function bulletFire()
for i = 1,5 do
if BulletInfo[i].firing == true then
if BulletInfo[i].direction == "right" then BulletInfo[i].x = BulletInfo[i].x + 10 end
if BulletInfo[i].direction == "left" then BulletInfo[i].x = BulletInfo[i].x - 10 end
if BulletInfo[i].direction == "up" then BulletInfo[i].y = BulletInfo[i].y - 10 end
if BulletInfo[i].direction == "down" then BulletInfo[i].y = BulletInfo[i].y + 10 end
screen:blit(BulletInfo[i].x,BulletInfo[i].y,BulletInfo[i].pic)
end
if BulletInfo[i].x < 0 or BulletInfo[i].x > 480 or BulletInfo[i].y < 0 or BulletInfo[i].y > 272 then BulletInfo[i].firing = false end
end
end
---p2--
function bulletFire2()
for i = 1,5 do
if BulletInfo2[i].firing == true then
if BulletInfo2[i].direction == "right" then BulletInfo2[i].x = BulletInfo2[i].x + 10 end
if BulletInfo2[i].direction == "left" then BulletInfo2[i].x = BulletInfo2[i].x - 10 end
if BulletInfo2[i].direction == "up" then BulletInfo2[i].y = BulletInfo2[i].y - 10 end
if BulletInfo2[i].direction == "down" then BulletInfo2[i].y = BulletInfo2[i].y + 10 end
screen:blit(BulletInfo2[i].x,BulletInfo2[i].y,BulletInfo2[i].pic)
end
if BulletInfo2[i].x < 0 or BulletInfo2[i].x > 480 or BulletInfo2[i].y < 0 or BulletInfo2[i].y > 272 then BulletInfo2[i].firing = false end
end
end

--end bullet fire--


block = Image.createEmpty(81,32)
block:clear(green)
PlayerHeight = 32
PlayerWidth = 32
Player2Height = 32
Player2Width = 32
blockHeight = 32
blockWidth = 81
Block = {}
Block[1] = { x = 100, y = 95, height = block:height(), width = block:width() }
Block[2] = { x = 300, y = 95, height = block:height(), width = block:width() }
Block[3] = { x = 200, y = 230, height = block:height(), width = block:width() }
Block[4] = { x = 200, y = 165, height = block:height(), width = block:width() }
function movePlayer()
pad = Controls.read()
---player one move----
if pad:left() then 
Player.x = Player.x - 2 
direction="left"
end
if pad:right() then 
Player.x = Player.x + 2 
direction="right"
end
if pad:up() then 
Player.y = Player.y - 2 
direction="up"
end
if pad:down() then 
Player.y = Player.y + 2 
direction="down"
end
----player2 move---
if pad:square() then 
Player2.x = Player2.x - 2
direction2="left" 
end
if pad:circle() then 
Player2.x = Player2.x + 2
direction2="right" 
end
if pad:triangle() then 
Player2.y = Player2.y - 2
direction2="up" 
end
if pad:cross() then 
Player2.y = Player2.y + 2
direction2="down" 
end
end
function collisionCheck(object)
for a = 1,5 do
if (Player.x + PlayerWidth > object.x) and (Player.x < object.x + object.width) and (Player.y + PlayerHeight > object.y) and (Player.y < object.y + object.height) then
Player.x = oldx
Player.y = oldy
end 
if (Player2.x + Player2Width > object.x) and (Player2.x < object.x + object.width) and (Player2.y + Player2Height > object.y) and (Player2.y < object.y + object.height) then
Player2.x = oldx2
Player2.y = oldy2
end
end
function ColCheck()
if player.health > 0 and (Player.x + PlayerWidth > BulletInfo2[1].x) and (Player.x < BulletInfo2[1].x) and (Player.y + PlayerHeight > BulletInfo2[1].y) and (Player.y < BulletInfo2[1].y) then
Player.x = oldx
Player.y = oldy
player.health = player.health - 3.0 end
end
if player.health < 1 then 
playerimg = explode
dofile("./player1.lua")
end
if player2.health < 1 then 
player2img = explode
dofile("./player2.lua")
end
end

function ColCheck2()
if player2.health > 0 and (Player2.x + Player2Width > BulletInfo[1].x) and (Player2.x < BulletInfo[1].x) and (Player2.y + Player2Height > BulletInfo[1].y) and (Player2.y < BulletInfo[1].y) then
Player2.x = oldx2
Player2.y = oldy2
player2.health = player2.health - 3.0 end
end
if player.health < 2 then 
playerimg = explode
end
if player2.health < 2 then 
player2img = explode
end


while true do
pad = Controls.read()
-- store player's position at beginning of each loop
--p1---
oldx = Player.x
oldy = Player.y
screen:clear()
--p2----
oldx2 = Player2.x
oldy2 = Player2.y
screen:clear()
movePlayer()
---bullet fire---
if pad:l() and oldpad:l() ~= pad:l() then
bulletSetup()
shoot:play()
end
if pad:r() and oldpad:r() ~= pad:r() then
bulletSetup2()
shoot:play()
end



--check collision for each block
collisionCheck(Block[1])
collisionCheck(Block[2])
collisionCheck(Block[3])
collisionCheck(Block[4])


--paste player to screen
screen:blit(0,0,bg, true)
screen:blit(0,126,road, true)
screen:blit(10,20,hole, true)
screen:blit(200,40,hole, true)
screen:blit(460,160,hole, true)
screen:blit(120,210,hole, true)
screen:blit(20,225,hole, true)
screen:blit(330,70,hole, true)
screen:blit(200,197,bridge, true)
screen:blit(Player.x,Player.y,playerimg)
screen:blit(Player2.x,Player2.y,player2img)
screen:fillRect(10,10,player.health,healthbar.y,red)
screen:print(10,30,player.health,white)

--paste all 3 blocks to screen
for a = 1,4 do
screen:blit(Block[a].x,Block[a].y,block, false)
end
screen:blit(100,23,h1, true)
screen:blit(300,8,h2, true)
screen:fillRect(360,10,player2.health,healthbar2.y,green)
screen:print(360,30,player2.health,white)
screen:blit(200,230,lake1, true)
screen:blit(200,165,lake2, true)
ColCheck()
ColCheck2()
bulletFire()
bulletFire2()
screen.waitVblankStart()
screen.flip()
oldpad = pad
end 
