Code: Select all
local anim8 = require 'anim8'
local enemy = require "enemy"
local char = require "char"
function love.load()
--WORLD--
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2+w2 and
x2 < x1+w1 and
y1 < y2+h2 and
y2 < y1+h1
end
world = love.physics.newWorld(0, 200, true)
ground = {}
ground.body = love.physics.newBody(world, 400, 575, "static")
ground.shape = love.physics.newRectangleShape(1000, 50)
ground.fixture = love.physics.newFixture(ground.body,ground.shape,100)
restart = function()
if alive == -2 and love.keyboard.isDown("r") then
alive = 2
player.lifes = 100
player.bulletsr = {}
player.bulletsl = {}
player.coldown = 20
monsters.coldown = 0
monsters.sup = {}
bitecooldown = 10
end
end
-- PLAYER--
--MONSTER--
end
function love.draw()
--PLAYER--
--MONSTER--
--WORLD--
love.graphics.polygon("fill", ground.body:getWorldPoints(ground.shape:getPoints()))
love.graphics.print(player.thisy , 90, 0)
love.graphics.print(player.lifes)
love.graphics.print(player.score,30, 0 )
love.graphics.setBackgroundColor( 0, 0, 60)
if alive == 2 then
love.graphics.print( "true", 60, 0)
else
love.graphics.print( "false", 60, 0)
end
end
function love.update(dt)
if player then
player:update(dt)
end
if monsters then
monsters:update(dt)
end
--WORLD--
restart()
world:update(dt)
for i, monster in ipairs(monsters.sup) do
for j, bullet in ipairs(player.bulletsr) do
if CheckCollision(monster.x, monster.y , 38, 46 , bullet.x, bullet.y, 10, 3) then
table.remove(player.bulletsr, j)
table.remove(monsters.sup, i)
player.score = player.score + 1
end
end
end
for i, monster in ipairs(monsters.sup) do
for j, bullet in ipairs(player.bulletsl) do
if CheckCollision(monster.x, monster.y , 38, 46 , bullet.x, bullet.y, 10, 3) then
table.remove(player.bulletsl, j)
table.remove(monsters.sup, i)
player.score = player.score + 1
end
end
end
for i, monster in ipairs(monsters.sup) do
if CheckCollision(monster.x, monster.y, 38, 46, player.x, player.y,22 ,54 )
and player.lifes > 0 and bitecooldown <= 0 then
bitecooldown = 10
player.lifes = player.lifes - 1
end
end
--PLAYER--
--MONSTER--
end
function love.keypressed(key)
if key == 'escape' then love.event.quit() end
if key == "right" then
t = 1
elseif key == "left" then
t = -1
end
if key == "r" then
alive = 2
end
end
Code: Select all
local enemy = {}
function enemy.load()
image2 = love.graphics.newImage("media/monster1.png")
local gmonster = anim8.newGrid(38,46, 228,46)
monsterr = anim8.newAnimation(gmonster('4-6',1), 0.1)
monsterl = anim8.newAnimation(gmonster('1-3',1), 0.1)
monsters = {}
bitecooldown = 10
monsters.coldown = 100
mdirection = -1
monsters.sup = {}
monsters.spawn = function()
if monsters.coldown <= 0 and player.lifes > 0 then
monsters.coldown = 100
monster = {}
randomNumber = love.math.random(1, 800)
if player.x < randomNumber and randomNumber <= player.x + 22 then
randomNumber = player.x +50
end
monster.x = randomNumber
monster.y = 490
monster.thisxM = nil
monster.thisyM = nil
monster.b = love.physics.newBody(world, monster.x + 38/2, monster.y + 46/2, "dynamic")
monster.b:setMass(10)
monster.s = love.physics.newRectangleShape(38, 46)
monster.f = love.physics.newFixture(monster.b, monster.s, 1)
table.insert(monsters.sup, monster)
end
end
end
function enemy.update()
monsters.spawn()
for i,v in ipairs(monsters.sup) do
monster.thisxM, monster.thisyM = v.b:getWorldPoints(v.s:getPoints())
v.y = monster.thisyM
monter.thisxM = v.x
end
bitecooldown = bitecooldown - 1
monsters.coldown = monsters.coldown - 1
for i,v in ipairs(monsters.sup) do
if v.x > player.x + 20 then
mdirection = -1
v.x = v.x - 3
elseif v.x < player.x - 30
then
v.x = v.x + 3
mdirection = 1
end
end
for i,v in ipairs(monsters.sup) do
v.b:setLinearVelocity(0, 300)
end
monsterl:update(dt)
monsterr:update(dt)
end
function enemy.draw()
if alive == 2 then
for i,v in ipairs(monsters.sup) do
if mdirection == -1 then
monsterl:draw(image2, v.x, v.y)
elseif mdirection == 1 then
monsterr:draw(image2, v.x, v.y)
end
end
end
end