[Solved] Clearing Screen After Restart

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
RonCode
Prole
Posts: 5
Joined: Sat Jan 24, 2015 6:33 am

[Solved] Clearing Screen After Restart

Post by RonCode »

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:

Code: Select all

if player.health<=0 and love.keyboard.isDown('r') then
	enemies = {} 
end
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:

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
And my enemy.lua file:

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
Last edited by RonCode on Mon Feb 02, 2015 12:12 am, edited 1 time in total.
User avatar
Duster
Prole
Posts: 32
Joined: Fri Dec 19, 2014 8:34 pm
Location: Ontario, Canada

Re: [Help] Clearing Screen After Restart

Post by Duster »

You shouldn't store your list of enemies in the same table as your enemy variables. I'd make two separate tables, enemy and enemies. The former would hold variables about your enemies, the latter would hold your actual enemy instances. Currently by saying "enemies = {}" you're also getting rid of your variables and functions and all that. I'm surprised it isn't throwing an error.
I can't really answer you with complete certainty because I can't test around with the code (what I usually do when debugging), but try what I described above and post a .love.
RonCode
Prole
Posts: 5
Joined: Sat Jan 24, 2015 6:33 am

Re: [Help] Clearing Screen After Restart

Post by RonCode »

Duster wrote:You shouldn't store your list of enemies in the same table as your enemy variables. I'd make two separate tables, enemy and enemies. The former would hold variables about your enemies, the latter would hold your actual enemy instances. Currently by saying "enemies = {}" you're also getting rid of your variables and functions and all that. I'm surprised it isn't throwing an error.
I can't really answer you with complete certainty because I can't test around with the code (what I usually do when debugging), but try what I described above and post a .love.
Wooooot!!!!!!!!!!!!!!! Thank you!!!!!!!!! Making two tables solved the issue!

Thanks again!
-R
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests