Page 1 of 1

Falling Snow Collision Fail

Posted: Tue Dec 20, 2011 6:31 pm
by serakus
Hey,
I am new to Lua and Love. I read through the basics and wanted to build my first game. You have to catch the Snow. But i cant get the Collisions System to work. It works in a way but sometimes it doesn´t. Please Help me.

Hopefully sombody can help me ;) :)

Re: Falling Snow Collision Fail

Posted: Tue Dec 20, 2011 7:21 pm
by Robin
You packaged your .love the wrong way. Make sure to zip the contents of your game folder, not the folder itself.

Re: Falling Snow Collision Fail

Posted: Tue Dec 20, 2011 7:25 pm
by serakus
oh sorry...
normaly i am starting the game through the terminal...

Re: Falling Snow Collision Fail

Posted: Tue Dec 20, 2011 7:42 pm
by Robin
Don't worry about it. Just remember to zip the contents in the future.

Also, I've looked at your code, but couldn't find the problem.

Re: Falling Snow Collision Fail

Posted: Tue Dec 20, 2011 7:47 pm
by Nixola
Me too, it doesn't seem to have anything wrong

Re: Falling Snow Collision Fail

Posted: Tue Dec 20, 2011 7:54 pm
by serakus
But why does the collision system fail ?
I cant understand it ...

Re: Falling Snow Collision Fail

Posted: Tue Dec 20, 2011 8:14 pm
by coffee
serakus wrote:But why does the collision system fail ?
I cant understand it ...
Serakus, all of us are a bit confused. Where you think the collision system is failling? What it should be happen? At least I can't see any problem. With the box I managed to score some points so the collision is working. You need to explain what then was supposed then to happen please.

Re: Falling Snow Collision Fail

Posted: Tue Dec 20, 2011 8:43 pm
by tentus
Your problem was that you were drawing the player from the top left, but doing the collision from the center. It took me a little fiddling to find it though, it's a sneaky bug.

Also, your code can be far simpler if you make the gamestate a string, rather than a table of booleans. You also don't need else return in there, Lua is very forgiving like that.

Code: Select all

function love.load()
	love.graphics.setCaption("Falling Snow")
	love.graphics.setMode( 400, 320, false, false, 0)
	init()
end

function love.update(dt)
	changestate()
	_counter = _counter + dt
	if gamestate == "game" then
		timeout(dt)
		enemymove(dt)
		playermove(dt)
		if _counter >= 1 then
			_enemy()
			_counter = 0
		end
	end
end

function love.draw()
	if gamestate == "over" then
		love.graphics.print(" Press Enter to try Again!", 10, 160)
	elseif gamestate == "pause" then
		love.graphics.print(" Press Enter to continue!", 10 , 160)
	elseif gamestate == "menu" then
		love.graphics.print(" Catch the Snow!", 10, 148)
		love.graphics.print(" Press Enter to start the Game!", 10, 160 )
	elseif gamestate == "game" then
		-- Drawing the Player
		love.graphics.rectangle("fill", player.x, player.y, player.width, player.height)
		-- Drawing the Score/FPS/Time
		love.graphics.print("Time: " .. time, 10, 10)
		love.graphics.print("Score: " .. score, 10, 30)
		-- Drawing Enemys
		love.graphics.setPoint(3, "smooth")
		for i, v in ipairs(enemy) do
			love.graphics.point(v.x, v.y)
		end
	end
end

function playermove(dt)
	if love.keyboard.isDown("left") then
		if (player.x - dt * player.speed) > 0 then		
			player.x = player.x - dt * player.speed
		end
	elseif love.keyboard.isDown("right") then
		if (player.x + dt * player.speed) + 10 < 400 then
			player.x = player.x + dt * player.speed
		end
	end
	--[[
	if love.keyboard.isDown("up") then
		player.y = player.y - dt * player.speed
	elseif love.keyboard.isDown("down") then
		player.y = player.y + dt * player.speed
	end
	]]--
end

function _enemy()
	if enemycount <= 6 then
		local en = {}
		en.speed = math.random(70,100)
		en.x = math.random(0,390)
		en.y = 0
		table.insert(enemy, en)
		enemycount = enemycount + 1
	end
end

function enemymove(dt)
	for i,v in ipairs(enemy) do
		v.y = v.y + dt * v.speed
		if v.y >= 320 then
			table.remove(enemy, i)
			enemycount = enemycount - 1
		elseif v.x >= player.x and v.x <= (player.x + 10) and v.y >= player.y and v.y <= (player.y + 10) then
			table.remove(enemy, i)
			enemycount = enemycount - 1
			score = score + 1
		end
	end
end

function init()
	player = {}
	player.x = 10
	player.y = 308
	player.speed = 150
	player.height = 10
	player.width = 10
	gamestate = "menu"
	time = 20
	score = 0
	enemy = {}
	enemycount = 0
	_counter = 0
end

function changestate()
	if gamestate == "game" and love.keyboard.isDown("p") then
		gamestate = "pause"
	end
	if love.keyboard.isDown("return") then
		if gamestate == "over" then
			newGame()
			gamestate = "game"
		elseif gamestate == "pause" or gamestate == "menu" then
			gamestate = "game"
		end
	end
end

function timeout(dt)
	time = time - dt
	if time <= 0 then
		gamestate = "over"
	end
end

function newGame()
	init()
end

Re: Falling Snow Collision Fail

Posted: Tue Dec 20, 2011 10:33 pm
by serakus
Thank You very much :) :)