Trying to Draw a ball for a Pong Game

float ballX = 200;
float ballY = 200;
float playerY = 0;
boolean accX = true;
boolean accY = true;
boolean first = true;
boolean game_over = false;
void setup (){
   size(500, 500);
   background (0);
   fill(255);
   rect(50, playerY, 25, 50);
}

void dessballe (){
   nettoyer();
   fill(255);
   ellipse (ballX, ballY, 25, 25);
   smooth();
}
void player (float y1){
   fill(255);
   rect(50, y1, 25, 100);
}
void nettoyer (){
  background(255);
  background(0);
}
void draw (){
 /* while (ballY < 475 && ballX < 475 && first == true){
       ballX += 5;
       ballY += 5;
       dessballe ();
   }*/
   first = false;
   while (!(game_over)){
     if (ballX >= 500){
          accX = false;
       }else if (ballY <= 0){
          accY = true;
       }else if (ballY >= 500){
          accY = false;
       }else if (ballX <= 0){
          game_over = true;
       }
       if (accX == true){
          ballX += 5;
          dessballe();
       }else if (accX == false){
          ballX -= 5;
          dessballe();
       }else if (accY == true){
          ballY += 5;
          dessballe();
       }else if (accY == false){
          ballY -= 5;
          dessballe();
       }
   }
   frameRate(60);
}
void keyPressed (){
  if (keyCode == UP){
     nettoyer();
     playerY -= 5;
     player(playerY);
  }
  if (keyCode == DOWN){
     nettoyer();
     playerY += 5;
     player(playerY);
  }
}

I would love to help you answer this, but a couple points I know will be made:

First, you did not format your code. You can highlight your code and then press the format button, or just press Control+Shift+C. That makes it looks much nicer.

Secondly, I don’t think anyone can necessarily explain why your code doesn’t work, because no one really knows why? What’s wrong with your code? What are you trying to do?

Of course, what any of us can do (which is what I’m already doing now) is copy and paste your code into our Processing IDE and look for syntax and run-time errors, but that’s really as far as we can go!

So please feel free to tell us a little bit more about what it is you would love to do, and I can assure you lots of people would be willing to help!

EnhancedLoop7

2 Likes

As @EnhancedLoop7 said, please format your code and exaplin what your problem is. We are not soothsayer.

Now even without knowing what your code should do you have that in your draw() loop:

while (!(game_over)) 

Since you game is never over, you are never finishing your draw loop so you are never drawing anything on the screen (except for the part in setup())

You should read more about the way processing works: https://processing.org/reference/draw_.html

2 Likes

Thank you very much, i had just to replace the while loop by a condition. This code is a pong game, and the problem was that the ball wasn’t drawed.

This code is a pong game, and the problem was that the ball wasn’t drawed, but now it’s solved.
thanks for your reply.

1 Like

Okay, I’m glad that got solved then!

EnhancedLoop7

Happy to hear that :slight_smile:

Hope to see your game in the Gallery section soon :tada:

I have another problem:

After solving that bug i added another player but it doesn’t want to go to the top, here’s the code(the last condition doesnt want to work)(i formatted the code but here it doesnt want to format):

float ballX = 200;
float ballY = 200;
float playerY = 0;
float player2Y = 0;
boolean accX = false;
boolean accY = false;
boolean game_over = false;
boolean nettoye = true;
void setup (){
 size(500, 500);
 background (0);
 player(playerY);
}

