Loading...
Logo
Processing Forum
I have two problems in my code, if anybody can help out it would be great.
1. for some reason every time i try to make my ellipse in Pong collide with my paddles, it doesn't work, if anyone could help I'd be grateful.
2. I have to write a 2D array for this game, there is a 8 x 2 grid seen on the game screen, and every time the ball goes through a section of a grid, i want to add a sound effect (I'm using SoundCipher but i dont need help with that). Could someone help me set up this Array? I'm really stuck.

The code is a bit messy at the moment, i'm trying out a few things and forget to delete some code.

Copy code




  1. //Globals for paddles
    int paddle1x, paddle1y;
    int paddle2x, paddle2y;
    int sizeAcross = 10, sizeDown = 60;



    //globals for ellipses
    int ellipseX, ellipseY;
    int speedX = 1, speedY = -1;
    int ellipseSize = 10;

    //globals for line spacings
    int lineX, spacing = 10;
    boolean startPong = false;


    //globals for grid
    PFont f;
  2. int gridcell1;//width of a grid cell
    int gridcell2;
    int xpos; //x coordinate in the grid in cell number units, 0 to number of columns - 1.
    int ypos;



    // Number of columns and rows in the grid
    int cols = 8;
    int rows = 2;




    void setup()
    {
      size(400, 400);
      stroke(255);
      paddle1x = 0;
      paddle2x = width - sizeAcross;
      ellipseX = width/2;
      ellipseY = height/2;
      lineX = width/2;
      f = createFont("Arial", 48);
      textFont(f, 10); // No need to call it on each cell, if you don't change it
      textAlign(CENTER);
      cols = width/50;
      rows = height/200;
    }


    void draw()
    {
      //redrawing the background each time
      background(0);


      {
        //draw grid
        gridDivision();

        //draw the paddles
        drawPaddles();

        //draw circles
        drawEllipses();
  3. gridcell1 = width/cols;
        gridcell2 = height/rows;
       
        xpos = ellipseX/gridcell1;
        ypos = ellipseY/gridcell2;
        println(xpos + ypos * cols);
      }
    }

    void gridDivision()
    {

     //draw grid
  4. rectMode(CORNER);
      for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++) {
          int x = i*50;
          int y = j*200;
          if (ellipseX >= x && ellipseX < x + 50 &&
            ellipseY >= y && ellipseY < y + 200)
              {
              }
          else {
            fill(255);
                 
          }

          stroke(0);
          rect(x, y, 50, 200);

          fill(100, 150, 200);
         
          text(str(j * cols + i + 1), x + 50 / 2, y + 200 / 2);

        }
      }

      for (float i = 0; i < height; i = i + spacing)
        line(lineX, i, lineX, i + 5);
    }



    void drawPaddles()
  5. rectMode(CENTER);


      //keys are for other characters
      paddle1y = ellipseY;


      if (paddle1y < 0) //ensure the paddle doesn't go off the top of the screen
        paddle1y = 0;
      else if (paddle1y > height - sizeDown) // ensure the paddle doesn't go off the bottom of the screen
          paddle1y = height - sizeDown;

      paddle2y = ellipseY;
      if (paddle2y < 0) //ensuring the paddle doesn't go off the top of the screen
        paddle2y = 0;
      else if (paddle2y > height - sizeDown) //ensuring the paddle doesn't go off the bottom of the screen
          paddle2y = height - sizeDown;
      //draw the paddles
      rect(paddle1x, paddle1y, sizeAcross, sizeDown);
      rect(paddle2x, paddle2y, sizeAcross, sizeDown);
    }



    void drawEllipses()
    {




      ellipseX = ellipseX + speedX; //Move the ball along the x axis
      ellipseY = ellipseY + speedY; //Move the ball along the y axis



      if (ellipseX > width)  //if the ellipse goes off the right of the screen. Note there is a nested if here.
      {
        speedX = -3; //set the speed to a negative number so it aims for the left hand paddle
        ellipseX = width/2; //reset the ellipse to the centre of the screen
        ellipseY = height/2;
      }



      if (ellipseX  < 0) //if the ellipse goes off the left of the screen. Note there is a nested if here.
      {
        speedX = 3; //set the speed so it aims for the player on the right
        ellipseX = width/2; //reset the ellipse to the center of the screen
        ellipseY = height/2;
      }

  6.  if (ellipseX + ellipseSize/2 == paddle1x - sizeAcross/2
      && ellipseY + ellipseSize/2 <= paddle1y + sizeDown/2
      && ellipseY - ellipseSize/2 >= paddle1y - sizeDown/2)
      {
        speedX = 1;
      }






      //if the ellipse goes off the bottom of the screen
      if (ellipseY > height - ellipseSize/2)
        speedY = speedY * - 1;

      //if the ellipse goes off the top of the screen
      else if (ellipseY < ellipseSize/2)
        speedY = speedY * - 1;





      //draw ball
      ellipse(ellipseX, ellipseY, ellipseSize, ellipseSize);
    }




    void keyPressed() {

      final int k = keyCode;

      if (k == 'R')//press r to pause game.
        if (looping)  noLoop();
        else          loop();
      if (keyPressed)
      {
        if (key == CODED)
        {
          if (keyCode == RIGHT) //if the right arrow has been pressed
            ellipseX = ellipseX + 3;
          else if (keyCode == LEFT) //if the left arrow has been pressed
            ellipseX = ellipseX - 3;
        }
      }
    }




