Skip to content

Instantly share code, notes, and snippets.

@jeffThompson
Created May 12, 2013 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffThompson/5564439 to your computer and use it in GitHub Desktop.
Save jeffThompson/5564439 to your computer and use it in GitHub Desktop.
Simple Processing sketch to generate algorithmic islands
/*
SMELL GAME LEVEL TESTS
Jeff Thompson | 2013 | www.jeffreythompson.org
Ideas:
+ Run forest fire simulation to set terrain
*/
int w = 200;
int h = 200;
int maxInsetMove = 1; // amount the land can move inward per step (up to edgeThresh)
int edgeThresh = 200;
int[][] level = new int[h][w];
void setup() {
size(w, h);
for (int i=0; i<120; i++) {
if (i%20 == 0) {
maxInsetMove += 1;
}
createLevel(w, h, maxInsetMove, edgeThresh);
displayLevel(color(70, 150, 220), color(75, 95, 40), true, i);
}
}
void createLevel(int w, int h, int maxInsetMove, int edgeThresh) {
// clear level
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
level[y][x] = -1;
}
}
// top
int rx = 0;
int ry = 0;
while (rx < w) {
for (int y=ry; y>=0; y--) {
level[y][rx] = 1;
}
rx += int(random(0, 2));
rx = constrain(rx, 0, w);
ry += int(random(-maxInsetMove, maxInsetMove));
ry = constrain(ry, 0, edgeThresh);
}
// bottom
rx = 0;
ry = height-1;
while (rx < w) {
for (int y=ry; y<h; y++) {
level[y][rx] = 1;
}
rx += int(random(0, 2));
rx = constrain(rx, 0, w);
ry += int(random(-maxInsetMove, maxInsetMove));
ry = constrain(ry, height-edgeThresh, height-1);
}
// left
rx = 0;
ry = 0;
while (ry < h) {
for (int x=rx; x>=0; x--) {
level[ry][x] = 1;
}
ry += int(random(0, 2));
ry = constrain(ry, 0, h);
rx += int(random(-maxInsetMove, maxInsetMove));
rx = constrain(rx, 0, edgeThresh);
}
// right
rx = w-1;
ry = 0;
while (ry < h) {
for (int x=rx; x<w; x++) {
level[ry][x] = 1;
}
ry += int(random(0, 2));
ry = constrain(ry, 0, h);
rx += int(random(-maxInsetMove, maxInsetMove));
rx = constrain(rx, w-edgeThresh, w-1);
}
}
void displayLevel(color sea, color land, boolean saveIt, int iteration) {
loadPixels();
for (int i=0; i<pixels.length; i++) {
if (level[i/width][i%width] == 1) {
pixels[i] = sea;
}
else {
pixels[i] = land;
}
}
updatePixels();
if (saveIt) {
save("output/island_" + iteration + ".png");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment