We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › need help making solid walls for a maze and more
Page Index Toggle Pages: 1
need help making solid walls for a maze and more (Read 1065 times)
need help making solid walls for a maze and more
Apr 28th, 2010, 7:26pm
 
Like the topic says I'm making a little maze game, and as it is right now I can just travel through walls with the ball. I obviously need some way to make the walls solid so i can't move through them. The ball can just slide along the wall if u make it; I don't want them to lose the game, its more of a maze than a "don't touch the sides game". I also want to make a "win screen" once you get the ball to the red square that basiclly puts up a screen in front of the maze that just says you win, etc.. I've tried a few things but I just can't get this to work. Thanks for your time.

Code:


PFont font;
float x;
float y;
float targetX, targetY;
float easing = 0.05;

void setup()
{
 size(800, 800);
 textAlign(CENTER);
 font = loadFont("AmericanLife-36.vlw");
 textFont(font);
 smooth();
 noStroke();  
}

void draw()
{
 background(100,100,100);
 
 targetX = mouseX;
 float dx = targetX - x;
 if(abs(dx) > 1)
 {
   x += dx * easing;
 }
 
 targetY = mouseY;
 float dy = targetY - y;
 if(abs(dy) > 1)
 {
   y += dy * easing;
 }
 fill(255,255,255);
 stroke(0);
 strokeWeight(1);
 ellipse(x, y, 33, 33); // moving ball
 fill(255,255,255);
 stroke(0,0,0);
 strokeWeight(10);
 rect(-10,120,500,100);//biggest rect
 rect(150,-10,50,60);
 rect(340,70,50,50);
 triangle(600,-10,810,-10,810,200);
 ellipse(570,170,50,50);
 ellipse(700,250,50,50);
 ellipse(530,225,50,50);
 ellipse(740,200,50,50);
 ellipse(656,300,50,50);
 triangle(500,275,550,350,500,400);
 triangle(650,350,600,400,650,450);
 triangle(525,550,590,420,640,475);
 rect(285,500,200,50);
 rect(285,400,200,-135);
 rect(200,330,20,250);
 rect(200,600,20,110);
 rect(220,650,120,20);
 rect(700,700,-100,-100);
 rect(725,375,20,375);
 rect(725,730,-600,20);
 rect(500,680,-50,50);
 triangle(-10,250,100,250,-10,400);
 rect(-10,425,100,20);
 rect(100,510,100,20);
 rect(-10,600,100,20);
 fill(255,0,0);
 rect(25,700,75,75); //win area
 fill(0,0,255);
 rect(25,25,75,75); //start area
 fill(0,0,0);
 text("Make your way to the Red Square",250,150);
 fill(255,0,0);
 text("RED SQUARE",250,175);
 fill(0,0,0);
 text("to win",250,200);
}

Re: need help making solid walls for a maze and more
Reply #1 - Apr 29th, 2010, 2:40am
 
I would make a class for the walls, to ease the checking of hit.
But in the current state of your program, a way to do that is to check the pixel color (get()) in the direction of moving, at a distance of 16 (make that a constant, like BALL_RADIUS, so you can change it later in one place only, and use it in your ball drawing).
Re: need help making solid walls for a maze and more
Reply #2 - Apr 29th, 2010, 1:20pm
 
Thanks for replying PhiLho. I understand what your saying, but I'm still kind of new at Processing and don't really know how to implement that into my program or know what the full code for it looks like. Embarrassed
Re: need help making solid walls for a maze and more
Reply #3 - Apr 29th, 2010, 1:24pm
 
int x; // our player's x-position
int y; // our player's y-position
int s; // our player's visual size
int half_size; // // half our player's visual size
int speed; // the speed at which we are moving

// our displayed level
PImage level;

// our collision map, basically a two-dimensional array (an array of arrays)
// the information of where we are able to go is encoded in the following way
// where '1' is 'true' and '0' is 'false'

/*
*  0 1 0 0 0 0 0
*  0 1 0 0 0 0 0
*  0 1 1 1 1 1 1  ---> a way! Smiley
*  0 1 0 0 0 0 0
*  0 1 0 0 0 0 0
*/

// this matrix will have the same dimension as our level image
boolean[][] collisionMap;

