My pong game has broken colision detection

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
CraftedPixel
Prole
Posts: 7
Joined: Tue Dec 29, 2015 5:26 pm

My pong game has broken colision detection

Post by CraftedPixel »

Can someone help me with my game of pong
Sure I find the game very interesting and all but I could use help with debugging my game
So here is the things that work:
  • Ball colliding with top and bottom
    Game resetting when you loose
    Moving Player 1 with W and S
    Moving Player 2 with Up and Down
    Fun and (very strangely) interesting
And now for what I need to improve
  • Ball coliding with paddles is very glitchy
    The paddles go off screen but I don't think that is much of an issue
    I'm mostly having trouble with the greater than less than
Here is the main.lua

Code: Select all

function love.load()
	piy = 85
	piiy = 85
	ballx = 139
	bally = 99
	ballgox = 0
	ballgoy = 0
	rngi = love.math.random(0,1)
	rngii = love.math.random(0,1)
	if rngi == 0 then
		rngi = -1
	end
	if rngii == 0 then
		rngii = -1
	end
	ballgox = rngi
	ballgoy = rngii
end
function love.draw()
	love.graphics.rectangle("fill", 10, piy, 5, 15)
	love.graphics.rectangle("fill", 265, piiy, 5, 15)
	love.graphics.rectangle("fill", ballx, bally, 2, 2)
end

function love.update(dt)
	ballx = ballx + ballgox -- movement --
	bally = bally + ballgoy
	if love.keyboard.isDown("up") then
		piiy = piiy - 2
	elseif love.keyboard.isDown("down") then
		piiy = piiy + 2
	end
	
	if love.keyboard.isDown("w") then
		piy = piy - 2
	elseif love.keyboard.isDown("s") then
		piy = piy + 2
	end
	if bally <= 0 then -- colliding with bounds --
		ballgoy = 1
	elseif bally >= 200 then
		ballgoy = -1
	end
	if ballx <= 0 or ballx >= 280 then -- going off screen! --
		ballx = 139
		bally = 99
		love.time.sleep(0.1)
	end
	if piy >= bally and bally >= piy + 15 then -- collisions with paddles (the broken part) --
		ballgox = -1
	end
	
	if piiy >= bally and bally >= piiy + 15 then
		ballgox = 1
	end
end
And here is config.lua:

Code: Select all

function love.conf(t)
	t.window.width = 280
	t.window.height = 200
	t.title = "Pong?"
end
how can you have something like
if var1 <= var2 >= var3 then

instead of
if var1 <= var2 and var2 >= var3 then

Any help with shortening the code or helping with the game collisions would be gladly appreciated!
User avatar
player_258
Prole
Posts: 9
Joined: Thu Jun 20, 2013 3:37 pm

Re: My pong game has broken colision detection

Post by player_258 »

try this

Code: Select all

if bally >= piy and bally <= piy + 15 then -- collisions with paddles (the broken part) --
      ballgox = 1
end
   
if bally >= piiy and bally <= piiy + 15 then
      ballgox = -1
end
all that's left is to check if ballx is near the paddles, but i'm sure u can figure that out on your own
sorry if i make any english mistake, not my native language
CraftedPixel
Prole
Posts: 7
Joined: Tue Dec 29, 2015 5:26 pm

Re: My pong game has broken colision detection

Post by CraftedPixel »

player_258 wrote:try this

Code: Select all

if bally >= piy and bally <= piy + 15 then -- collisions with paddles (the broken part) --
      ballgox = 1
end
   
if bally >= piiy and bally <= piiy + 15 then
      ballgox = -1
end
all that's left is to check if ballx is near the paddles, but i'm sure u can figure that out on your own
Wow! Thanks man! that helped alot! I still want to keep the games character so I won't have a near paddles script.
Thanks for the help! Im calling the game wizard pong!

Oh by the way as return for the help I will put your User URL as a comment in my code. Thanks player!
(.) (.)
--------- <-- best smile face i could make
------
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests