Guest User

Game.java

a guest
May 3rd, 2015
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.23 KB | None | 0 0
  1. package bombermangame;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Point;
  6. import java.awt.Rectangle;
  7. import java.awt.event.ActionEvent;
  8. import java.util.Timer;
  9. import java.util.TimerTask;
  10.  
  11. import javax.swing.AbstractAction;
  12. import javax.swing.ActionMap;
  13. import javax.swing.InputMap;
  14. import javax.swing.JComponent;
  15. import javax.swing.JPanel;
  16. import javax.swing.KeyStroke;
  17.  
  18. public class Game extends JPanel {
  19.     private static final long serialVersionUID = 1L;
  20.  
  21.     // Players X and Y (Directional)
  22.     public static int dx = 0;
  23.     public static int dy = -20;
  24.  
  25.     // Player MoveSpeed
  26.     private double moveSpeed = 7;
  27.  
  28.     // Walls
  29.     public static Rectangle[][] walls;
  30.     public static Rectangle[] noWalls;
  31.  
  32.     // Animation Setters
  33.     // public static boolean right = false;
  34.     // public static boolean left = false;
  35.     // public static boolean down = true;
  36.     // public static boolean up = false;
  37.  
  38.     // Should the player move in which direction
  39.     private int moving = 0;
  40.     private int rightMove = 40;
  41.     private int leftMove = 30;
  42.     private int downMove = 10;
  43.     private int upMove = 20;
  44.  
  45.     protected static int animation = 0;
  46.     protected static int down = 0;
  47.     protected static int up = 10;
  48.     protected static int left = 20;
  49.     protected static int right = 30;
  50.  
  51.     // Intersects
  52.     public boolean isInsideWalls = false;
  53.  
  54.     // Is game running?
  55.     public static boolean running = false;
  56.  
  57.     // For FPS counter
  58.     private long nextSecond = System.currentTimeMillis() + 1000;
  59.     private int frameInLastSecond = 0;
  60.     private int framesInCurrentSecond = 0;
  61.  
  62.     public Game() {
  63.         super(true);
  64.  
  65.         keyInputs();
  66.         this.requestFocus();
  67.  
  68.         if (running = true) {
  69.             walls();
  70.         }
  71.         Timer timer = new Timer();
  72.         TimerTask task = new TimerTask() {
  73.             @Override
  74.             public void run() {
  75.                 if (running) {
  76.                     // Update the game
  77.                     update();
  78.  
  79.                 }
  80.             }
  81.         };
  82.         timer.scheduleAtFixedRate(task, 0, 33);
  83.     }
  84.  
  85.     public void fps(Graphics g) {
  86.         g.setColor(Color.GREEN);
  87.         long currentTime = System.currentTimeMillis();
  88.  
  89.         if (currentTime > nextSecond) {
  90.             nextSecond += 1000;
  91.             frameInLastSecond = framesInCurrentSecond;
  92.             framesInCurrentSecond = 0;
  93.         }
  94.         framesInCurrentSecond++;
  95.  
  96.         g.drawString(Integer.toString(frameInLastSecond) + " fps", 20, 20);
  97.     }
  98.  
  99.     public void update() {
  100.         playerMovement();
  101.  
  102.         // Updates the paint();
  103.         revalidate();
  104.         paintImmediately(0, 0, MainClass.WIDTH, MainClass.HEIGHT);
  105.     }
  106.  
  107.     // Checks if players Rectangle his with another Rectangle (Walls)
  108.     public boolean intersectsBox(Rectangle r, Rectangle r2) {
  109.         return r.intersects(r2);
  110.     }
  111.  
  112.     public Point P1, P2, P3, P4;
  113.  
  114.     public void intersectsBox2(Rectangle r, Rectangle r2) {
  115.         P1 = new Point((int) r.getMinX(), (int) r.getMinY());;
  116.         P2 = new Point((int) r.getMaxX(), (int) r.getMaxY());
  117.         P3 = r2.getLocation();
  118.         P4 = new Point((int) r2.getMaxX(), (int) r2.getMaxY());
  119.         if ((P2.y < P3.y || P1.y > P4.y || P2.x < P3.x || P1.x > P4.x)
  120.                 && !intersectsBox(playerRectangle(), noWalls[0])) {
  121.             isInsideWalls = true;
  122.         }
  123.     }
  124.  
  125.     // Gets the players rectangle
  126.     public Rectangle playerRectangle() {
  127.         return new Rectangle(9 + dx, 23 + dy, 54, 90);
  128.     }
  129.  
  130.     public void walls() {
  131.         int wallsY = 0, wallsX = 0;
  132.         walls = new Rectangle[9][9];
  133.         for (int i = 0; i < 9; i++) {
  134.             for (int j = 0; j < 9; j++) {
  135.                 walls[i][j] = new Rectangle(wallsX, wallsY, 95 / 3 + 10,
  136.                         95 / 6 - 5);
  137.                 wallsY = i * 95;
  138.                 wallsX = j * 95;
  139.  
  140.             }
  141.         }
  142.         noWalls = new Rectangle[1];
  143.         noWalls[0] = new Rectangle(0, 0, 95 / 3 + 10, 95 / 6 - 5);
  144.     }
  145.  
  146.     public void drawWalls(Graphics g) {
  147.         // Draws Default Walls
  148.         int wallsY = 0, wallsX = 0;
  149.         for (int i = 0; i < 9; i++) {
  150.             for (int j = 0; j < 9; j++) {
  151.                 g.setColor(Color.blue);
  152.                 g.fillRect(wallsX, wallsY, 95, 95);
  153.                 g.setColor(Color.black);
  154.                 g.drawRect(wallsX, wallsY, 95, 95);
  155.                 wallsY = i * 95;
  156.                 wallsX = j * 95;
  157.             }
  158.         }
  159.  
  160.         // UbWalls
  161.         g.setColor(Color.orange);
  162.         g.fillRect(walls[1][2].x, walls[1][2].y, 95, 95);
  163.  
  164.         // Temp NoWalls
  165.         g.setColor(Color.BLACK);
  166.         g.fillRect(0, 0, 95, 95);
  167.         g.fillRect(95, 0, 95, 95);
  168.         g.fillRect(0, 95, 95, 95);
  169.     }
  170.  
  171.     public void playerMovement() {
  172.         if (isInsideWalls) {
  173.             System.out.println("YOU ARE IN THE BOX!");
  174.             if (animation == down) {
  175.                 dy -= moveSpeed;
  176.                 isInsideWalls = false;
  177.             } else if (animation == up) {
  178.                 dy += moveSpeed;
  179.                 isInsideWalls = false;
  180.             } else if (animation == left) {
  181.                 dx += moveSpeed;
  182.                 isInsideWalls = false;
  183.             } else if (animation == right) {
  184.                 dx -= moveSpeed;
  185.                 isInsideWalls = false;
  186.             }
  187.         } else {
  188.             // Moves the player
  189.             if (moving == downMove) {
  190.                 dy += moveSpeed;
  191.                 moving = 0;
  192.             } else if (moving == upMove) {
  193.                 dy -= moveSpeed;
  194.                 moving = 0;
  195.             } else if (moving == leftMove) {
  196.                 dx -= moveSpeed;
  197.                 moving = 0;
  198.             } else if (moving == rightMove) {
  199.                 dx += moveSpeed;
  200.                 moving = 0;
  201.             }
  202.         }
  203.  
  204.         // Check if player reached border of screen
  205.         if (playerRectangle().getX() + 64 > MainClass.WIDTH) {
  206.             dx -= moveSpeed;
  207.         } else if (playerRectangle().getX() < 0) {
  208.             dx += moveSpeed;
  209.         }
  210.         if (playerRectangle().getY() + 120 > MainClass.HEIGHT) {
  211.             dy -= moveSpeed;
  212.         } else if (playerRectangle().getY() + 4 < 0) {
  213.             dy += moveSpeed;
  214.         }
  215.  
  216.         //Checks for intersection
  217.         for (int i = 0; i < 9; i++) {
  218.             for (int j = 0; j < 9; j++) {
  219.                 // if (intersectsBox(walls[i][j],playerRectangle())
  220.                 // && !intersectsBox(playerRectangle(), noWalls[0])) {
  221.                 // isInsideWalls = true;
  222.                 // }
  223.                 intersectsBox2(walls[i][j], playerRectangle());
  224.             }
  225.         }
  226.     }
  227.  
  228.     public void keyInputs() {
  229.         int mapName = JComponent.WHEN_IN_FOCUSED_WINDOW;
  230.         InputMap imap = this.getInputMap(mapName);
  231.  
  232.         KeyStroke dKey = KeyStroke.getKeyStroke('d');
  233.         imap.put(dKey, "right");
  234.  
  235.         KeyStroke aKey = KeyStroke.getKeyStroke('a');
  236.         imap.put(aKey, "left");
  237.  
  238.         KeyStroke wKey = KeyStroke.getKeyStroke('w');
  239.         imap.put(wKey, "up");
  240.  
  241.         KeyStroke sKey = KeyStroke.getKeyStroke('s');
  242.         imap.put(sKey, "down");
  243.  
  244.         AbstractAction mvright = new AbstractAction() {
  245.             private static final long serialVersionUID = 1L;
  246.  
  247.             public void actionPerformed(ActionEvent e) {
  248.                 animation = right;
  249.                 moving = rightMove;
  250.             }
  251.         };
  252.  
  253.         AbstractAction mvleft = new AbstractAction() {
  254.             private static final long serialVersionUID = 1L;
  255.  
  256.             public void actionPerformed(ActionEvent e) {
  257.                 animation = left;
  258.                 moving = leftMove;
  259.             }
  260.         };
  261.  
  262.         AbstractAction mvup = new AbstractAction() {
  263.             private static final long serialVersionUID = 1L;
  264.  
  265.             public void actionPerformed(ActionEvent e) {
  266.                 animation = up;
  267.                 moving = upMove;
  268.             }
  269.         };
  270.  
  271.         AbstractAction mvdown = new AbstractAction() {
  272.             private static final long serialVersionUID = 1L;
  273.  
  274.             public void actionPerformed(ActionEvent e) {
  275.                 animation = down;
  276.                 moving = downMove;
  277.             }
  278.         };
  279.  
  280.         ActionMap amap = this.getActionMap();
  281.         amap.put("right", mvright);
  282.         amap.put("left", mvleft);
  283.         amap.put("up", mvup);
  284.         amap.put("down", mvdown);
  285.     }
  286.  
  287.     @Override
  288.     protected void paintComponent(Graphics g) {
  289.         super.paintComponent(g);
  290.  
  291.         // Draws Walls
  292.         drawWalls(g);
  293.  
  294.         // Draws Players Animation
  295.         DrawPlayer.draw(g);
  296.  
  297.         // FPS Counter(Debug purposes mainly)
  298.         fps(g);
  299.  
  300.         // Disposes For New Frame
  301.         g.dispose();
  302.  
  303.     }
  304.  
  305. }
Advertisement
Add Comment
Please, Sign In to add comment