We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am an absolute beginner at coding in any way shape of form. As a 15-year-old with barely any experience thanks to a bare bones high school class, I decided to take it up as a hobby and it really rubs me the right way. It's a hell of a lot of fun and I hope to someday get as talented as the individuals on this forum. :) My question here is, how do I get the ball to bounce off the right side pedal? It's probably some tiny mistake, but I can't seem to find it. I would highly appreciate any help and possibly tips for a nooby. Please excuse my messy code.
Ninja Edit: To begin each round, you click anywhere. Left is controlled with W and A, while Right is using UP and DOWN. It should go up to 7 points, but there's a bug in there where you can continue playing if you click enough times. :P
float XCoor = (325);
float YCoor = (225);
float xspeed = 0;
float yspeed = 0;
float xspeed2 = xspeed;
float yspeed2 = yspeed;
float XCoor2 = 0;
float YCoor2 = 225;
float XCoor3 = 600;
float YCoor3 = 225;
int score1 = 0;
int score2 = 0;
PFont myFont;
void setup(){
size(650,450);
myFont = createFont("Courier Regular", 32);
textFont(myFont);
textAlign(CENTER);
}
void draw(){
background(0);
fill(255);
ellipse(XCoor, YCoor, 50, 50);
fill(255);
rect(XCoor2, YCoor2, 50, 100);
fill(255);
rect(XCoor3, YCoor3, 50, 100);
XCoor += xspeed;
YCoor += yspeed;
if(XCoor>=625){
xspeed*=-1;
}
if(YCoor>=425||YCoor<=25){
yspeed*=-1;
}
if(XCoor2 + 50 >= XCoor - 25){
if(YCoor>=YCoor2 && YCoor <= YCoor2 + 100){
xspeed *= -1;
}
if(YCoor2 + 100 >= YCoor - 25){
if(XCoor <= XCoor2 && XCoor >= XCoor2 + 100){
yspeed *= -1;
}
}
if(XCoor3 - 50 >= XCoor - 25){
if(YCoor>=YCoor3 && YCoor <= YCoor3 - 100){
xspeed *= -1;
}
}
if(YCoor3 - 100 >= YCoor - 25){
if(XCoor>=XCoor3 && XCoor <= XCoor3 - 100){
yspeed *= -1;
}
}
}
if(XCoor+25>=650){
score1++;
XCoor=325;
YCoor=225;
xspeed=0;
yspeed=0;
}
if(XCoor-25<0){
score2++;
XCoor=325;
YCoor=225;
xspeed=0;
yspeed=0;
}
text(score1,(width/2)-30, height/12);
text(score2,(width/2)+30, height/12);
if(score1==7||score2==7){
fill(255);
rect(0,0,650,450);
fill(0);
text("Game Over!",width/2,200);
text("Reboot game to play again",width/2,250);
}
}
void keyPressed(){
if(key == 'w'){
YCoor2 -= 10;
}
if(key == 's'){
YCoor2 += 10;
}
if (key == CODED) {
if (keyCode == UP) {
YCoor3 -=10;
}
if(keyCode ==DOWN){
YCoor3 +=10;
}
}
}
void keyReleased(){
if(key == 'w'){
YCoor2 -= 10;
}
if(key == 's'){
YCoor2 += 10;
}
if (key == CODED) {
if (keyCode == UP) {
YCoor3 -=10;
}
if(keyCode ==DOWN){
YCoor3 +=10;
}
}
}
void mousePressed(){
if(mousePressed){
xspeed-=3;
yspeed+=3.1;
}
}
Answers
Your code is fine. You even managed to post it formatted on the forums, which is a step above most first time posters. Plus you used createFont() instead of loadFont() so I could copy and paste and run your code without needing to make changes. Gold star.
You have some duplicated logic when it comes to key-pressing. The paddles move when the key is pressed down, and then again when it is released. Is this intentional?
I would suggest you use width and height instead of hard-coding edge boundary numbers.
Lines 33 to 35 doesn't really do anything, because the conditional on line 59 is basically the same, and the code inside that if will over-write the changes to XCoor that were made on line 34.
I'm not sure why your right paddle isn't bouncing, but your conditional checks are probably wrong. You might want to write a single function that does rectangle-ellipse collision checking, which you can then use and make sure that your logic is sound.
You might also want to kick things to the next level and write some classes. One for paddles, one for the ball.
Pong has been done to death, so I'm sure GoToLoop will now magically appear and post an example sketch...
Gold star on my first try; I'm honored! Thank you very much for taking the time to dig through this. There really isn't a reason for the paddles moving twice, that's just me being a noob with the syntax. Is there a way I can do it so the player doesn't have to spam the buttons to get it to move? Can you please explain where I should use the width/height? I'm not entirely sure what conditional checks are, but how do I make them correct? Writing a function for that sounds like a good idea. I'm just so used to staying within the comfort of my trusty if statements :) May I have an example of that? As for classes, I haven't learned how to do those yet. But that's definitely on the checklist.
Oh, you bet. Pong is definitely the most overused game, but I'm desperate to make something on my own for once, you know? I guess it's so overdone because of how simple the idea is, along with the fact that there's not much you've got to wrap your head around. Once again, thanks a ton. I apologize for the avalanche of questions.
Here's a code scrub for you. A lot has changed here.
Something that hasn't changed is the bounce-off-paddles logic. But now it is tidy. You can use Ctrl+t to auto-format your code. If you had done that, you might have noticed that the brackets don't line up in that logic in the way you would expect...
Wow, I have learned so much from that. Thank you! It'll be much easier to get it to work without all that clutter. If anybody else could please give me some advice on the paddles, though, I'd be very happy.