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.Rectangle;
- 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;
- 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;
- int xChar;
- int yChar;
- int xMouse;
- int yMouse;
- int xLaser;
- int yLaser;
- int xDirection;
- int yDirection;
- int xZombie = 50;
- int yZombie = 50;
- int zSpeed = 1;
- ZombieClass zom = new ZombieClass();
- FPSCounter FPS = new FPSCounter();
- public String Controls = "Movement - ArrowKeys, Shooting - LMB, Sprint - Shift";
- public Image dbImage;
- public Graphics dbg;
- Font font = new Font("Arial", 1, 20);
- Font font2 = new Font("Arial", 1, 15);
- Image charImage;
- Image backImage;
- Image zombieImage;
- public void collide() {
- Rectangle playerRect = new Rectangle(xChar, yChar, 32, 32);
- Rectangle zombieRect = new Rectangle(xZombie, yZombie, 32, 32);
- if (playerRect.intersects(zombieRect)) {
- Graphics g = getGraphics();
- Font font3 = new Font("Arial", 1, 15);
- g.setFont(font3);
- g.setColor(Color.YELLOW);
- g.drawString("Collision!", 200, 150);
- zSpeed = 0;
- } else {
- zSpeed = 1;
- }
- }
- public void run() {
- try {
- while (true) {
- this.FPS.tick();
- move();
- zom.FindCharacter();
- Thread.sleep(10L);
- collide();
- }
- } catch (Exception e) {
- System.out.println("Error in your Run method");
- }
- }
- 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 Display() {
- backImage = new ImageIcon("res/Textures/Background.png").getImage();
- charImage = new ImageIcon("res/Characters/Character.png").getImage();
- addKeyListener(new Display.AL());
- addMouseListener(new Display.Mouse());
- setTitle(TITLE);
- setSize(WIDTH, HEIGHT);
- setResizable(false);
- setVisible(true);
- setDefaultCloseOperation(3);
- setLocationRelativeTo(null);
- this.xChar = 125;
- this.yChar = 125;
- this.xMouse = (this.xChar + 16);
- this.yMouse = (this.yChar + 16);
- this.xLaser = (this.xChar + 16);
- this.yLaser = (this.yChar + 16);
- }
- public void paint(Graphics g) {
- this.dbImage = createImage(getWidth(), getHeight());
- this.dbg = this.dbImage.getGraphics();
- paintComponent(this.dbg);
- g.drawImage(this.dbImage, 0, 0, this);
- }
- public void paintComponent(Graphics g) {
- g.drawImage(this.backImage, 0, 0, this);
- g.drawImage(this.charImage, this.xChar, this.yChar, this);
- g.drawImage(this.zombieImage, xZombie, yZombie, this);
- g.setColor(Color.YELLOW);
- g.setFont(this.font2);
- g.drawString(this.Controls, 90, 40);
- g.drawString("MoveSpeed: " + this.moveSpeed, 480, 40);
- g.setColor(Color.YELLOW);
- g.setFont(this.font);
- g.drawString("FPS: " + this.FPS.FPS, 5, 40);
- this.xLaser = (this.xChar + 16);
- this.yLaser = (this.yChar + 16);
- g.setColor(Color.YELLOW);
- g.drawLine(this.xChar + 16, this.yChar + 16, this.xLaser, this.yLaser);
- repaint();
- }
- public static void main(String[] args) {
- Display dis = new Display();
- Thread t1 = new Thread(dis);
- t1.start();
- }
- public class AL extends KeyAdapter {
- public AL() {
- }
- public void keyPressed(KeyEvent e) {
- int keyCode = e.getKeyCode();
- if (keyCode == 16) {
- Display.this.moveSpeed = 8;
- }
- if (keyCode == 37) {
- Display.this.setXDirection(-Display.this.moveSpeed);
- }
- if (keyCode == 39) {
- Display.this.setXDirection(Display.this.moveSpeed);
- }
- if (keyCode == 38) {
- Display.this.setYDirection(-Display.this.moveSpeed);
- }
- if (keyCode == 40) {
- Display.this.setYDirection(Display.this.moveSpeed);
- }
- e.consume();
- }
- public void keyReleased(KeyEvent e) {
- int keyCode = e.getKeyCode();
- if (keyCode == 16) {
- Display.this.moveSpeed = 5;
- }
- if (keyCode == 37) {
- Display.this.setXDirection(0);
- }
- if (keyCode == 39) {
- Display.this.setXDirection(0);
- }
- if (keyCode == 38) {
- Display.this.setYDirection(0);
- }
- if (keyCode == 40) {
- Display.this.setYDirection(0);
- }
- e.consume();
- }
- }
- public class FPSCounter {
- public int currentFPS = 0;
- public int FPS = 0;
- public long start = 0L;
- public FPSCounter() {
- }
- public void tick() {
- this.currentFPS += 1;
- if (System.currentTimeMillis() - this.start >= 1000L) {
- this.FPS = this.currentFPS;
- this.currentFPS = 0;
- this.start = System.currentTimeMillis();
- }
- }
- public int getFPS() {
- return this.FPS;
- }
- }
- class Mouse extends MouseAdapter {
- Mouse() {
- }
- public void mousePressed(MouseEvent e) {
- int xCoordinate = e.getX();
- int yCoordinate = e.getY();
- Display.this.xMouse = xCoordinate;
- Display.this.yMouse = yCoordinate;
- Display.this.xLaser = Display.this.xMouse;
- Display.this.yLaser = Display.this.yMouse;
- }
- public void mouseReleased(MouseEvent e) {
- Display.this.xLaser = (Display.this.xChar + 16);
- Display.this.yLaser = (Display.this.yChar + 16);
- }
- }
- public class ZombieClass {
- public ZombieClass() {
- zombieImage = new ImageIcon("res/Characters/Zombie.png").getImage();
- }
- public void FindCharacter() {
- if (xZombie < Display.this.xChar) {
- xZombie += zSpeed;
- }
- if (xZombie > Display.this.xChar) {
- xZombie -= zSpeed;
- }
- if (yZombie < Display.this.yChar) {
- yZombie += zSpeed;
- }
- if (yZombie > Display.this.yChar) {
- yZombie -= zSpeed;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment