Updated! Need help with collision and constant enemies still.
Posted: Fri Jul 14, 2017 10:21 am
URGENT! ASSIGNMENT DUE IN THREE DAYS
Hello everyone, please don't roast me I'm quite aware I am not the best coder out there but I'm trying. I want to make my enemies spawn constantly, roughly about every 5 seconds. I'm not too sure how to make the lua timer to work with my code so any solution will do. I have a form of collision but it doesn't work anywhere near to where it's supposed to. When my player shoots bullets, it can kill the enemy from basically wherever the bullet goes. I was wondering if there's anyway to fix the collision or if I need to write a whole new one. If anyone could possibly help me out or write the collision for me it would be greatly appreciated.
Enemy code
The game code
Hello everyone, please don't roast me I'm quite aware I am not the best coder out there but I'm trying. I want to make my enemies spawn constantly, roughly about every 5 seconds. I'm not too sure how to make the lua timer to work with my code so any solution will do. I have a form of collision but it doesn't work anywhere near to where it's supposed to. When my player shoots bullets, it can kill the enemy from basically wherever the bullet goes. I was wondering if there's anyway to fix the collision or if I need to write a whole new one. If anyone could possibly help me out or write the collision for me it would be greatly appreciated.
Enemy code
Code: Select all
enemies_controller = {}
enemies_controller.enemies = {}
enemies_controllerSpawn = {}
enemies_controller.image = love.graphics.newImage('zookeeper.png')
function enemies_load()
enemies_controller:Enemy(love.math.random(0, love.graphics.getWidth()), 660)
enemies_controller:Enemy(love.math.random(0, love.graphics.getWidth()), -50)
enemies_controller:Enemy(-38, love.math.random(0, love.graphics.getHeight()))
enemies_controller:Enemy(1200, love.math.random(0, love.graphics.getHeight()))
timer = 0
end
function enemies_controller:fire()
if self.cooldown <= 0 then
self.cooldown = 30
bullet = {}
bullet.x = self.x
bullet.y = self.y + 35
table.insert(self.bullets, bullet)
end
end
function enemies_controller:Enemy(x, y)
enemy = {}
enemy.x = x
enemy.y = y
enemy.width = 10
enemy.height = 10
enemy.bullets = {}
enemy.cooldown = 30
enemy.speed = 20
table.insert(self.enemies, enemy)
end
function enemies_controller:Spawn()
for i=1,5 do
enemy[#enemy + 1] = {
x = enemy.x,
y = enemy.y,
w = enemy.width,
h = enemy.height
}
end
function enemy_update(dt)
timer = timer + dt
if timer >= 5 then
table.insert(enemies_controller)
timer = timer - 5
end
end
end
function enemy_draw()
for _,e in pairs(enemies_controller.enemies) do
love.graphics.draw(enemies_controller.image, e.x, e.y, 10, 10)
end
end
Code: Select all
function checkCollisions(enemies, bullets)
for i, e in ipairs(enemies) do
for _, o in ipairs(bullets) do
if o.y <= e.y + e.height and o.x >= e.x and o.x <= e.x + e.width then
table.remove(enemies, 1)
table.remove(bullets,1)
winner = true
end
end
end
end
function game_load()
winner = false
background_image = love.graphics.newImage('background.png')
bullet_image = love.graphics.newImage('banana.png')
player = {}
player.x = 580
player.y = 200
player.heat = 0
player.heatp = 0.1
bullets = {}
player.ground = love.graphics.getHeight() - 80
player.y_velocity = 0
player.gravity = -1000
player.speed = 7
player.image = love.graphics.newImage('rickstandard.png')
enemies_controller:Enemy(love.math.random(0, love.graphics.getWidth()), 660)
enemies_controller:Enemy(love.math.random(0, love.graphics.getWidth()), -50)
enemies_controller:Enemy(-38, love.math.random(0, love.graphics.getHeight()))
enemies_controller:Enemy(1200, love.math.random(0, love.graphics.getHeight()))
timer = 0
end
function game_update(dt)
if love.keyboard.isDown("d") then
player.x = player.x + player.speed
player.image = love.graphics.newImage('rickright.png'),
player.x == -1
end
if love.keyboard.isDown("a") then
player.x = player.x - player.speed
player.image = love.graphics.newImage('rickleft.png'),
player.x == 1
end
if love.keyboard.isDown("s") then
player.y = player.y + player.speed
player.image = love.graphics.newImage('rickdown.png'),
player.y == -1
end
if love.keyboard.isDown('w') then
player.y = player.y - player.speed
player.image = love.graphics.newImage('rickup.png'),
player.y == 1
end
player.heat = math.max(0, player.heat - dt)
if love.keyboard.isDown('space') and player.heat <= 0 then
local direction = math.atan2(love.mouse.getY() - player.y, love.mouse.getX() - player.x)
table.insert(bullets, {
x = player.x + 60,
y = player.y + 60,
dir = direction,
speed = 600,
})
player.heat = player.heatp
end
local i, o
for i, o in ipairs(bullets) do
o.x = o.x + math.cos(o.dir) * o.speed * dt
o.y = o.y + math.sin(o.dir) * o.speed * dt
if o.x < 0 or o.x > love.graphics.getWidth() then
table.remove(bullets, i)
elseif o.direction == -1 and o.x < o.xMax then
table.remove(bullets, i)
elseif o.direction == 1 and o.x > o.xMax then
table.remove(bullets, i)
else
o.previousX = o.x
end
end
if player.y_velocity ~= 0 then
player.y = player.y + player.y_velocity * dt
player.y_velocity = player.y_velocity - player.gravity * dt
end
if player.y > player.ground then
player.y_velocity = 0
player.y = player.ground
end
timer = timer + dt
if timer >= 5 then
enemies_controllerSpawn,
timer = timer - 5
end
checkCollisions(enemies_controller.enemies, bullets)