Page 1 of 1

Y collision check fails

Posted: Mon Dec 05, 2011 8:38 pm
by PurpleDrain
Hello!

I apologize for making this my first post, but I need help with this problem I can't seem to figure out!
What I am trying to make is a classic ping pong game, and I am having a problem with whenever the left bar touches the top or the bottom of the screen the right bar seem to fail the check.

I will appreciate any kind of help, also feel free to tell me if you can see anything that I can improve upon or change.

Thanks.

Re: Y collision check fails

Posted: Mon Dec 05, 2011 9:20 pm
by tentus
Change line 41:

Code: Select all

elseif p2y <= 5 then
to this:

Code: Select all

end
if p2y <= 5 then

Re: Y collision check fails

Posted: Mon Dec 05, 2011 10:09 pm
by PurpleDrain
Thanks! I appreciate your help.

Re: Y collision check fails

Posted: Mon Dec 05, 2011 11:51 pm
by tentus
Just a suggestion, but your code would be simpler and cleaner if you do the minimum/maximum checks inside of the isDown statements.

Code: Select all

-- GAME START

p1x, p1y = 70, 300 -- P1 start pos
p2x, p2y = 730, 300 -- P2 start pos
mx, my = 0, 0 -- Default mouse val

function love.draw()
	love.graphics.print("Mx: " .. mx .. "\n" .. "My: " .. my, 700, 20) -- X,Y Info

	p1 = love.graphics.rectangle("fill", p1x, p1y, 10, 85) -- P1 entity
	p2 = love.graphics.rectangle("fill", p2x, p2y, 10, 85) -- P2 entity
end

function love.update(dt)
	mx, my = love.mouse.getPosition()

	local p1yc, p2yc = p1y + 65, p2y + 65

-- Controls P1
	if love.keyboard.isDown("w") then
		p1y = p1y - 5
		if p1y < 5 then
			p1y = 5
		end
	elseif love.keyboard.isDown("s") then
		p1y = p1y  + 5
		if p1y > 510 then
			p1y = 510
		end
	end
-- Controls P2
	if love.keyboard.isDown("up") then
		p2y = p2y - 5
		if p2y < 5 then
			p2y = 5
		end
	elseif love.keyboard.isDown("down") then
		p2y = p2y  + 5
		if p2y > 510 then
			p2y = 510
		end
	end

end


Re: Y collision check fails

Posted: Tue Dec 06, 2011 7:31 am
by PurpleDrain
That's more effective, I've implented that instead now. I thank you again for your help!