Advertisement
Guest User

Player class

a guest
Mar 31st, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. package myFirstGame;
  2.  
  3. import java.awt.Graphics;
  4. import java.awt.Image;
  5. import java.awt.Rectangle;
  6. import javax.swing.ImageIcon;
  7.  
  8. public class Player {
  9.  
  10.     int Width = 25;
  11.     int Height = 45;
  12.     public int x;
  13.     public int y;
  14.     int speed = 2;
  15.     int gravity = 7;
  16.  
  17.     Image playerChar;
  18.  
  19.     public boolean loaded = false;
  20.  
  21.     public boolean imagesLoaded = false;
  22.  
  23.     boolean Jump = false;
  24.     boolean goingRight = false;
  25.     boolean goingLeft = false;
  26.     boolean goingDown = false;
  27.  
  28.     Rectangle boundingBox;
  29.  
  30.     public Player(int x, int y) {
  31.  
  32.         this.x = x;
  33.         this.y = y;
  34.  
  35.         boundingBox = new Rectangle(x, y, Width, Height);
  36.         boundingBox.setBounds(x, y, Width, Height);
  37.  
  38.     }
  39.  
  40.     public void tick(Game game) {
  41.         boundingBox.setBounds(x, y, Width, Height);
  42.         if (Jump && y - 5 >= 0) {
  43.             y -= 100;
  44.            
  45.         }
  46.         if (goingRight && x + Width + 5 <= game.getWidth()) {
  47.             x += 3;
  48.            
  49.         }
  50.         if (goingLeft && x - 5 >= 0) {
  51.             x -= 3;
  52.            
  53.         }
  54.         if (goingDown && y + Height + 5 <= game.getHeight()) {
  55.             y += 0;
  56.         }
  57.         if (y + Height < 405 && !Jump) {
  58.             y += gravity;
  59.            
  60.         }
  61.  
  62.     }
  63.  
  64.     public void render(Graphics g) {
  65.  
  66.         loaded = true;
  67.        
  68.         playerChar = new ImageIcon("C:\\Users\\Bruker\\Pictures\\player.gif").getImage();
  69.        
  70.         if (Game.player.loaded) {
  71.             g.drawImage(Game.player.playerChar, x, y, null);
  72.         }
  73.  
  74.     }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement