Posted by Epitaph64 on Thu 4 Jun 01:37
report abuse | download | new post
- package bufferedterrain;
- import org.newdawn.slick.AppGameContainer;
- import org.newdawn.slick.BasicGame;
- import org.newdawn.slick.Color;
- import org.newdawn.slick.GameContainer;
- import org.newdawn.slick.Graphics;
- import org.newdawn.slick.Input;
- import org.newdawn.slick.SlickException;
- public class Main extends BasicGame {
- Main()
- {
- super("Terrain Test");
- }
- private Input input;
- private int[][] terrain;
- public MersenneTwisterFast mt;
- private int width = 256;
- private int height = 192;
- try
- {
- AppGameContainer app = new AppGameContainer(new Main());
- app.setDisplayMode(1024, 768, false);
- app.setFullscreen(false);
- app.setShowFPS(false);
- app.setVSync(true);
- app.setClearEachFrame(false);
- app.start();
- }
- catch (SlickException e)
- {
- e.printStackTrace();
- }
- }
- @Override
- public void init(GameContainer container) throws SlickException
- {
- input = container.getInput();
- mt = new MersenneTwisterFast();
- start();
- }
- @Override
- public void update(GameContainer container, int delta) throws SlickException
- {
- if (input.isKeyPressed(Input.KEY_ESCAPE)) container.exit();
- if (input.isKeyPressed(Input.KEY_F2)) start();
- if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON))
- {
- if (mX >= 1 && mX < width - 1 && mY >= 1 && mY < height - 1)
- {
- terrain[mX][mY] = -1;
- terrain[mX + 1][mY] = -1;
- terrain[mX + 1][mY + 1] = -1;
- terrain[mX][mY + 1] = -1;
- }
- }
- if (input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON))
- {
- if (mX >= 1 && mX < width - 1 && mY >= 1 && mY < height - 1)
- {
- terrain[mX][mY] = 0;
- terrain[mX + 1][mY] = 0;
- terrain[mX + 1][mY + 1] = 0;
- terrain[mX][mY + 1] = 0;
- }
- }
- for (int x = 0; x < width; x++)
- {
- for (int y = height - 2; y >= 0; y--)
- {
- if (terrain[x][y] > 0)
- {
- if (terrain[x][y+1] == 0)
- {
- terrain[x][y+1] = terrain[x][y];
- terrain[x][y] = 0;
- }
- }
- if (mt.nextBoolean())
- {
- if (x + 1 < width)
- {
- if (terrain[x+1][y] == 0 && terrain[x][y] > 0)
- {
- terrain[x+1][y] = terrain[x][y];
- terrain[x][y] = 0;
- }
- }
- if (x - 1 >= 0)
- {
- if (terrain[x-1][y] == 0 && terrain[x][y] > 0)
- {
- terrain[x-1][y] = terrain[x][y];
- terrain[x][y] = 0;
- }
- }
- }
- }
- }
- terrain[32 + mt.nextInt(32)-16][0] = 1;
- terrain[96 + mt.nextInt(32)-16][0] = 2;
- terrain[160 + mt.nextInt(32)-16][0] = 3;
- terrain[224 + mt.nextInt(32)-16][0] = 4;
- }
- {
- g.fillRect(0, 0, 1024, 768);
- for (int y = 0; y < height; y++)
- {
- for (int x = 0; x < width; x++)
- {
- if (terrain[x][y] != 0)
- {
- switch(terrain[x][y])
- {
- case -1:
- {
- break;
- }
- case 1:
- {
- break;
- }
- case 2:
- {
- break;
- }
- case 3:
- {
- break;
- }
- case 4:
- {
- break;
- }
- }
- g.fillRect(x * 4, y * 4, 4, 4);
- }
- }
- }
- }
- private void start()
- {
- terrain = new int[width][height];
- }
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.