Advertisement
Guest User

Awww

a guest
Feb 2nd, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1.  
  2. import org.newdawn.slick.Animation;
  3. import org.newdawn.slick.GameContainer;
  4. import org.newdawn.slick.Graphics;
  5. import org.newdawn.slick.Input;
  6. import org.newdawn.slick.SlickException;
  7. import org.newdawn.slick.SpriteSheet;
  8.  
  9. public class Player extends Entity {
  10.    
  11.     public Input input;
  12.     public String playerPath = "/res/player.png";
  13.     public SpriteSheet sheet;
  14.     public Animation upAnim;
  15.     public Animation downAnim;
  16.     public Animation rightAnim;
  17.     public Animation leftAnim;
  18.    
  19.     public boolean up, down, right, left;
  20.    
  21.     public Player(float x, float y, int width, int height, float moveSpeed) {
  22.        
  23.         super(x, y, width, height, moveSpeed);
  24.        
  25.         try {
  26.            
  27.             sheet = new SpriteSheet(playerPath, width, height);
  28.             upAnim = new Animation(sheet, 0, 3, 2, 3, false, 250, false);
  29.             downAnim = new Animation(sheet, 0, 0, 2, 0, false, 250, false);
  30.             rightAnim = new Animation(sheet, 0, 2, 2, 2, false, 250, false);
  31.             leftAnim = new Animation(sheet, 0, 1, 2, 1, false, 250, false);
  32.            
  33.         } catch (SlickException e) {
  34.            
  35.             new RuntimeException("Could not load spritesheet..", e);
  36.         }
  37.     }
  38.  
  39.     public void update(GameContainer gc, int delta) throws SlickException {
  40.  
  41.         input = gc.getInput();
  42.        
  43.         boolean upPressed = input.isKeyDown(Input.KEY_W);
  44.         boolean downPressed = input.isKeyDown(Input.KEY_S);
  45.         boolean rightPressed = input.isKeyDown(Input.KEY_D);
  46.         boolean leftPressed = input.isKeyDown(Input.KEY_A);
  47.        
  48.         if(upPressed) {
  49.            
  50.             y -= delta * moveSpeed;
  51.            
  52.             up = true;
  53.             upAnim.setAutoUpdate(true);
  54.         }
  55.        
  56.         else {
  57.            
  58.             up = false;
  59.             upAnim.setAutoUpdate(false);
  60.         }
  61.        
  62.         if(downPressed) {
  63.            
  64.             y += delta * moveSpeed;
  65.            
  66.             down = true;
  67.             downAnim.setAutoUpdate(true);
  68.         }
  69.        
  70.         else {
  71.            
  72.             down = false;
  73.             downAnim.setAutoUpdate(false);
  74.         }
  75.        
  76.         if(rightPressed) {
  77.            
  78.             x += delta * moveSpeed;
  79.            
  80.             right = true;
  81.             rightAnim.setAutoUpdate(true);
  82.         }
  83.        
  84.         else {
  85.            
  86.             right = false;
  87.             rightAnim.setAutoUpdate(false);
  88.         }
  89.        
  90.         if(leftPressed) {
  91.            
  92.             x -= delta * moveSpeed;
  93.            
  94.             left = true;
  95.             leftAnim.setAutoUpdate(true);
  96.         }
  97.        
  98.         else {
  99.            
  100.             left = false;
  101.             leftAnim.setAutoUpdate(false);
  102.         }
  103.     }
  104.  
  105.     public void render(GameContainer gc, Graphics g) throws SlickException {
  106.  
  107.         if(up) {
  108.            
  109.             upAnim.draw(x, y);
  110.         }
  111.        
  112.         else if(down) {
  113.            
  114.             downAnim.draw(x, y);
  115.         }
  116.        
  117.         else if(right) {
  118.            
  119.             rightAnim.draw(x, y);
  120.         }
  121.        
  122.         else if(left) {
  123.            
  124.             leftAnim.draw(x, y);
  125.         }
  126.        
  127.         else {
  128.            
  129.             downAnim.draw(x, y);
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement