We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Just want to ask some questions about this PONG GAME. And here is the conditions: 1.the top paddle is controlled by the mouse, and the bottom is controlled by the keyboard. 2.If the ball passes the top or bottom paddle in the y direction, a point is scored (by console), and a new ball is generated.
Now I was stuck in the how to generate a new ball and how to consider if the ball hit the paddle or paddles missed it.
Here are the questions: 1.How can I make relationship between the coordinate of the center of the paddle to the center of the ball? Should I add more conditions in the if block to change the speed of ball which it hits the paddles? 2.How can I generate a new ball when the both paddles missed the ball? Should I add a boolean named collision and assign it to true? And if it was false the program will generate a new ball? Was my method right or wrong? Could you guys give me some hints?
float ballX = 250;
float ballY = 250;
float ballSize = 20;
float ballSpeedX = 3;
float ballSpeedY = 8;
float ballGenSpeed = 3;
float borderTop = 25;
float borderBottom = 450;
float borderLeft = 0;
float borderRight = 450;
float paddleH = 10;
float paddleW = 100;
float paddleSpeed = 15;
float paddleX;
void setup()
{
size(500, 500);
background(0);
}
void draw()
{
background(0);
ballX += ballSpeedX;
ballY += ballSpeedY;
if (ballX < borderLeft || ballX > borderRight)
{
ballSpeedX *= -1;
}
if (ballY < borderTop+(paddleH*2) || ballY > borderBottom-paddleH)
{
ballSpeedY *= -1;
}
ellipse(ballX, ballY, ballSize, ballSize);
boolean collision = true;
if (!collision) {
for (int i = 0; i<20; i++)
{
float theta = i/20 * 2 * PI;
float ballDirectX = cos(theta)*ballGenSpeed;
float ballDirectY = sin(theta)*ballGenSpeed;
ellipse(ballDirectX, ballDirectY, ballSize, ballSize);
}
}
rect(mouseX, borderTop, paddleW, paddleH);
rect(paddleX, borderBottom, paddleW, paddleH);
if (key == CODED) {
if (keyCode == LEFT && keyPressed) {
paddleX -= paddleSpeed;
} else if (keyCode == RIGHT && keyPressed) {
paddleX += paddleSpeed;
}
}
}
Answers
Your ballSpeedY should change the direction only
if (ballX>paddleX && ballX< paddleX +paddle)
. You should add these conditions to line 34. In the case of your top paddle, it is advised to also have a variable for an x position, rather then passing mouseX directly to rectangle.To reset your ball, you just need to assign
ballX = 250; ballY = 250;
This should happen whenballY<0 || ballY>height
.Did you saw the boolean collision, it means if paddles missed the ball, the ball should regenerate in random direction at 250,250. Is there any problem in my syntax? Should it happen when ballY<0 || ballY > height also? And for the first issue, ballX< paddleX +paddle , is there something missing in the last?
Well, you're setting boolean collision to be equal true just before checking if it is false. But this part of code will never run, as you just setting it to true always before this check!
What you should do is rethink your approach. For now you have one block of code, let's see if we can split it into more smaller blocks to make the structure more clear.
You need two paddles and means to control them. You don't have problems with that, except the issue with passing mouseX directly to rect, as I mentioned before. Consider having x variable for both paddles, paddle1X and paddle2X. Then, add a line
paddle1X = mouseX
before drawing rectangle. This makes more lines of code, but helps to organize it and make more clear. Now you can separate the code for everything connected with paddles in separate function, say:Notice that we first set variables and then drawing the rects to represent your paddles. This void should go outside your draw or setup, so it will be a separate function! Now, in draw you can just call
paddles();
and the code would run.Continuing next..
Secondly, you need to have another function for ball and everything that is connected with its behaviour and appearance, say
void ball(){...}
. You're fine in making ball bounce from left and right, but it also bounces from top and down, as I said previously, ballSpeedY should be reversed only if ball hits the paddle, so you need to rethink your conditional. This should happen if your ballY is smaller then borderTop+paddleH (not sure why you have paddle*2 there) and, as I mentioned whenballX>paddleX && ballX< paddleX +paddleW
- this line means that the speed will be reversed only if ballX is between the beginning and the and of your paddle. The same is for the bottom, you can organise it in one conditional statement, however I would recommend to have separate for top and bottom for now. Yourellipse(ballX, ballY, ballSize, ballSize);
should go to the end of a function. Remember, first configureing attributes, then drawing the shape!Upon accomplishing that you would have a ball that bounces from you paddles and leaves the screen if a paddle misses the ball. If that happens we want a ball to be reset (and maybe a score added to one of the players?). As I mentioned, resetting the ball means setting its coordinates back to original
ballX = 250; ballY = 250;
and now we are coming to the part in your code that could never been executed. Lets see what you try to achieve. You want a ball to have a random direction, right? But what you're doing in your lines 46-52 is creating 20 more balls! OMG, we need only one! and you already have an ellipse function to draw it. You also have variables for ball's speedX and speedY, so everything we need is to configure these. The randomness may be achieved in different ways, the random function may be what you need, however it returns a random float, so if you put a line like thisballSpeedX = random(-3,3);
you may get for example a value of 0.23 that is not what we need. I'll leave you to solve this by yourself, I'm sure you can do it. I would also recommend to change your ball speed variables types to int, as multiplying floats not always give an accurate result.So, you'll just need to add conditional, as I mentioned
if (ballY<0 || ballY>height){//set your ballX and ballY to 250 and set speedX and speedY to random}
To summarize the structure, you should have something like this
If you'll have a problem with random, just make a code without random direction and post it here, then we'll se what can be done further. If you have questions, just leave them here. waiting for your code :)