-Doom
Code: Select all
require 'menu'
function love.load()
bg = love.graphics.newImage("bg.png")
menubg = love.graphics.newImage('menuback.png')
tank = love.graphics.newImage("tank.png")
troll = love.graphics.newImage("invader.png")
zap = love.graphics.newImage("laser.png")
zap2 = love.graphics.newImage("laser2.png")
scorefont = love.graphics.newFont('score.ttf', 12)
menuFont = love.graphics.newFont('score.ttf', 25)
bmusic = love.audio.newSource("music.mp3")
bmusic:setVolume(0.5)
love.audio.play(bmusic)
gamestate = "menu"
--button
button_load(20, 10, 'Start', 'start')
button_load(20, 30, 'Quit', 'quit')
button_load(300, 300, 'Menu', 'menu')
ecount = 0
wavecount = 1
wavespeed = 5
health = 100
playhp = 100
maxhp = 100
score = 0
text = 0
hero = {} -- new table for the hero
hero.x = 300 -- x,y coordinates of the hero
hero.y = 370
hero.width = 30
hero.height = 15
hero.speed = 150
hero.shots = {} -- holds our fired shots
enemyshots = {}
movement = "mode1"
mx = 0
enemies = {}
ii = 0
for i=0,11 do
enemy = {}
enemy.width = 40
enemy.height = 20
if i > 5 then
enemy.x = ii * (enemy.width + 60) + 100
enemy.y = enemy.height + 200
ii = ii + 1
else
enemy.x = i * (enemy.width + 60) + 100
enemy.y = enemy.height + 100
end
table.insert(enemies, enemy)
end
end
function nextWave()
wavecount = wavecount + 1
wavespeed = wavespeed + (math.random(1,10)/10)
ii = 0
for i=0,11 do
enemy = {}
enemy.width = 40
enemy.height = 20
if i > 5 then
enemy.x = ii * (enemy.width + 60) + 100
enemy.y = enemy.height + 200
ii = ii + 1
else
enemy.x = i * (enemy.width + 60) + 100
enemy.y = enemy.height + 100
end
table.insert(enemies, enemy)
end
end
function love.keyreleased(key)
if (key == " ") then
shoot()
end
end
function love.update(dt)
mx = mx + 1
local remEnemy = {}
local remShot = {}
local remEShot = {}
if gamestate == "playing" then
if playhp == 0 then
gamestate = "defeat"
end
-- keyboard actions for player
if love.keyboard.isDown("left") or love.keyboard.isDown("a") then
hero.x = hero.x - hero.speed*dt
elseif love.keyboard.isDown("right") or love.keyboard.isDown("d") then
hero.x = hero.x + hero.speed*dt
elseif love.keyboard.isDown("`") then
love.graphics.toggleFullscreen( )
end
ecount = #enemies
if #enemies < 1 then
nextWave()
end
for e,s in ipairs(enemies) do
shotchance = math.random(1,1000)
if shotchance == 152 then
enemyshoot(s)
end
end
-- update the shots
for i,v in ipairs(enemyshots) do
-- move them
v.y = v.y + dt * 200
-- mark shots that are not visible for removal
if v.y < 0 then
table.insert(remEShot, i)
end
-- check for collision with player
if CheckCollision(v.x,v.y,5,4,hero.x+10,hero.y+50,hero.width+10,hero.height+10) then
playhp = playhp - 10
-- mark the shot to be removed
table.insert(remEShot, i)
end
end
for i,v in ipairs(hero.shots) do
-- move them up up up
v.y = v.y - dt * 200
-- mark shots that are not visible for removal
if v.y < 0 then
table.insert(remShot, i)
end
-- check for collision with enemies
for ii,vv in ipairs(enemies) do
if CheckCollision(v.x,v.y,2,5,vv.x,vv.y,vv.width,vv.height) then
score = score + 100
-- mark that enemy for removal
table.insert(remEnemy, ii)
-- mark the shot to be removed
table.insert(remShot, i)
end
end
end
end
-- remove the marked enemies
for i,v in ipairs(remEnemy) do
table.remove(enemies, v)
end
for i,v in ipairs(remShot) do
table.remove(hero.shots, v)
end
for i,v in ipairs(remEShot) do
table.remove(enemyshots, v)
end
if mx < 100 then
movement = "mode1"
elseif mx > 100 and mx < 200 then
movement = "mode2"
elseif mx == 200 then
mx = 0
movement = "mode1"
end
-- update enemies
if gamestate == "playing" then
for i,v in ipairs(enemies) do
-- let them fall down slowly
v.y = v.y + dt*wavespeed
if movement == "mode1" then
v.x = v.x - .1
elseif movement =="mode2" then
v.x = v.x + .1
end
-- check for collision with ground
if v.y > 465 then
table.insert(remEnemy, v)
if playhp > 0 then
playhp = playhp - 10
end
end
end
if playhp == 0 then
gamestate = "defeat"
end
end
end
function love.draw()
if gamestate == "playing" or gamestate == "defeat" then
love.graphics.setIcon(troll)
--draw a background
love.graphics.setColor(255,255,255,255)
love.graphics.draw(bg,5,0,0,1)
--draw some ground
love.graphics.setColor(0,255,0,255)
love.graphics.rectangle("fill", 0, 465, 800, 150)
--draw our hero
love.graphics.setColor(255,255,0,255)
love.graphics.draw(tank, hero.x, hero.y, 0, 3)
--hero hp bar
love.graphics.setColor(255,0,0)
if playhp > 0 then
love.graphics.rectangle("fill", hero.x, hero.y + 100, playhp/maxhp*100, 5)
end
--draw our heros shots
love.graphics.setColor(255,255,255,255)
for i,v in ipairs(hero.shots) do
love.graphics.draw(zap, v.x, v.y, 0, 2)
end
love.graphics.setColor(255,255,255,255)
for i,v in ipairs(enemyshots) do
love.graphics.draw(zap2, v.x-25, v.y-25, 0, 2)
end
--draw our enemies
love.graphics.setColor(0,255,255,255)
for i,v in ipairs(enemies) do
love.graphics.draw(troll, v.x, v.y, 0, 2)
end
--drawing text info
love.graphics.setFont(scorefont)
love.graphics.setColor(0,255,255,255)
love.graphics.print("Enemies: ["..ecount.."]", 10, 50, 0, 1, 1)
love.graphics.print("Score: ["..score.."]", 10, 75, 0, 1, 1)
love.graphics.print("Wave: ["..wavecount.."]", 10, 100, 0, 1, 1)
love.graphics.print("Health: ["..playhp.."]", 10, 125, 0, 1, 1)
if gamestate == "defeat" then
button_draw()
end
elseif gamestate == "menu" then
love.graphics.setColor(0,255,0,255)
love.graphics.draw(menubg,0,0,0,1)
button_draw()
elseif gamestate == "defeat" then
love.graphics.setColor(0,255,0)
button_draw()
end
end
function love.mousepressed(x, y)
if gamestate == "menu" or gamestate == "defeat" then
button_click(x, y)
end
end
function shoot() --players weapon
local shot = {}
shot.x = hero.x+hero.width*1
shot.y = hero.y+80
table.insert(hero.shots, shot)
end
function enemyshoot(gah) --invaders weapon
local eshot = {}
eshot.x = gah.x+gah.width*1
eshot.y = gah.y+80
table.insert(enemyshots, eshot)
end
-- Collision detection function.
-- Checks if a and b overlap.
-- w and h mean width and height.
function CheckCollision(ax1,ay1,aw,ah, bx1,by1,bw,bh)
local ax2,ay2,bx2,by2 = ax1 + aw, ay1 + ah, bx1 + bw, by1 + bh
return ax1 < bx2 and ax2 > bx1 and ay1 < by2 and ay2 > by1
end
[url][/url]