Advertisement
Guest User

The_Box

a guest
Mar 23rd, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.78 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.Graphics;
  4. import java.awt.Image;
  5. import java.awt.event.KeyAdapter;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.MouseAdapter;
  8. import java.awt.event.MouseEvent;
  9.  
  10. import javax.swing.ImageIcon;
  11. import javax.swing.JFrame;
  12. import javax.swing.JOptionPane;
  13.  
  14. public class Display extends JFrame implements Runnable {
  15.     private static final long serialVersionUID = 1L;
  16.  
  17.     public static String TITLE = "The Box Alpha 0.05";
  18.     public static int WIDTH = 600;
  19.     public static int HEIGHT = 400;
  20.  
  21.     public int moveSpeed = 5;
  22.     public boolean Paused = false;
  23.  
  24.     int xChar, yChar, xMouse, yMouse, xLaserAnchor, yLaserAnchor, xLaserDest, yLaserDest, xDirection, yDirection, xCoordinate, yCoordinate;
  25.     int xZombie = 50, yZombie = 50;
  26.     FPSCounter FPS = new FPSCounter();
  27.     boolean SHOT = false;
  28.  
  29.     public String Controls = "Movement - ArrowKeys, Shooting - LMB, Sprint - Shift, Pause - ESC";
  30.  
  31.     public Image dbImage;
  32.     public Graphics dbg;
  33.     Font font = new Font("Arial", Font.BOLD, 12);
  34.     Font font2 = new Font("Arial", Font.BOLD, 15);
  35.     Image charImage, backImage, zombieImage;
  36.  
  37.     public void run() {
  38.         try {
  39.             while (true) {
  40.                 if (!Paused) {
  41.                     FPS.tick();
  42.                     move();
  43.                     FindCharacter();
  44.                 }
  45.                 Thread.sleep(10);
  46.             }
  47.         } catch (Exception e) {
  48.             System.out.println("Error");
  49.         }
  50.  
  51.     }
  52.  
  53.     public void Shooting() {
  54.         while (!SHOT) {
  55.             xLaserDest = xLaserAnchor;
  56.             yLaserDest = yLaserAnchor;
  57.         }
  58.         while (SHOT) {
  59.             xLaserDest = xCoordinate;
  60.             yLaserDest = yCoordinate;
  61.         }
  62.     }
  63.  
  64.     public class FPSCounter {
  65.         public int currentFPS = 0;
  66.         public int FPS = 0;
  67.         public long start = 0;
  68.  
  69.         public void tick() {
  70.             currentFPS++;
  71.             if (System.currentTimeMillis() - start >= 1000) {
  72.                 FPS = currentFPS;
  73.                 currentFPS = 0;
  74.                 start = System.currentTimeMillis();
  75.             }
  76.         }
  77.  
  78.         public int getFPS() {
  79.             return FPS;
  80.         }
  81.     }
  82.  
  83.     public void move() {
  84.         xChar += xDirection;
  85.         yChar += yDirection;
  86.  
  87.         if (xChar <= 0) {
  88.             xChar = 0;
  89.         }
  90.         if (xChar >= WIDTH - 32) {
  91.             xChar = WIDTH - 32;
  92.         }
  93.         if (yChar <= 24) {
  94.             yChar = 24;
  95.         }
  96.         if (yChar >= HEIGHT - 32) {
  97.             yChar = HEIGHT - 32;
  98.         }
  99.  
  100.     }
  101.  
  102.     public void setXDirection(int xdir) {
  103.         xDirection = xdir;
  104.     }
  105.  
  106.     public void setYDirection(int ydir) {
  107.         yDirection = ydir;
  108.     }
  109.  
  110.     public class AL extends KeyAdapter {
  111.         public void keyPressed(KeyEvent e) {
  112.             int keyCode = e.getKeyCode();
  113.  
  114.             if (keyCode == e.VK_ESCAPE) {
  115.                 if (!Paused) {
  116.                     Paused = true;
  117.                 } else {
  118.                     Paused = false;
  119.                 }
  120.             }
  121.  
  122.             if (keyCode == e.VK_SHIFT) {
  123.                 moveSpeed = 8;
  124.             }
  125.  
  126.             if (keyCode == e.VK_LEFT) {
  127.                 setXDirection(-moveSpeed);
  128.             }
  129.  
  130.             if (keyCode == e.VK_RIGHT) {
  131.                 setXDirection(+moveSpeed);
  132.             }
  133.  
  134.             if (keyCode == e.VK_UP) {
  135.                 setYDirection(-moveSpeed);
  136.             }
  137.  
  138.             if (keyCode == e.VK_DOWN) {
  139.                 setYDirection(+moveSpeed);
  140.             }
  141.             e.consume();
  142.  
  143.         }
  144.  
  145.         public void keyReleased(KeyEvent e) {
  146.             int keyCode = e.getKeyCode();
  147.  
  148.             if (keyCode == e.VK_SHIFT) {
  149.                 moveSpeed = 5;
  150.             }
  151.  
  152.             if (keyCode == e.VK_LEFT) {
  153.                 setXDirection(0);
  154.             }
  155.  
  156.             if (keyCode == e.VK_RIGHT) {
  157.                 setXDirection(0);
  158.             }
  159.  
  160.             if (keyCode == e.VK_UP) {
  161.                 setYDirection(0);
  162.             }
  163.  
  164.             if (keyCode == e.VK_DOWN) {
  165.                 setYDirection(0);
  166.             }
  167.             e.consume();
  168.  
  169.         }
  170.     }
  171.  
  172.     public void drawImages() {
  173.         try {
  174.             // Load images
  175.             backImage = new ImageIcon("res/Textures/Background.png").getImage();
  176.             charImage = new ImageIcon("res/Characters/Character.png").getImage();
  177.             zombieImage = new ImageIcon("res/Characters/Zombie.png").getImage();
  178.         } catch (Exception e) {
  179.             JOptionPane.showMessageDialog(null, "Error loading images. Please send a message to Ryan B. at RyanMB1997@hotmail.com");
  180.         }
  181.     }
  182.  
  183.     public Display() {
  184.  
  185.         // Game properties
  186.         addKeyListener(new AL());
  187.         addMouseListener(new Mouse());
  188.  
  189.         // Draw images
  190.         drawImages();
  191.  
  192.         // Screen properties
  193.         setTitle(TITLE);
  194.         setSize(WIDTH, HEIGHT);
  195.         setResizable(false);
  196.         setVisible(true);
  197.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  198.         setLocationRelativeTo(null);
  199.  
  200.         xChar = 125;
  201.         yChar = 125;
  202.  
  203.         xMouse = xChar + 16;
  204.         yMouse = yChar + 16;
  205.  
  206.         xLaserAnchor = xChar + 16;
  207.         yLaserAnchor = yChar + 16;
  208.  
  209.     }
  210.  
  211.     public void FindCharacter() {
  212.         if (xZombie < xChar) {
  213.             xZombie++;
  214.         }
  215.         if (xZombie > xChar) {
  216.             xZombie--;
  217.         }
  218.         if (yZombie < yChar) {
  219.             yZombie++;
  220.         }
  221.         if (yZombie > yChar) {
  222.             yZombie--;
  223.         }
  224.     }
  225.  
  226.     class Mouse extends MouseAdapter {
  227.         @Override
  228.         public void mousePressed(MouseEvent e) {
  229.             int xCoordinate = e.getX();
  230.             int yCoordinate = e.getY();
  231.             xMouse = xCoordinate;
  232.             yMouse = yCoordinate;
  233.  
  234.             xLaserDest = xMouse;
  235.             yLaserDest = yMouse;
  236.  
  237.             SHOT = true;
  238.         }
  239.  
  240.         @Override
  241.         public void mouseReleased(MouseEvent e) {
  242.             xLaserDest = xChar + 16;
  243.             yLaserDest = yChar + 16;
  244.  
  245.             SHOT = false;
  246.  
  247.         }
  248.     }
  249.  
  250.     public void paint(Graphics g) {
  251.         dbImage = createImage(getWidth(), getHeight());
  252.         dbg = dbImage.getGraphics();
  253.         paintComponent(dbg);
  254.         g.drawImage(dbImage, 0, 0, this);
  255.  
  256.     }
  257.  
  258.     public void paintComponent(Graphics g) {
  259.  
  260.         xLaserAnchor = xChar + 16;
  261.         yLaserAnchor = yChar + 16;
  262.  
  263.         g.drawImage(backImage, 0, 0, this);
  264.         g.drawImage(charImage, xChar, yChar, this);
  265.         g.drawImage(zombieImage, xZombie, yZombie, this);
  266.  
  267.         g.setColor(Color.YELLOW);
  268.         g.setFont(font);
  269.         g.drawString(Controls, 70, 40);
  270.  
  271.         g.drawString("MoveSpeed: " + moveSpeed, 460, 40);
  272.  
  273.         g.setColor(Color.YELLOW);
  274.         g.setFont(font2);
  275.         g.drawString("FPS: " + FPS.FPS, 5, 40);
  276.  
  277.         g.setColor(Color.YELLOW);
  278.         g.drawLine(xLaserAnchor, yLaserAnchor, xLaserDest, yLaserDest);
  279.  
  280.         repaint();
  281.     }
  282.  
  283.     public static void main(String[] args) {
  284.         Display dis = new Display();
  285.         // Threads
  286.         Thread t1 = new Thread(dis);
  287.         t1.start();
  288.     }
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement