Code: Select all
function load()
player = love.graphics.newImage("player.PNG")
crosshair = love.graphics.newImage("crosshair.PNG")
enemy = love.graphics.newImage("enemy.PNG")
gunsound = love.audio.newSound("shotgun.wav")
message1 = "Zombie Apocalypse"
message2 = "Press any key to start."
message3 = "You Lose"
message4 = "Score:"
message5 = "Press any key to continue."
Enemies = 0
EX = {}
EY = {}
EA = {}
EHealth = {}
bulletangles = {}
gunpelletsX = {}
gunpelletsY = {}
pt1 = {}
pt2 = {}
ptc = {}
PX = 400
PY = 300
PHealth = 100
PAngle = 0
lastfire = 2
difficulty = 2
diffup = 0
score = 0
nextenemy = 0
screen = 1
math.randomseed(os.time())
end
function draw()
if screen == 1 then
love.graphics.draw(message1,350,200)
love.graphics.draw(message2,325,300)
end
if screen == 2 then
if lastfire<0.05 then
for a=1,6,1 do
love.graphics.line(PX,PY,gunpelletsX[a],gunpelletsY[a])
end
end
love.graphics.draw(player,PX,PY,PAngle)
for a=1,Enemies,1 do
love.graphics.draw(enemy,EX[a],EY[a],EA[a])
end
love.graphics.draw(crosshair,MX,MY)
love.graphics.setColor(255,0,0)
love.graphics.setLineWidth(50)
love.graphics.line(775,600,775,600-PHealth*6)
love.graphics.setColor(255,255,255)
love.graphics.setLineWidth(1)
end
if screen == 3 then
love.graphics.draw(message3,375,200)
love.graphics.draw(message4..score,350,300)
love.graphics.draw(message5,325,400)
end
end
function update(dt)
if screen == 2 then
diffup = dt + diffup
if diffup == 30 then
diffup = diffup - 10
difficulty = difficulty^2
end
nextenemy = dt + nextenemy
if nextenemy >= 1/difficulty then
nextenemy = nextenemy - 1/difficulty
randangle = math.random(1,360)
enemycreate(math.sin(math.rad(randangle))*800+400,math.cos(math.rad(randangle))*600+300)
end
for a=1,Enemies,1 do
if math.sqrt((EX[a]-PX)^2+(EY[a]-PY)^2) < 20 then
PHealth = PHealth - 10*dt
end
end
if PHealth <=0 then
reset()
screen = 3
end
if love.keyboard.isDown(love.key_w) then
PY = PY-100*dt
if PY < 0 then
PY = 0
end
elseif love.keyboard.isDown(love.key_s) then
PY = PY+100*dt
if PY > 600 then
PY = 600
end
end
if love.keyboard.isDown(love.key_a) then
PX = PX-100*dt
if PX < 0 then
PX = 0
end
elseif love.keyboard.isDown(love.key_d) then
PX = PX+100*dt
if PX > 800 then
PX = 800
end
end
if love.mouse.isDown(love.mouse_left) then
if lastfire>1 then
love.audio.play(gunsound)
lastfire = 0
for a=1,6,1 do
bulletangles[a] = -PAngle+math.random(-10,10)
gunpelletsX[a] = PX-1000*math.sin(math.rad(bulletangles[a]))
gunpelletsY[a] = PY-1000*math.cos(math.rad(bulletangles[a]))
collision(PX,PY,gunpelletsX[a],gunpelletsY[a],a)
end
end
end
MX = love.mouse.getX()
MY = love.mouse.getY()
PAngle = math.deg(math.atan2(MY-PY,MX-PX))+90
for a=1,Enemies,1 do
EA[a] = math.deg(math.atan2(PY-EY[a],PX-EX[a]))+90
EX[a] = EX[a]-70*math.sin(math.rad(-EA[a]))*dt
EY[a] = EY[a]-70*math.cos(math.rad(-EA[a]))*dt
end
lastfire = lastfire+dt
for a=1,Enemies,1 do
if a <= Enemies then
if EHealth[a]<=0 then
enemykill(a)
end
end
end
end
end
function keypressed(key)
if screen == 1 then
if key > 0 then
screen = 2
end
end
if screen == 3 then
if key > 0 then
screen = 1
end
end
end
function collision( xplayer, yplayer, xbullet, ybullet, bullet )
for a=1,Enemies,1 do
pt1.x = xplayer
pt1.y = yplayer
pt2.x = xbullet
pt2.y = ybullet
ptc.x = EX[a]
ptc.y = EY[a]
if LineIntersectCircle(pt1, pt2, ptc, 30) then
EHealth[a] = EHealth[a]-20
end
end
end
function distance(x1, y1, x2, y2)
return math.sqrt((x1 - x2)^2 + (y1 - y2)^2);
end;
function rotate(x, y, a)
local sina, cosa = math.sin(a), math.cos(a);
return x * cosa - y * sina, x * sina + y * cosa;
end;
function LineIntersectCircle(p1, p2, pc, r)
local tp1, tp2 = { x = p1.x - pc.x, y = p1.y - pc.y }, { x = p2.x - pc.x, y = p2.y - pc.y };
if distance(tp1.x, tp1.y, 0, 0) < r then return true; end;
if distance(tp2.x, tp2.y, 0, 0) < r then return true; end;
local lineangle = math.atan2(p2.y - p1.y, p2.x - p1.x);
local rp1 = {}; rp1.x, rp1.y = rotate(tp1.x, tp1.y, -lineangle);
local rp2 = {}; rp2.x, rp2.y = rotate(tp2.x, tp2.y, -lineangle);
assert(math.abs(rp1.y - rp2.y) <= math.abs(rp1.y / 10000));
if math.abs(rp1.y) > r then return false; end;
if rp1.x * rp2.x > 0 then return false; end;
return true;
end;
function enemycreate(X,Y)
Enemies = Enemies+1
EX[Enemies] = X
EY[Enemies] = Y
EA[Enemies] = 0
EHealth[Enemies] = 100
end
function enemykill(E)
if Enemies>0 then
table.remove(EX,E)
table.remove(EY,E)
table.remove(EA,E)
table.remove(EHealth,E)
Enemies = Enemies-1
score = score + 1
end
end
function reset()
Enemies = 0
EX = {}
EY = {}
EA = {}
EHealth = {}
bulletangles = {}
gunpelletsX = {}
gunpelletsY = {}
pt1 = {}
pt2 = {}
ptc = {}
PX = 400
PY = 300
PHealth = 100
PAngle = 0
lastfire = 2
difficulty = 2
diffup = 0
score = 0
nextenemy = 0
end