Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- public static void main(String[] args) {
- 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))
- {
- int mX = (int) Math.floor(input.getMouseX() / 4);
- int mY = (int) Math.floor(input.getMouseY() / 4);
- 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))
- {
- int mX = (int) Math.floor(input.getMouseX() / 4);
- int mY = (int) Math.floor(input.getMouseY() / 4);
- 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;
- }
- public void render(GameContainer container, Graphics g) throws SlickException
- {
- g.setColor(Color.black);
- 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:
- {
- g.setColor(new Color(255, 100, 100));
- break;
- }
- case 1:
- {
- g.setColor(Color.white);
- break;
- }
- case 2:
- {
- g.setColor(Color.orange);
- break;
- }
- case 3:
- {
- g.setColor(Color.blue);
- break;
- }
- case 4:
- {
- g.setColor(Color.red);
- break;
- }
- }
- g.fillRect(x * 4, y * 4, 4, 4);
- }
- }
- }
- }
- private void start()
- {
- terrain = new int[width][height];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement