Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import javax.swing.ImageIcon;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- public class Display extends JFrame implements Runnable {
- private static final long serialVersionUID = 1L;
- public static String TITLE = "The Box Alpha 0.05";
- public static int WIDTH = 600;
- public static int HEIGHT = 400;
- public int moveSpeed = 5;
- public boolean Paused = false;
- int xChar, yChar, xMouse, yMouse, xLaserAnchor, yLaserAnchor, xLaserDest, yLaserDest, xDirection, yDirection, xCoordinate, yCoordinate;
- int xZombie = 50, yZombie = 50;
- FPSCounter FPS = new FPSCounter();
- boolean SHOT = false;
- public String Controls = "Movement - ArrowKeys, Shooting - LMB, Sprint - Shift, Pause - ESC";
- public Image dbImage;
- public Graphics dbg;
- Font font = new Font("Arial", Font.BOLD, 12);
- Font font2 = new Font("Arial", Font.BOLD, 15);
- Image charImage, backImage, zombieImage;
- public void run() {
- try {
- while (true) {
- if (!Paused) {
- FPS.tick();
- move();
- FindCharacter();
- }
- Thread.sleep(10);
- }
- } catch (Exception e) {
- System.out.println("Error");
- }
- }
- public void Shooting() {
- while (!SHOT) {
- xLaserDest = xLaserAnchor;
- yLaserDest = yLaserAnchor;
- }
- while (SHOT) {
- xLaserDest = xCoordinate;
- yLaserDest = yCoordinate;
- }
- }
- public class FPSCounter {
- public int currentFPS = 0;
- public int FPS = 0;
- public long start = 0;
- public void tick() {
- currentFPS++;
- if (System.currentTimeMillis() - start >= 1000) {
- FPS = currentFPS;
- currentFPS = 0;
- start = System.currentTimeMillis();
- }
- }
- public int getFPS() {
- return FPS;
- }
- }
- public void move() {
- xChar += xDirection;
- yChar += yDirection;
- if (xChar <= 0) {
- xChar = 0;
- }
- if (xChar >= WIDTH - 32) {
- xChar = WIDTH - 32;
- }
- if (yChar <= 24) {
- yChar = 24;
- }
- if (yChar >= HEIGHT - 32) {
- yChar = HEIGHT - 32;
- }
- }
- public void setXDirection(int xdir) {
- xDirection = xdir;
- }
- public void setYDirection(int ydir) {
- yDirection = ydir;
- }
- public class AL extends KeyAdapter {
- public void keyPressed(KeyEvent e) {
- int keyCode = e.getKeyCode();
- if (keyCode == e.VK_ESCAPE) {
- if (!Paused) {
- Paused = true;
- } else {
- Paused = false;
- }
- }
- if (keyCode == e.VK_SHIFT) {
- moveSpeed = 8;
- }
- if (keyCode == e.VK_LEFT) {
- setXDirection(-moveSpeed);
- }
- if (keyCode == e.VK_RIGHT) {
- setXDirection(+moveSpeed);
- }
- if (keyCode == e.VK_UP) {
- setYDirection(-moveSpeed);
- }
- if (keyCode == e.VK_DOWN) {
- setYDirection(+moveSpeed);
- }
- e.consume();
- }
- public void keyReleased(KeyEvent e) {
- int keyCode = e.getKeyCode();
- if (keyCode == e.VK_SHIFT) {
- moveSpeed = 5;
- }
- if (keyCode == e.VK_LEFT) {
- setXDirection(0);
- }
- if (keyCode == e.VK_RIGHT) {
- setXDirection(0);
- }
- if (keyCode == e.VK_UP) {
- setYDirection(0);
- }
- if (keyCode == e.VK_DOWN) {
- setYDirection(0);
- }
- e.consume();
- }
- }
- public void drawImages() {
- try {
- // Load images
- backImage = new ImageIcon("res/Textures/Background.png").getImage();
- charImage = new ImageIcon("res/Characters/Character.png").getImage();
- zombieImage = new ImageIcon("res/Characters/Zombie.png").getImage();
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null, "Error loading images. Please send a message to Ryan B. at RyanMB1997@hotmail.com");
- }
- }
- public Display() {
- // Game properties
- addKeyListener(new AL());
- addMouseListener(new Mouse());
- // Draw images
- drawImages();
- // Screen properties
- setTitle(TITLE);
- setSize(WIDTH, HEIGHT);
- setResizable(false);
- setVisible(true);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setLocationRelativeTo(null);
- xChar = 125;
- yChar = 125;
- xMouse = xChar + 16;
- yMouse = yChar + 16;
- xLaserAnchor = xChar + 16;
- yLaserAnchor = yChar + 16;
- }
- public void FindCharacter() {
- if (xZombie < xChar) {
- xZombie++;
- }
- if (xZombie > xChar) {
- xZombie--;
- }
- if (yZombie < yChar) {
- yZombie++;
- }
- if (yZombie > yChar) {
- yZombie--;
- }
- }
- class Mouse extends MouseAdapter {
- @Override
- public void mousePressed(MouseEvent e) {
- int xCoordinate = e.getX();
- int yCoordinate = e.getY();
- xMouse = xCoordinate;
- yMouse = yCoordinate;
- xLaserDest = xMouse;
- yLaserDest = yMouse;
- SHOT = true;
- }
- @Override
- public void mouseReleased(MouseEvent e) {
- xLaserDest = xChar + 16;
- yLaserDest = yChar + 16;
- SHOT = false;
- }
- }
- public void paint(Graphics g) {
- dbImage = createImage(getWidth(), getHeight());
- dbg = dbImage.getGraphics();
- paintComponent(dbg);
- g.drawImage(dbImage, 0, 0, this);
- }
- public void paintComponent(Graphics g) {
- xLaserAnchor = xChar + 16;
- yLaserAnchor = yChar + 16;
- g.drawImage(backImage, 0, 0, this);
- g.drawImage(charImage, xChar, yChar, this);
- g.drawImage(zombieImage, xZombie, yZombie, this);
- g.setColor(Color.YELLOW);
- g.setFont(font);
- g.drawString(Controls, 70, 40);
- g.drawString("MoveSpeed: " + moveSpeed, 460, 40);
- g.setColor(Color.YELLOW);
- g.setFont(font2);
- g.drawString("FPS: " + FPS.FPS, 5, 40);
- g.setColor(Color.YELLOW);
- g.drawLine(xLaserAnchor, yLaserAnchor, xLaserDest, yLaserDest);
- repaint();
- }
- public static void main(String[] args) {
- Display dis = new Display();
- // Threads
- Thread t1 = new Thread(dis);
- t1.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement