Help with random map generator

This is my first time using processing, and I’m creating a project for a class. I want to make a random map generator using 100x100 pixel image tiles. The idea is that the tiles are placed in random spots when certain keys are pressed. What I’m trying to figure out is how to make these images appear in 100 pixel intervals on the 1200x700 map. Is it possible to use the random function, or something similar, on a string of coordinates? For example, instead of landing on any number between 0 and 600, it can only land on 0, 100, 200, 300, 400, 500, or 600? It seems simple enough in my head, but I’ve been trying different things for hours with no results.

Here is the code I have so far.

//forest
PImage forest0, forest1, forest2, forest3, forest4, forest5, forest6, forest7, forest8, forest9;
PImage [] forestArray = new PImage [10];
//grass
PImage grass0, grass1, grass2, grass3, grass4, grass5, grass6, grass7, grass8, grass9;
PImage [] grassArray = new PImage [10];
//interest
PImage interest0, interest1, interest2, interest3, interest4, interest5, interest6, interest7, interest8, interest9;
PImage [] interestArray = new PImage [10];
//village
PImage village0, village1, village2, village3, village4, village5, village6, village7, village8, village9;
PImage [] villageArray = new PImage [10];
//water
PImage water0, water1, water2, water3, water4, water5, water6, water7, water8, water9;
PImage [] waterArray = new PImage [10];
//text box
int state = 0;
String result="";

void setup(){
  size(1200,750);
  background(101, 143, 70);
  //forest
  forest0 = loadImage("forest0.png");
  forest1 = loadImage("forest1.png");
  forest2 = loadImage("forest2.png");
  forest3 = loadImage("forest3.png");
  forest4 = loadImage("forest4.png");
  forest5 = loadImage("forest5.png");
  forest6 = loadImage("forest6.png");
  forest7 = loadImage("forest7.png");
  forest8 = loadImage("forest8.png");
  forest9 = loadImage("forest9.png");
  forestArray[0] = forest0;
  forestArray[1] = forest1;
  forestArray[2] = forest2;
  forestArray[3] = forest3;
  forestArray[4] = forest4;
  forestArray[5] = forest5;
  forestArray[6] = forest6;
  forestArray[7] = forest7;
  forestArray[8] = forest8;
  forestArray[9] = forest9;
  //grass
  grass0 = loadImage("grass0.png");
  grass1 = loadImage("grass1.png");
  grass2 = loadImage("grass2.png");
  grass3 = loadImage("grass3.png");
  grass4 = loadImage("grass4.png");
  grass5 = loadImage("grass5.png");
  grass6 = loadImage("grass6.png");
  grass7 = loadImage("grass7.png");
  grass8 = loadImage("grass8.png");
  grass9 = loadImage("grass9.png");
  grassArray[0] = grass0;
  grassArray[1] = grass1;
  grassArray[2] = grass2;
  grassArray[3] = grass3;
  grassArray[4] = grass4;
  grassArray[5] = grass5;
  grassArray[6] = grass6;
  grassArray[7] = grass7;
  grassArray[8] = grass8;
  grassArray[9] = grass9;
  //interest
  interest0 = loadImage("interest0.png");
  interest1 = loadImage("interest1.png");
  interest2 = loadImage("interest2.png");
  interest3 = loadImage("interest3.png");
  interest4 = loadImage("interest4.png");
  interest5 = loadImage("interest5.png");
  interest6 = loadImage("interest6.png");
  interest7 = loadImage("interest7.png");
  interest8 = loadImage("interest8.png");
  interest9 = loadImage("interest9.png");
  interestArray[0] = interest0;
  interestArray[1] = interest1;
  interestArray[2] = interest2;
  interestArray[3] = interest3;
  interestArray[4] = interest4;
  interestArray[5] = interest5;
  interestArray[6] = interest6;
  interestArray[7] = interest7;
  interestArray[8] = interest8;
  interestArray[9] = interest9;
  //village
  village0 = loadImage("village0.png");
  village1 = loadImage("village1.png");
  village2 = loadImage("village2.png");
  village3 = loadImage("village3.png");
  village4 = loadImage("village4.png");
  village5 = loadImage("village5.png");
  village6 = loadImage("village6.png");
  village7 = loadImage("village7.png");
  village8 = loadImage("village8.png");
  village9 = loadImage("village9.png");
  villageArray[0] = village0;
  villageArray[1] = village1;
  villageArray[2] = village2;
  villageArray[3] = village3;
  villageArray[4] = village4;
  villageArray[5] = village5;
  villageArray[6] = village6;
  villageArray[7] = village7;
  villageArray[8] = village8;
  villageArray[9] = village9;
  //water
  water0 = loadImage("water0.png");
  water1 = loadImage("water1.png");
  water2 = loadImage("water2.png");
  water3 = loadImage("water3.png");
  water4 = loadImage("water4.png");
  water5 = loadImage("water5.png");
  water6 = loadImage("water6.png");
  water7 = loadImage("water7.png");
  water8 = loadImage("water8.png");
  water9 = loadImage("water9.png");
  waterArray[0] = water0;
  waterArray[1] = water1;
  waterArray[2] = water2;
  waterArray[3] = water3;
  waterArray[4] = water4;
  waterArray[5] = water5;
  waterArray[6] = water6;
  waterArray[7] = water7;
  waterArray[8] = water8;
  waterArray[9] = water9;
}

