Simple Maze Game Problem

Hi there! I am using Processing for a school project and I am not able to resolve an issue.
I don’t know how to stop the red ball when it hits the boundary of the maze. I’m thinking to put the pixels of the ball and the maze.jpg in two different array. I don’t know how to say to the ball’s array STOP it when touch the black pixels. Basically the ball can’t go through the maze.
Also I organize everything in three classes, could be a problem for the arrays?

Please some one can help me?

“GAME”

Ball player;
Maze img; 

void settings() {
  size (1000, 1000, P2D);
  img = new Maze ();
  player = new Ball();
}

void draw() {
  clear();
  background (255);
  img.display();
  player.display ();
}

“BALL”

class Ball {
  float x;
  float y; 
  float n;
  float m;
  //float a;
  float b= width-800;    //canvas is 1000,
  float c = height-800;

  Ball () {
    //float n = map ( a, 0, 100, 650, width);
   // float m = map ( a, 0, 100, 650, height);
    float n = constrain (b,650,width);
    float m = constrain (c,650,height);
    x = random(n);
    y = random (m);
  }

  void display() {

    if (mousePressed==false) {
      noStroke();
      fill (255, 0, 0);
      ellipse (x, y, 15, 15);
    } else {
      noStroke();
      fill (255, 0, 0);
      ellipse (mouseX, mouseY, 15, 15);
    }
  }
  
}

“MAZE”

class Maze {
  PImage img1;

  Maze () {
    img1 = new PImage ();
    img1 = loadImage ("circular maze.png");
    size (width, height, P2D); 
    img1.resize(img1.width*2, img1.height*2);
  }

  void display () {
    pushMatrix ();
    translate(width/2, height/2);
    imageMode (CENTER);
    image (img1, 0, 0);
    popMatrix ();
  }
}
1 Like

In my understanding your maze is an image you load.

Basically you can use color col = get(playerX, playerY); to get the pixel color under the player position

Let’s say the walls are black and the paths are white then you can distinguish when you use brightness ()

Basically when you move the player with the cursor check the pixel he would go to for its brightness and if it’s black it’s not allowed to move

Hey, and welcome to the forum!

Great to have you here!

Warm regards,

Chrisir

1 Like

Example code with mouse



void setup() {
  size(600, 600);
  textSize(26);
}

void draw() {
  // white background 
  background(255);

  // black rect on the left 
  fill(0); 
  rect(0, 0, 
    width/2, height);

  // get test color 
  color colorFromMousePosition = get(mouseX, mouseY); 

  // eval test color
  fill(255, 0, 0); // RED 
  println(brightness(colorFromMousePosition));
  if (brightness(colorFromMousePosition) < 50) 
    text("black", 17, 24);
  else 
  text("white", 17, 24);

  // Text with help text
  textAlign(CENTER);
  text("Move the mouse over the areas", 
    width/2, 96);
  textAlign(LEFT); // back to default
}
1 Like

Tomorrow I will try to resolve with your suggestion and yes, it is an image I will upload like a jpg. Thank you very much Chrisir.

1 Like
class Ball {
  float x;
  float y; 
  float n;
  float m;
  float b= width-800;  
  float c = height-800;
  PImage img1;
  color colorFromMousePosition = get(mouseX, mouseY); 
  //float a;


  Ball () {
    float n = constrain (b, width, width-800);                  
    float m = constrain (c, height, height-800);            
    x = random(n);
    y = random (m);
  }

  void display() {

    if (mousePressed==false) {
      noStroke();
      fill (230, 0, 0);
      ellipse (x, y, 12, 12);
    } else {
      noStroke();
      fill (230, 0, 0);
      ellipse (mouseX, mouseY, 12, 12);
    }
  }
  void boundary () {
    if (brightness(colorFromMousePosition) == 255) {
      ellipse (mouseX, mouseY, 12, 12);
    } else {
      ellipse (x-1,y-1,12,12);
    }
  }
}

This time I post only the Class ball. I hvae a problem, in the variables of the class I put " color colorFromMousePosition = get(mouseX, mouseY); " and then in the function I creat a void boundary but if I run the code is shown the “NullPointerException”……… Also in the last else I don’t think so the x and y of the elllipse are good to stop the movement… Have you got any other input?? I’m stuck

1 Like

err…

You don’t have reached a full understanding of the idea…

1.

This line
color colorFromMousePosition = get(mouseX, mouseY);
would not be executed throughout but only once at start.
It’s not a rule / standing order that processing would execute throughout or so.

Instead you need to repeat colorFromMousePosition = get(mouseX, mouseY); in a part that gets executed again and again - so in the display() method of the class for example, or in boundary().

void boundary () {
   colorFromMousePosition = get(mouseX, mouseY);  //   !!!!! 

   if (brightness(colorFromMousePosition) == 255) {
      ellipse (mouseX, mouseY, 12, 12);
      ....

2.

Of course you don’t want to test your point of the maze with mouseX,mouseY, that was only an example.
You want to test it with the ball position — or is the mouse position the ball position?
Then my point (2) is irrelevant.

I don’t understand fully, if you want to drag the ball with the mouse? Probably the dragging should only be allowed when there’s no wall in the way?

Chrisir

1 Like

The ball should move when the mouse is cliccked, and yes I want to test with the postion of the ball not with the mouse postion. I’m stuck

1 Like