Page 1 of 1

Pong help

Posted: Thu Oct 24, 2013 7:08 am
by PhunkMasterFlex
Ok so what i'm trying to do here, is get it so that when someone scores the ball will be stuck to the paddle and move with it until enter is pressed.

I got it so the ball moves to where the paddle is, it just won't move with it, and I don't know why.


The code that should make it move with the paddle is this

Code: Select all

if ball.x + ball.radius < 0 then
     ball.x = player.x + 33
     ball.y = player.y + 75
     ball.xv = 400
     ball.xy = 400
     player2.score = player2.score + 1
     go = false
     if love.keyboard.isDown("w") then
          ball.y = (player.y + 75) - (player.speed * dt)
     elseif love.keyboard.isDown("d") then
          ball.y = (player.y + 75) + (player.speed * dt)
     end
end
The code that's supposed to move the ball with the paddle starts at line 8. I'm obviously doing something wrong somewhere. Can someone help me out?

Also controls for the game are W and D for the left paddle, up arrow and down arrow for the right paddle, and return/enter to start the ball.

You can download the .love file at http://puu.sh/4Y4G2

Re: Pong help

Posted: Thu Oct 24, 2013 4:37 pm
by tavuntu
Hi, your problem it's only a logic problem, check the code you left above:

Code: Select all

if ball.x + ball.radius < 0 then -- This is true when the ball is scored to the left right?
     -- When you change the ball coordinates, this ^ won't be true anymore and
     -- the if-elseif structure of below won't work (nothing of this will be accesible)
		
                ball.x = player.x + 33
		ball.y = player.y + 75

		ball.xv = 400
		ball.yv = 400
		player2.score = player2.score + 1
		go = false
		if love.keyboard.isDown("w") then
			ball.y = (player.y + 75) - (player.speed * dt)
		elseif love.keyboard.isDown("d") then
			ball.y = (player.y + 75) + (player.speed * dt)
		end
	end

I can be wrong but, according to my logic, this is the problem you have, sorry for the bad English :)

Re: Pong help

Posted: Thu Oct 24, 2013 4:41 pm
by tavuntu
you could use something like a ball_retained flag to control when the ball must move along the paddle in th update method.