Advertisement
Guest User

Basic Game Crashes

a guest
Feb 4th, 2015
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.43 KB | None | 0 0
  1. package com.circlewars.game;
  2.  
  3. import org.newdawn.slick.*;
  4. import org.newdawn.slick.tiled.TiledMap;
  5.  
  6. public class BasicEngine extends BasicGame implements Runnable
  7. {
  8.  
  9.     private TiledMap grassMap;
  10.     private Animation sprite, up, down, left, right;
  11.     private float x = 32f, y = 32f;
  12.  
  13.     /** The collision map indicating which tiles block movement - generated based on tile properties */
  14.     private boolean[][] blocked;
  15.     private static final int SIZE = 32;
  16.  
  17.  
  18.     public BasicEngine()
  19.     {
  20.         super("Test game");
  21.     }
  22.  
  23.     @Override
  24.     public void run()
  25.     {
  26.         try
  27.         {
  28.             AppGameContainer app = new AppGameContainer(new BasicEngine());
  29.             app.setDisplayMode(1280, 720, false);
  30.             app.start();
  31.         }
  32.         catch (SlickException e)
  33.         {
  34.             e.printStackTrace();
  35.         }
  36.     }
  37.  
  38.     @Override
  39.     public void init(GameContainer container) throws SlickException
  40.     {
  41.         Image [] movementUp = {new Image("com/circlewars/game/sprites/wmg1_bk1.png"), new Image("com/circlewars/game/sprites/wmg1_bk2.png")};
  42.         Image [] movementDown = {new Image("com/circlewars/game/sprites/wmg1_fr1.png"), new Image("com/circlewars/game/sprites/wmg1_fr2.png")};
  43.         Image [] movementLeft = {new Image("com/circlewars/game/sprites/wmg1_lf1.png"), new Image("com/circlewars/game/sprites/wmg1_lf2.png")};
  44.         Image [] movementRight = {new Image("com/circlewars/game/sprites/wmg1_rt1.png"), new Image("com/circlewars/game/sprites/wmg1_rt2.png")};
  45.         int [] duration = {300, 300};
  46.         grassMap = new TiledMap("com/circlewars/game/grassmap.tmx");
  47.  
  48.           /*
  49.           * false variable means do not auto update the animation.
  50.           * By setting it to false animation will update only when
  51.           * the user presses a key.
  52.           */
  53.         up = new Animation(movementUp, duration, false);
  54.         down = new Animation(movementDown, duration, false);
  55.         left = new Animation(movementLeft, duration, false);
  56.         right = new Animation(movementRight, duration, false);
  57.  
  58.         // Original orientation of the sprite. It will look right.
  59.         sprite = right;
  60.  
  61.         blocked = new boolean[grassMap.getWidth()][grassMap.getHeight()];
  62.  
  63.         for (int xAxis=0;xAxis<grassMap.getWidth(); xAxis++)
  64.         {
  65.             for (int yAxis=0;yAxis<grassMap.getHeight(); yAxis++)
  66.             {
  67.                 int tileID = grassMap.getTileId(xAxis, yAxis, 0);
  68.                 String value = grassMap.getLayerProperty(tileID, "collision", "false");
  69.                 if ("true".equals(value))
  70.                 {
  71.                     blocked[xAxis][yAxis] = true;
  72.                 }
  73.             }
  74.         }
  75.     }
  76.  
  77.     @Override
  78.     public void update(GameContainer container, int delta) throws SlickException
  79.     {
  80.         Input input = container.getInput();
  81.         float fdelta = delta*0.1f;
  82.         if (input.isKeyDown(Input.KEY_UP))
  83.         {
  84.             sprite = up;
  85.             if (!(isBlocked(x, y - fdelta) || isBlocked(x+SIZE-1, y - fdelta)))
  86.              {
  87.                 sprite.update(delta);
  88. // The lower the delta the slowest the sprite will animate.
  89.                 y -= fdelta;
  90.             }
  91.         }
  92.         else if (input.isKeyDown(Input.KEY_DOWN))
  93.         {
  94.             sprite = down;
  95.             if (!(isBlocked(x, y + SIZE + fdelta) || isBlocked(x+SIZE-1, y + SIZE + fdelta)))
  96.             {
  97.                 sprite.update(delta);
  98.                 y += fdelta;
  99.             }
  100.         }
  101.         else if (input.isKeyDown(Input.KEY_LEFT))
  102.         {
  103.             sprite = left;
  104.             if (!(isBlocked(x - fdelta, y) || isBlocked(x - fdelta, y+SIZE-1)))
  105.             {
  106.                 sprite.update(delta);
  107.                 x -= fdelta;
  108.             }
  109.         }
  110.         else if (input.isKeyDown(Input.KEY_RIGHT))
  111.         {
  112.             sprite = right;
  113.             if (!(isBlocked(x + SIZE + fdelta, y) || isBlocked(x + SIZE + fdelta, y+SIZE-1)))
  114.             {
  115.                 sprite.update(delta);
  116.                 x += fdelta;
  117.             }
  118.         }
  119.     }
  120.  
  121.     @Override
  122.     public void render(GameContainer container, Graphics g) throws SlickException {
  123.         grassMap.render(0, 0);
  124.         sprite.draw((int)x, (int)y);
  125.     }
  126.    
  127.     private boolean isBlocked(float x, float y)
  128.     {
  129.         int xBlock = (int)x / SIZE;
  130.         int yBlock = (int)y / SIZE;
  131.         return blocked[xBlock][yBlock];
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement