Creating a count function

Im new to processing so im still a beginner. So, I have added code below that displays a simple maze. This code allows the users to move the ball around the maze, and to collect the small dots. I want to add a funtion that allows me to add and display the number of dots collected. so whenever the ball goes over the dots, the count goes up by one. I don’t know how to do this. Do I write an if statement? do I make it so that whenever the ball is in the coin position, the count goes up by one. I tried this but I don’t know how to make the position of the ball equal to the position of the coin.
Any suggestions?

// 1's are walls
// 2's are dots
// 0's are empty space
int[][] maze= {{1,1,1,1,1,1,1,1,1,1},
  {1,2,2,2,2,2,2,2,2,1},
  {1,0,1,1,1,1,1,1,0,1},
  {1,0,0,0,0,0,0,0,0,1},
  {1,0,1,1,1,1,1,1,0,1},
  {1,2,2,2,2,2,2,2,2,1},
  {1,1,1,1,1,1,1,1,1,1}};
  

int boxSize=50;
int ballX;
int ballY;
int radius;
int ballSpeed;

void setup() {
size(800, 600);
setupBall();
}
void draw() {
  drawMaze();
  drawBall();
}
void setupBall() {
  ballX = boxSize;
  ballY = boxSize*2;
  radius = boxSize/3;
  ballSpeed = 5;
}

void drawBall() {
  noStroke();
  fill(255, 0, 0);
  ellipseMode(CORNER); // draw from the corner of the circle not the centre
  ellipse(ballX, ballY, radius*2, radius*2); 
}

void keyPressed()
{
  if ( (keyCode == LEFT)  && !isWall(ballX-ballSpeed,ballY))
  {
    ballX = ballX - ballSpeed;
  }

  if ( (keyCode == RIGHT)  && !isWall(ballX+ballSpeed+radius*2,ballY))
  {
    ballX = ballX + ballSpeed;
  }

  if ( (keyCode == UP) && !isWall(ballX,ballY-ballSpeed))
  {
    ballY = ballY - ballSpeed;
  }

  if ( (keyCode == DOWN) && !isWall(ballX,ballY+radius*2+ballSpeed))
  {
    ballY = ballY + ballSpeed;
  }
  eatDot(ballX,ballY);
}

boolean isWall(int x,int y) {
  int mazeX = round(x/boxSize); 
  int mazeY = round(y/boxSize);
  if (maze[mazeY][mazeX]==1) 
    return true;
  else 
    return false;
}
void eatDot(int x,int y) {
  int mazeX = round(x/boxSize); 
  int mazeY = round(y/boxSize);
  if (maze[mazeY][mazeX]==2) 
    maze[mazeY][mazeX]=0;
}

void drawMaze() {

  int mazeHeight = maze.length;
  int mazeWidth = maze[0].length;

  for(int y=0;y<mazeHeight;y++) {

    for(int x=0;x<mazeWidth;x++) {

      if (maze[y][x]==1) {
        fill(0,0,0);
      } else {
        fill(255,255,255); 
      }

      rect(boxSize*x,boxSize*y,boxSize,boxSize);

      // draw the dots
      if (maze[y][x]==2) {
        fill(255,255,0);
        ellipseMode(CENTER);
        ellipse(boxSize*x+boxSize/2,boxSize*y+boxSize/2,boxSize/5,boxSize/5);
      } 
    }
  }
}
1 Like

In this function the eating happens, right?

Just add { } here where you set maze to 0 (that you already have) and then score++;

make score a global Variable int score=0;

2 Likes

I didn’t realise that I didn’t add the brackets. Thanks for your help.

1 Like