Replies(11)

When I say the collision doesn't work in pong, I mean i put in the code required but nothing happens when i run it, no error is shown.
I don't see the code corresponding to the collision with the paddles.
And you should clean up the code, starting by eliminating the dependency on a library which isn't necessary (yet) and either stops users from testing your code (if not having the library) or needing them to eliminate the corresponding code (better ease the task of those willing to help).

What do you want to put in your array?
I removed the code corresponding to the collision because it didn't do anything! I've been trying different code and it doesn't work, so i removed it completely hoping somebody here could figure out a code that does work!

Regarding the array, there is a 8 x 2 grid used in this program, and i want these things to happen. These all have to do with soundcipher, a library i can use, but i dont know how to make something happen if in a segment of a grid. Can you help me out?

a. If the ball is in an odd numbered segment, a note of 8 times the segment value should be sent to the java sound synthesizer (i.e. the note should be played) (velocity and duration of your choice). The note can only hold values ranging between 0 and 127. The resulting note should also be stored in the array at the segment number’s position.
b. Every five times the ball hits an odd numbered segment, the instrument value should change randomly. The instrument value cannot be greater than 127.
c. If the ball in an even numbered segment, a note message of 7 times the segment position should be sent to the java sound synthesizer (i.e. the note should be played) (velocity and duration of your choice). The note can only hold values ranging between 0 and 127. The resulting note should also be stored in the array at the segment number’s position.
d. If the ball is located in segment 6, it causes (an) action(s) of your choice (either graphical, sound or both) to happen. The action should be an algorithm, and more than the use of a single programming function.

Also i've removed the library at the start.
There are lot of examples of code showing how a ball can hit a rectangle, starting with the examples shipped with Processing. There is little point in pasting such code here. I believe it is more interesting to show your best attempt, and then we can show what went wrong... Perhaps more educative than spoon feeding with some ready code.

The text of the assignment is indeed strongly related to the library, it seems this part should be in the Contributed Libraries section (as knowledge of the library is needed). It doesn't seem you need an array, only detect which part of the grid the ball is in.
While i work on the collision code, how do you suggest i detect which part of the grid the ball is in, and call upon this?

I will need to be able to make something happen when it is in a section of a grid, so would an if statement work?

Divide width by the number of columns, it will give the width of a grid cell. Divide the horizontal position of the ball by this width, it will give the x coordinate in the grid in cell number units, 0 to number of columns - 1.
Do something similar vertically. You will get a posX, posY coordinate within the grid.
If you number these cells from left to right, top to bottom, just add posX to posY * number of columns (I voluntarily don't use numbers, use global constants instead, it allows to change a value at one place and have all occurrences updated at once).
I've added this code and it should work, but it seems to do nothing.
Copy code
  1.   if (ellipseX + ellipseSize/2 == paddle1x - sizeAcross/2
      && ellipseY + ellipseSize/2 <= paddle1y + sizeDown/2
      && ellipseY - ellipseSize/2 >= paddle1y - sizeDown/2)
      {
        speedX = 1;
      }

Also I added rectMode(CENTER); to the code in void drawPaddles() because i have a function in there that makes the paddles follow the ball, and now they are following from the middle of the paddle instead of the top left hand corner.
ok so i've added this code to void draw
Copy code
  1. gridcell1 = width/cols;
        gridcell2 = height/rows;
       
        xpos = ellipseX/gridcell1;
        ypos = ellipseY/gridcell2;
        println(xpos + ypos * cols);
but I am unsure how to act upon this, how do i make something happen if the ball is in a cell, for example all cells with an odd number? or cell number 6? perhaps im missing some simple concept.
In println it is showing that the ball is in let's say grid 6, but how do i call on this, make something occur if in cell 6. I want to make an if statement like this

Copy code
  1. if (ellipseX == "segment 6")
  2.             {
  3.                   sc.playNote(60, 100, 2.0);
Copy code
  1.                                                            }

Thanks for your help so far.

if (ellipseX + ellipseSize/2 == paddle1x - sizeAcross/2
The ball, because of its speed, can be already beyond this limit. Use <= or >= instead.

if (ellipseX == "segment 6")
Better compute the position as you did:
int pos = xpos + ypos * cols;
Then you can do the comparison like:
if (pos == 6)

The collision still doesnt seem to be working... really can't get my head around why...
Got the other stuff working though, thanks a lot! I thought I'd never get my head around it.