void draw() {
  
  //text box
  fill(141, 173, 40);
  noStroke();
  rect(0, 700, 1200, 50);
  
  switch (state) {
  case 0:
  fill(0);
  text("A new world awaits... \n"+result, 25, 725);
  break;
  
  case 1:
  fill(0);
  text ("Welcome, traveler. \n"+result, 25, 725);
  break;
  }
  
}

void keyPressed() {
  //text box
  if (key==ENTER||key==RETURN) {
    state++;
  } else
  result = result + key;
  if (key == BACKSPACE) {
  background(101, 143, 70);
  }
  //forest
  if (key == 'q' || key == 'Q' || key == 'y' || key == 'Y' || key == 'a' || key == 'A' || key == 'h' || key == 'H' || key == 'x' || key == 'X' || key == 'm' || key == 'M'){
  image(forestArray[int(random(forestArray.length))], random (0,1100), random(0, 600));
  }
  //grass
  if (key == 'w' || key == 'W' || key == 'u' || key == 'U' || key == 's' || key == 'S' || key == 'j' || key == 'J' || key == 'c' || key == 'C'){
  image(grassArray[int(random(grassArray.length))], random(0, 1100), random(0, 600));
  }
  //interest
  if (key == 'e' || key == 'E' || key == 'i' || key == 'I' || key == 'd' || key == 'D' || key == 'k' || key == 'K' || key == 'v' || key == 'V'){
  image(interestArray[int(random(interestArray.length))], random(0, 1100), random(0, 600));
  }
  //village
  if (key == 'r' || key == 'R' || key == 'o' || key == 'O' || key == 'f' || key == 'F' || key == 'l' || key == 'L' || key == 'b' || key == 'B'){
  image(villageArray[int(random(villageArray.length))], random(0, 1100), random(0, 600));
  }
  //water
  if (key == 't' || key == 'T' || key == 'p' || key == 'P' || key == 'g' || key == 'G' || key == 'z' || key == 'Z' || key == 'n' || key == 'N'){
  image(waterArray[int(random(waterArray.length))], random(0, 1100), random(0, 600));
  }
}

Thank you!

1 Like

100 * int(random(6));

That said, you don’t need two images for every image. Just do:

PImage[] forestArray = new PImage[9];
forestArray[0] = loadImage(“forest0.png”);
forestArray[1] = loadImage(“forest1.png”);
///...etc
2 Likes

ok, too late, still it might help:

// random tile? / map generator
// 
int j=0, k=0;

void setup() {
  size(1100, 600);
  noStroke();
}

void draw() {
  rect(j, k, 100, 100);
}
void keyPressed() {
  if ( key == ' ') {
    j = 100*int(random(0, 11));
    k = 100*int(random(0, 6));
    fill(random(256), random(256), random(256));
  }
}

1 Like