void dessballe (float x, float y){
 nettoyer();
 if (nettoye){
 fill(255);
 ellipse (x, y, 25, 25);
 smooth();
 }
}
void player (float y1){
 fill(255);
 rect(50, y1, 25, 100);
}
void player2 (float y2){
 fill(255);
 rect(450, y2, 25, 100);
}
void nettoyer (){
 background(255);
 background(0);
 player(playerY);
 player2(player2Y);
 nettoye = true;
}
void draw (){
   balle ();
   frameRate(30);
}
void balle() {
if (!(game_over)){
 if (accY == true){
  ballY += 5;
 }
 if (accX == false){
  ballX -= 5;
 }
 if (accX == true){
  ballX += 5;
 }
 if (accY == false){
  ballY -= 5;
  }
 if (ballX >= 500){
  game_over = false;
 }else if (ballY <= 0){
  accY = true;
 }else if (ballY >= 500){
  accY = false;
 }else if (ballX <= 0){
  game_over = true;
}else if (ballX == 75 &&  ballY <= playerY + 100 && ballY >= playerY){
  accX = true; 
}else if (ballX == 450 &&  ballY <= player2Y + 100 && ballY >= player2Y){
  accX = false; 
}
 dessballe(ballX, ballY);
}
}
void keyPressed () {
   if (keyCode == UP){
     if (player2Y > 0 ){
      player2Y -= 7;
     }
   }else if (keyCode == DOWN){
     if (player2Y + 100 < 500){
      player2Y += 7;
     }
   }else if (key == 's'){
     if (playerY + 100 < 500){
      playerY += 7;
   }else if (key == 'z'){
     if (playerY > 0 ){
      playerY -= 7;
     }
   }
   }
}

Well I tried copying your game into my IDE. It seems like your ball isn’t bouncing correctly? I don’t know if that is how you want it, but I did have some issues with it going out of the screen when it wasn’t supposed to.

I found this pong game online here:

Made with Processing, and has two players. Maybe you can look at the code and get some ideas on what you want to do?

I have also made a two player pong game, so if you want any more help, I wouldn’t mind leading you towards achieving your goal, as best as I can. Hope this helps,

EnhancedLoop7

Please format your code :blush:

It consist on these two steps:

  1. In your code editor (PDE, VS code, Eclipse, etc) ensure you execute the beautifier function. This function automatically indents your code. Auto-indenting makes your code easier to read and helps catching bugs due to mismatch parenthesis, for instance. In the PDE, you use the key combination: ctrl+t
  2. You copy and paste your code in the forum. Then you select the code and you hit the formatting button aka. the button with this symbol: </>

That’s it! Please notice you do not create a new post in case you need to format something you already posted. You can edit your post, copy the code to the PDE, indent the code properly there and then past it back here, format the code and >> save << the edits.

Extra info:

Formatting your code makes everybody’s life easier, your code looks much better plus it ensures your code integrity is not affected by the forum’s formatting (Do you know the forum processes markup code?) Please visit the sticky posts or the FAQ section/post to learn about this, other advantages and super powers you can get in this brand new forum.

Kf

My code is formatted but when i post it it doesn’t want to format.

Hi @Blue_Obsidian – you are correct, your code text is indented properly in your post.

However, HTML ignores spaces, so the result looks flat and run-together. In order for the code to display correctly, you need to wrap it in ``` markers.

The easiest way to do this is to highlight your code in the editor, then press the </> button on the toolbar. I have edited your previous post to show you the result.

Hi Blue_Obsidian,

There are some things you can do to improve in your code.

First, don’t put the frameRate() function in the draw() function. You can simply add this line in the setup() function and it will work the same.

Then if you want more fluidity in your game, get rid of the keyPressed() function and modify the draw() function as followed:

void draw () {
  balle ();
  if (keyPressed) {
    if (keyCode == UP) {
      if (player2Y > 0 ) {
        player2Y -= 7;
      }
    } else if (keyCode == DOWN) {
      if (player2Y + 100 < 500) {
        player2Y += 7;
      }
    } else if (key == 's') {
      if (playerY + 100 < 500) {
        playerY += 7;
      } else if (key == 'z') {
        if (playerY > 0 ) {
          playerY -= 7;
        }
      }
    }
  }
}

Note that I simply copy pasted everything inside the keyPressed() function to put it in the if(keyPressed) statement.

Now for you problem of not beeing able to go up with the second player, you just messed up you brackets. Right now you can only enter the part where you press “z” if you press “s”. Check carefully.

Here is the proper implementation:

if (keyPressed) {
  // Player 2
  if (keyCode == UP) {
    if (player2Y > 0 ) {
      player2Y -= 7;
    }
  } else if (keyCode == DOWN) {
    if (player2Y + 100 < 500) {
      player2Y += 7;
    }
  } 

  // Player 1
  if (key == 's') {
    if (playerY + 100 < 500) {
      playerY += 7;
    }
  } else if (key == 'z') {
    if (playerY > 0 ) {
      playerY -= 7;
    }
  }
  
}

Thank you, my code works now.