Falling Snow Collision Fail

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
serakus
Prole
Posts: 5
Joined: Tue Oct 18, 2011 7:25 pm

Falling Snow Collision Fail

Post 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 ;) :)
Attachments
Falling Snow.love
(2.18 KiB) Downloaded 171 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Falling Snow Collision Fail

Post by Robin »

You packaged your .love the wrong way. Make sure to zip the contents of your game folder, not the folder itself.
Attachments
Falling Snow.love
(1.03 KiB) Downloaded 176 times
Help us help you: attach a .love.
serakus
Prole
Posts: 5
Joined: Tue Oct 18, 2011 7:25 pm

Re: Falling Snow Collision Fail

Post by serakus »

oh sorry...
normaly i am starting the game through the terminal...
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Falling Snow Collision Fail

Post 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.
Help us help you: attach a .love.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Falling Snow Collision Fail

Post by Nixola »

Me too, it doesn't seem to have anything wrong
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
serakus
Prole
Posts: 5
Joined: Tue Oct 18, 2011 7:25 pm

Re: Falling Snow Collision Fail

Post by serakus »

But why does the collision system fail ?
I cant understand it ...
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Falling Snow Collision Fail

Post 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.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Falling Snow Collision Fail

Post 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
Kurosuke needs beta testers
serakus
Prole
Posts: 5
Joined: Tue Oct 18, 2011 7:25 pm

Re: Falling Snow Collision Fail

Post by serakus »

Thank You very much :) :)
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 7 guests