Guest User

Untitled

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