dah01
|
 |
«
Posted
2011-12-23 07:25:04 » |
|
My Helper Version: http://pastebin.com/VCjU3NnK Original: http://pastebin.com/n4b21FWg Reddit Thread: http://www.reddit.com/r/gamedev/comments/nn8hb/java_2d_island_generator_first_real_project_since/A guy over at reddit.com/r/gamedev posted the source for an island generator: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| package polytester;
import org.newdawn.slick.AppGameContainer; import org.newdawn.slick.BasicGame; import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.Input; import org.newdawn.slick.geom.Polygon; import org.newdawn.slick.SlickException; import org.newdawn.slick.Color; import java.util.Random; import java.awt.Font; import java.util.Scanner;
public class hola extends BasicGame { Polygon[][] rand = new Polygon[25][25]; int[][][] grid = new int[5][25][25]; int tempax = 0, tempay = 0; Random r = new Random(); int time; Scanner input = new Scanner(System.in); Font font = new Font("Helvetica", Font.BOLD, 30); int x = 20, y = 20, number = 100, phase = 1; int xc = 32, yc = 24; String print; boolean next; int z, p, tempx, tempy; int timer; int checksum; int chance; int cursorx = 200, cursory = 200, cx = 1, cy = 1; int sex = 0; int countd=0;
public hola() { super("polygontest"); } public void init(GameContainer container) throws SlickException { container.setVSync(true); for(int z = 0; z < 25; z++){ for(int p = 0; p < 25; p++){
rand[z][p] = new Polygon(new float[]{ 0,0, 0,y, x,y, x,0 }); chance = r.nextInt(2); grid[0][z][p] = chance; if(chance == 1){ rand[z][p].setLocation(z*x, p*y); }else{ rand[z][p].setLocation(900, 900); } tempx++; } tempy++; tempx=0; } for(int dope = 0; dope < 4; dope++){ for(int mattb = 0; mattb < 25; mattb++){ for(int mattx = 0; mattx < 25; mattx++){ if(mattb-1 >= 0 && mattb+1 <=24 && mattx-1 >= 0 && mattx+1 <= 24){ checksum=grid[dope][mattb-1][mattx-1]+grid[dope][mattb-1][mattx]+grid[dope][mattb-1][mattx+1]+ grid[dope][mattb][mattx-1]+grid[dope][mattb][mattx+1]+grid[dope][mattb+1][mattx-1]+ grid[dope][mattb+1][mattx]+grid[dope][mattb+1][mattx+1]; if(checksum >= 4 && grid[dope][mattb][mattx] == 1){ grid[dope+1][mattb][mattx] = 1; } if(checksum >= 5){ grid[dope+1][mattb][mattx] = 1; } } } } }
for(int ax = 0; ax < 25; ax++){ for(int ay = 0; ay < 25; ay++){ if(grid[4][ax][ay] == 1){ rand[ax][ay].setLocation(ax*x, ay*y); tempax++; } } tempay++; tempax=0; } }
public void update(GameContainer container, int delta) { timer--; if(container.getInput().isKeyDown(Input.KEY_SPACE) && timer < 0){ timer = 30; countd++; if(countd>4)countd=0; for(int ax = 0; ax < 25; ax++){ for(int ay = 0; ay < 25; ay++){ if(grid[countd][ax][ay] == 1){ rand[ax][ay].setLocation(ax*x, ay*y); tempax++; } } tempay++; tempax=0; } } } public void render(GameContainer container, Graphics g) {
for(int d = 0; d < 25; d++){ for(int o = 0; o < 25; o++){ if(grid[countd][d][o]==1){ g.drawString("#", d*x, o*y); System.out.print("#"); }else{ g.drawString(" ", d*x, o*y); System.out.print(" "); } } System.out.println(); } System.out.println("BREAKBREAKBREAK"); } public static void main(String[] argv) throws SlickException { AppGameContainer container = new AppGameContainer(new hola(), 500, 500, false); container.setShowFPS(false); container.start(); } } |
I attempted to make it a little easier to use but I have no idea what witchcraft was used in it's creation so I'm not sure how to tidy up the generation section but at least it's easy to use now. If you're just looking for a 2d island map you want to call get2dClarifiedMap(width,height) http://pastebin.com/VCjU3NnK1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| package com.hadesvine.tiledgame.examples;
import org.newdawn.slick.geom.Polygon; import java.util.Random;
public class MapHelper {
public static final int DEFAULTWIDTH = 32; public static final int DEFAULTHEIGHT = 25; public static final int CLARIFICATION = 4;
public MapHelper() { }
public static int[][][] getNewMap(int width, int height, int clarification) { Polygon[][] rand = new Polygon[width][height]; int[][][] grid; int tempax = 0, tempay = 0; Random r = new Random(); int x = 20; int y = 20; int tempx = 0; int tempy = 0; int checksum = 0; int chance = 0; grid = new int[5][width][height]; for (int z = 0; z < width; z++) { for (int p = 0; p < height; p++) { rand[z][p] = new Polygon(new float[]{ 0, 0, 0, y, x, y, x, 0 }); chance = r.nextInt(2); grid[0][z][p] = chance; if (chance == 1) { rand[z][p].setLocation(z * x, p * y); } else { rand[z][p].setLocation(900, 900); } tempx++; } tempy++; tempx = 0; } for (int i = 0; i < clarification; i++) { for (int mattb = 0; mattb < width; mattb++) { for (int mattx = 0; mattx < height; mattx++) { if (mattb - 1 >= 0 && mattb + 1 < width && mattx - 1 >= 0 && mattx + 1 < height) { checksum = grid[i][mattb - 1][mattx - 1] + grid[i][mattb - 1][mattx] + grid[i][mattb - 1][mattx + 1] + grid[i][mattb][mattx - 1] + grid[i][mattb][mattx + 1] + grid[i][mattb + 1][mattx - 1] + grid[i][mattb + 1][mattx] + grid[i][mattb + 1][mattx + 1];
if (checksum >= clarification && grid[i][mattb][mattx] == 1) { grid[i + 1][mattb][mattx] = 1; }
if (checksum >= 5) { grid[i + 1][mattb][mattx] = 1; } } } } } for (int ax = 0; ax < width; ax++) { for (int ay = 0; ay < height; ay++) { if (grid[CLARIFICATION][ax][ay] == 1) { rand[ax][ay].setLocation(ax * x, ay * y); tempax++; } } tempay++; tempax = 0; } return grid ; } public static void main(String[] args) { printRandomMapToConsole(800,600,4); } public static void printRandomMapToConsole(int width, int height, int clarification){ int [][][]grid = getNewMap(width, height,clarification); for (int d = 0; d < height; d++) { for (int o = 0; o < width; o++) { if (grid[clarification][d][o] == 1) { System.out.print("#"); } else { System.out.print(" "); } } System.out.println(); } } public static void printRandomMapToConsole(int width, int height){ int [][][]grid = getNewMap(width, height,CLARIFICATION); for (int d = 0; d < height; d++) { for (int o = 0; o < width; o++) { if (grid[CLARIFICATION][d][o] == 1) { System.out.print("#"); } else { System.out.print(" "); } } System.out.println(); } } public static int[][] getClarified2DIslandMap(int width, int height){ int [][][] gridWithClarification = getNewMap(width,height,4); int [][] clarifiedGrid = gridWithClarification[4]; return clarifiedGrid; } } |
As a note, this works much better on smaller maps.
|