void setup() {
 
 size(200, 200);
 background(0);
 // draw our player from the center
 rectMode(CENTER);
 
 // start in the center of the canvas
 x = width/2;
 y = height/2;
 // size of 10, half_size will be 5
 s = 10;
 half_size = s / 2;
 // moving 2 pixels each time we press a key
 speed = 2;
 
 // load our lovely level image
 level = loadImage("level.gif");
 // load our black & white pixel collision map image
 PImage colMapImage = loadImage("collisionMap.gif");
 // generate our collision map as an two-dimensional boolean array
 collisionMap = new boolean[colMapImage.width][colMapImage.height];
 
 // our collision map is encoded only in black & white pixels
 // so we get them for comparison
 color black = color(0);
 color white = color(255);
 
 // go through each row in our collision map image
 for (int i = 0; i < colMapImage.width; i++) {
   // go through each column in our collision map image
   for (int j = 0; j < colMapImage.height; j++) {
     
     // get the color value of the pixel at our current position
     color c = colMapImage.get(i, j);
     // if the pixel is black
     if (c == black) {
       // we can't go there
       collisionMap[i][j] = false;
     }
     // if it is white
     else if (c == white) {
       // it's good to go
       collisionMap[i][j] = true;
     }
     else {
       // should happen Wink
       println("Whoops! We shouldn't have colors other than black and white in our collision map!");
     }

   }
 }
}

void draw() {
 background(0);
 // show our level
 image(level, 0, 0);
 
 // draw our player at it's current position
 fill(255, 0, 0);
 noStroke();
 rect(x, y, 10, 10);
}

/*
*  this is where we actually move
*  the strategy is as follows:
*  - pretend we would move in the planned direction
*    (left, right, up or down)
*  - then check if at this new position, all 4 corners
*    of our rectangle would be within our allowed area
*  - if that's the case: move
*/
void keyPressed() {

 // would each corner of the next step be within our boundaries?
 // default: no!
 boolean up_left = false;
 boolean up_right = false;
 boolean down_right = false;
 boolean down_left = false;

 if (keyCode == LEFT) {
   // check first if we are still within our canvas
   if (x >= half_size + speed)
   {
     // check all four corners to see if they would be with the allowed area
     up_left = collisionMap[x - speed - half_size][y - half_size];
     up_right = collisionMap[x - speed + half_size][y - half_size];
     down_right = collisionMap[x - speed + half_size][y + half_size];
     down_left = collisionMap[x - speed - half_size][y + half_size];
     
     // if that's the case for each corner
     if (up_left && up_right && down_right && down_left) {
       // move
       x -= speed;
     }
   }
 }
 
 // this basically is repeated for all possible ways to go…
 if (keyCode == RIGHT) {
   if (x <= width - half_size - speed)
   {
     up_left = collisionMap[x + speed - half_size][y - half_size];
     up_right = collisionMap[x + speed + half_size][y - half_size];
     down_right = collisionMap[x + speed + half_size][y + half_size];
     down_left = collisionMap[x + speed - half_size][y + half_size];

     if (up_left && up_right && down_right && down_left) {
       x += speed;
     }
   }
 }

 if (keyCode == UP) {
   if (y >= half_size + speed)
   {
     up_left = collisionMap[x - half_size][y - speed - half_size];
     up_right = collisionMap[x + half_size][y - speed - half_size];
     down_right = collisionMap[x + half_size][y - speed + half_size];
     down_left = collisionMap[x - half_size][y - speed + half_size];

     if (up_left && up_right && down_right && down_left) {
       y -= speed;
     }
   }
 }

 if (keyCode == DOWN) {
   if (y <= height - half_size - speed)
   {
     up_left = collisionMap[x - half_size][y + speed - half_size];
     up_right = collisionMap[x + half_size][y + speed - half_size];
     down_right = collisionMap[x + half_size][y + speed + half_size];
     down_left = collisionMap[x - half_size][y + speed + half_size];

     if (up_left && up_right && down_right && down_left) {
       y += speed;
     }
   }
 }
}



There we go, you just need to create a data folder in your sketchfolder and then put a level.gif and a collisionmap.gif. Its important that there is only black and white, black for the walls.
greets
Page Index Toggle Pages: 1