Hello, I need help with recreating the game, “Pong”. I have gotten both of the paddles and the divider betwen which side is which, I need to get help with getting, the score, and the ball to move on its own, the mouse moves the left paddle and the arrow keys move the right paddle, up and down,
heres my code,
float leftScore;
float rx;
float rightScore;
float ball;
hi Adrian,
you already did half of the job, nice !
(first, side comment, when you paste code here select it and click </> icon, it’ s more easy for readers)
you’re not that far, you need a ball, manage ball’s movements, check if a paddle is on its way,
so let’s start first with the ball and basic moves,
see how you manage the right paddle, it s not that different, you had rx (wich is vertical y position of the paddle in fact), you need for the ball two variables, ballX and ballY for eg, draw a ball there,
then update those values
so, each frame you recalculate the new position of the ball, it will be last position+movement
this movement is defined by an angle and a speed so, for eg, ballX will be updated each frame like ballX=ballX+dx;
where dx=speed*cos(angle);
i let you do the same for ballY
angle will change when the ball hit a wall or a paddle and speed will be the difficulty
may be start to implement this without interaction with paddles
as you did in
if (rx > 970){
rx=970;
}
you have to manage when the ball hit the borders
so it will be the same, if(ballY<10) change the angle
i let you think here how the angle have to change on this case…
i tried to not give you too much code, but don t hesitate to ask if needed
so, I entered in the command just now and it says speed cannot be resolved to a variable, (it also said it had a problem with dx but i resolved that with a float command, would that be the same thing? or??)
ok, when you run do you see the errors about variables?
(you already added on declarations ballX and dx
but speed and angle are variables too, they are needed)
about:
ballX = ballX + dx;{
dx = speed* cos (angle);
}
you don ’ t need { } here
so now lets draw a ball at ballX,ballY coordinates
ballX will be the horizontal position, you need a ballY too for vertical position you can use ellipse()
then update movement, so calculate amount of mouvement it will be,
horizontally: dx=… as you did , and vertically dy=…
then update coordinates
ballX=ballx+dx;
ballY=ballY+dy;
something else: when you write float ballX;
you say to software that you will use a variable named ballX, by default here it will be set to 0.0, but it s not working for all kind of variable , and it s not the default value you want i think (the game shouldn’t start with a ball at 0,0 coordinates (up left part of the screen)
so, it s good to always set the default value you want
for eg:
ok, sorry it has taken me so long to reply back! thank you for the answer, I have been taking a small break from pong haha. I got the code, put it in there and looks like this
problem is there, i let you do the maths
you draw a ball at ballX, ballY
then, each frame, you update ballY = ballY+dy
do you see the problem?
then , next , when you see the ball, you need to calculate dy same than what you did for dx = speed* cos (angle);