Advertisement
Guest User

Epitaph64

a guest
Jun 3rd, 2009
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.18 KB | None | 0 0
  1. package bufferedterrain;
  2.  
  3. import org.newdawn.slick.AppGameContainer;
  4. import org.newdawn.slick.BasicGame;
  5. import org.newdawn.slick.Color;
  6. import org.newdawn.slick.GameContainer;
  7. import org.newdawn.slick.Graphics;
  8. import org.newdawn.slick.Input;
  9. import org.newdawn.slick.SlickException;
  10.  
  11. public class Main extends BasicGame {
  12.  
  13.     Main()
  14.     {
  15.         super("Terrain Test");
  16.     }
  17.  
  18.     private Input input;
  19.    
  20.     private int[][] terrain;
  21.    
  22.     public MersenneTwisterFast mt;
  23.  
  24.     private int width = 256;
  25.     private int height = 192;
  26.  
  27.     public static void main(String[] args) {
  28.         try
  29.         {
  30.             AppGameContainer app = new AppGameContainer(new Main());
  31.             app.setDisplayMode(1024, 768, false);
  32.             app.setFullscreen(false);
  33.             app.setShowFPS(false);
  34.             app.setVSync(true);
  35.             app.setClearEachFrame(false);
  36.             app.start();
  37.         }
  38.         catch (SlickException e)
  39.         {
  40.             e.printStackTrace();
  41.         }
  42.     }
  43.  
  44.     @Override
  45.     public void init(GameContainer container) throws SlickException
  46.     {
  47.         input = container.getInput();
  48.         mt = new MersenneTwisterFast();
  49.         start();
  50.     }
  51.  
  52.     @Override
  53.     public void update(GameContainer container, int delta) throws SlickException
  54.     {
  55.         if (input.isKeyPressed(Input.KEY_ESCAPE)) container.exit();
  56.         if (input.isKeyPressed(Input.KEY_F2)) start();
  57.         if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON))
  58.         {
  59.             int mX = (int) Math.floor(input.getMouseX() / 4);
  60.             int mY = (int) Math.floor(input.getMouseY() / 4);
  61.             if (mX >= 1 && mX < width - 1 && mY >= 1 && mY < height - 1)
  62.             {
  63.                 terrain[mX][mY] = -1;
  64.                 terrain[mX + 1][mY] = -1;
  65.                 terrain[mX + 1][mY + 1] = -1;
  66.                 terrain[mX][mY + 1] = -1;
  67.             }
  68.         }
  69.         if (input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON))
  70.         {
  71.             int mX = (int) Math.floor(input.getMouseX() / 4);
  72.             int mY = (int) Math.floor(input.getMouseY() / 4);
  73.             if (mX >= 1 && mX < width - 1 && mY >= 1 && mY < height - 1)
  74.             {
  75.                 terrain[mX][mY] = 0;
  76.                 terrain[mX + 1][mY] = 0;
  77.                 terrain[mX + 1][mY + 1] = 0;
  78.                 terrain[mX][mY + 1] = 0;
  79.             }
  80.         }
  81.         for (int x = 0; x < width; x++)
  82.         {
  83.             for (int y = height - 2; y >= 0; y--)
  84.             {
  85.                 if (terrain[x][y] > 0)
  86.                 {
  87.                     if (terrain[x][y+1] == 0)
  88.                     {
  89.                         terrain[x][y+1] = terrain[x][y];
  90.                         terrain[x][y] = 0;
  91.                     }
  92.                 }
  93.                 if (mt.nextBoolean())
  94.                 {
  95.                     if (x + 1 < width)
  96.                     {
  97.                         if (terrain[x+1][y] == 0 && terrain[x][y] > 0)
  98.                         {
  99.                             terrain[x+1][y] = terrain[x][y];
  100.                             terrain[x][y] = 0;
  101.                         }
  102.                     }
  103.                     if (x - 1 >= 0)
  104.                     {
  105.                         if (terrain[x-1][y] == 0 && terrain[x][y] > 0)
  106.                         {
  107.                             terrain[x-1][y] = terrain[x][y];
  108.                             terrain[x][y] = 0;
  109.                         }
  110.                     }
  111.                 }
  112.             }
  113.         }
  114.         terrain[32 + mt.nextInt(32)-16][0] = 1;
  115.         terrain[96 + mt.nextInt(32)-16][0] = 2;
  116.         terrain[160 + mt.nextInt(32)-16][0] = 3;
  117.         terrain[224 + mt.nextInt(32)-16][0] = 4;
  118.  
  119.     }
  120.  
  121.     public void render(GameContainer container, Graphics g) throws SlickException
  122.     {
  123.         g.setColor(Color.black);
  124.         g.fillRect(0, 0, 1024, 768);
  125.         for (int y = 0; y < height; y++)
  126.         {
  127.             for (int x = 0; x < width; x++)
  128.             {
  129.                 if (terrain[x][y] != 0)
  130.                 {
  131.                     switch(terrain[x][y])
  132.                     {
  133.                         case -1:
  134.                         {
  135.                             g.setColor(new Color(255, 100, 100));
  136.                             break;
  137.                         }
  138.                         case 1:
  139.                         {
  140.                             g.setColor(Color.white);
  141.                             break;
  142.                         }
  143.                         case 2:
  144.                         {
  145.                             g.setColor(Color.orange);
  146.                             break;
  147.                         }
  148.                         case 3:
  149.                         {
  150.                             g.setColor(Color.blue);
  151.                             break;
  152.                         }
  153.                         case 4:
  154.                         {
  155.                             g.setColor(Color.red);
  156.                             break;
  157.                         }
  158.                     }
  159.                     g.fillRect(x * 4, y * 4, 4, 4);
  160.                 }
  161.             }
  162.         }
  163.     }
  164.  
  165.     private void start()
  166.     {
  167.         terrain = new int[width][height];
  168.     }
  169. }
  170.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement