I'm currently following a tutorial to create a "Zombie-shooting-survival" game on Udemy that I think is kinda good.
But I have a problem where my "zombies" are supposed to delete themselves when they collide with the player but sometimes it works but more than often it doesn't (the zombies stack up on each other when created).
I can't really see what I'm doing wrong and would appriciate an extra set of eyes that could take a look at my "tutorial" project.
Thanks!
Code: Select all
function love.load()
-- Game sprites
sprites = {}
sprites.player = love.graphics.newImage('sprites/player.png')
sprites.bullet = love.graphics.newImage('sprites/bullet.png')
sprites.zombie = love.graphics.newImage('sprites/zombie.png')
sprites.background = love.graphics.newImage('sprites/background.png')
-- Player table
player = {} -- Player table, can take care off a lot of variables
player.X = 200 -- Player's inital starting position on the X-axis
player.Y = 200 -- Player's inital starting position on the Y-axis
player.speed = 180 -- Player's "speed" multiplied with deltaTime
-- Zombie table, for several zombies
zombies = {}
end
-- Player movement on Y-axis
function love.update(dt)
if love.keyboard.isDown("w") then
player.Y = player.Y - player.speed * dt
elseif love.keyboard.isDown("s") then
player.Y = player.Y + player.speed * dt
end
-- Player movement on X-axis
if love.keyboard.isDown("a") then
player.X = player.X - player.speed * dt
elseif love.keyboard.isDown("d") then
player.X = player.X + player.speed * dt
end
--Zombies update
for i,z in ipairs(zombies) do
z.X = z.X + math.cos(zombiePlayerAngle(z)) * z.speed * dt
z.Y = z.Y + math.sin(zombiePlayerAngle(z)) * z.speed * dt
if distanceBetween(z.X, z.Y, player.X, player.Y) < 30 then
-- Removes Zombies in the table
for i,z in ipairs(zombies) do
zombies[i] = nil
end
end
end
end
function love.draw()
-- Background gets drawn first
love.graphics.draw(sprites.background, 0, 0)
-- And Player gets drawn ontop of the background
love.graphics.draw(sprites.player, player.X, player.Y, playerMouseAngle(), nil, nil, sprites.player:getWidth()/2, sprites.player:getHeight()/2)
-- Cycles through the zombies table and draws(?) each zombie using different values
for i,z in ipairs(zombies) do
love.graphics.draw(sprites.zombie, z.X, z.Y, zombiePlayerAngle(z), nil, nil, sprites.zombie:getWidth()/2, sprites.zombie:getHeight()/2)
end
end
function playerMouseAngle()
-- Enables so that the Player sprite can rotate and face the mouse's current position
return math.atan2(player.Y - love.mouse.getY(), player.X - love.mouse.getX()) + math.pi
end
function zombiePlayerAngle(enemy)
-- Enables so that the Zombie sprite can rotate and face the Player's current position
return math.atan2(player.Y - enemy.Y, player.X - enemy.X)
end
function spawnZombie()
-- For one singular zombie, different compared to the zombies table
zombie = {}
zombie.X = math.random(0, love.graphics.getWidth())
zombie.Y = math.random(0, love.graphics.getHeight())
zombie.speed = 130
-- Takes the content in the singular zombie table and adds it to the zombies table (above)
table.insert(zombies, zombie)
end
function love.keypressed(key, scancode, isrepeat)
if key == "space" then
spawnZombie()
end
function distanceBetween(X1, X2, Y1, Y2)
return math.sqrt((Y2 - Y1)^2 + (X2 - X1)^2)
end
end