Pong help

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
User avatar
PhunkMasterFlex
Prole
Posts: 1
Joined: Thu Oct 24, 2013 6:42 am

Pong help

Post 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
User avatar
tavuntu
Citizen
Posts: 65
Joined: Mon Dec 24, 2012 6:56 am
Contact:

Re: Pong help

Post 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 :)
Last edited by tavuntu on Thu Oct 24, 2013 4:47 pm, edited 1 time in total.
User avatar
tavuntu
Citizen
Posts: 65
Joined: Mon Dec 24, 2012 6:56 am
Contact:

Re: Pong help

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests