[Solved] Clearing Screen After Restart
Posted: Sun Feb 01, 2015 8:06 pm
Hey Guys,
I've been trying to figure this out now for a couple of days to no avail...
I'm trying to clear the enemies from the screen by using enemies = {} after the user hits the restart button to clear the enemies table.
The exact code I'm using:
Presently, after each restart, the score and player.health resets as designed, but the enemies continue to spawn. How would I stop enemies from spawning after player.health == 0?
Any help would be much appreciated! Thanks!
-R
My main.lua file:
And my enemy.lua file:
I've been trying to figure this out now for a couple of days to no avail...
I'm trying to clear the enemies from the screen by using enemies = {} after the user hits the restart button to clear the enemies table.
The exact code I'm using:
Code: Select all
if player.health<=0 and love.keyboard.isDown('r') then
enemies = {}
end
Any help would be much appreciated! Thanks!
-R
My main.lua file:
Code: Select all
require "bin/player"
require "bin/bullet"
require "bin/enemy"
require "bin/font"
function love.load()
--Assets
player.img = love.graphics.newImage('assets/player.png')
playerLeft = love.graphics.newImage('assets/playerLeft.png')
playerRight = love.graphics.newImage('assets/playerRight.png')
bulletImg = love.graphics.newImage('assets/bullet.png')
enemyImg = love.graphics.newImage('assets/enemy.png')
gunSound = love.audio.newSource("assets/gun-sound.wav", "static")
backgroundImg = love.graphics.newImage('assets/spaceBackground.png')
--Background Music
bgm = love.audio.newSource("assets/bgsong.mp3", "stream", true)
love.audio.play(bgm)
--Vital Variables
gamestate = 'playing'
screenWidth = love.graphics.getWidth()
screenHeight = love.graphics.getHeight()
default_font = love.graphics.newFont(20)
--Loading Libraries
player.load()
end
function love.keypressed(key)
if gamestate == 'playing' then
player.shoot(key)
end
end
function love.update(dt)
if gamestate == 'playing' then --so character doesn't move if in a menu
UPDATE_PLAYER(dt)
UPDATE_BULLET(dt)
UPDATE_ENEMY(dt)
end
end
function love.draw()
love.graphics.draw(backgroundImg, 0, 0)
if gamestate == 'playing' then --so character doesn't move if in a menu
DRAW_PLAYER()
DRAW_ENEMY()
DRAW_FONT()
DRAW_BULLET()
end
end
Code: Select all
enemies = {}
enemies.width = 78
enemies.height = 78
enemies.speed = 150
enemies.timer = 0
enemies.timerLim = math.random(1,2)
screenWidth = love.graphics.getWidth()
enemies.spawnX = math.random(0, screenWidth - enemies.width) --spawn enemy between left or right side of the screen
function enemies.spawn(x,y)
table.insert(enemies, {x=x, y=y})
end
--
function enemies.draw()
for i,v in ipairs(enemies) do
love.graphics.draw(enemyImg, v.x, v.y)
--love.graphics.setColor(0,255,0) --set enemies to green
--love.graphics.rectangle('fill', v.x, v.y, enemies.width, enemies.height)
end
end
--
function enemies.move(dt)
for i,v in ipairs(enemies) do
v.y = v.y + enemies.speed * dt
end
end
--
function enemies.boundary()
for i,v in ipairs(enemies) do
if v.y > screenHeight then
table.remove(enemies, i)
end
--comparing two x and y values to see if they're overlapping and reduce health
if v.x + enemies.width > player.x and
v.x < player.x + player.width and
v.y + enemies.height > player.y and
v.y < player.y + player.height then
player.health = player.health - 1
table.remove(enemies, i)
end
end
end
--
function enemies.collide()
for i,v in ipairs(enemies) do
for ia,va in ipairs(bullet) do
if v.x + enemies.width > va.x and
v.x < va.x + bullet.width and
v.y + enemies.height > va.y and
v.y < va.y + bullet.height then
player.points = player.points + 1
table.remove(enemies, i)
table.remove(bullet, ia)
end
end
end
end
function enemies.spawningHandler(dt)
enemies.timer = enemies.timer + dt
if enemies.timer > enemies.timerLim then
enemies.spawn(enemies.spawnX,-enemies.height)
enemies.timer = 0
enemies.timerLim = math.random(1,2)
enemies.spawnX = math.random(0,screenWidth - enemies.width)
end
end
--PARENT FUNCTIONS
function DRAW_ENEMY()
enemies.draw()
end
function UPDATE_ENEMY(dt)
enemies.move(dt)
enemies.boundary()
enemies.collide()
enemies.spawningHandler(dt)
end