Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bombermangame;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Point;
- import java.awt.Rectangle;
- import java.awt.event.ActionEvent;
- import java.util.Timer;
- import java.util.TimerTask;
- import javax.swing.AbstractAction;
- import javax.swing.ActionMap;
- import javax.swing.InputMap;
- import javax.swing.JComponent;
- import javax.swing.JPanel;
- import javax.swing.KeyStroke;
- public class Game extends JPanel {
- private static final long serialVersionUID = 1L;
- // Players X and Y (Directional)
- public static int dx = 0;
- public static int dy = -20;
- // Player MoveSpeed
- private double moveSpeed = 7;
- // Walls
- public static Rectangle[][] walls;
- public static Rectangle[] noWalls;
- // Animation Setters
- // public static boolean right = false;
- // public static boolean left = false;
- // public static boolean down = true;
- // public static boolean up = false;
- // Should the player move in which direction
- private int moving = 0;
- private int rightMove = 40;
- private int leftMove = 30;
- private int downMove = 10;
- private int upMove = 20;
- protected static int animation = 0;
- protected static int down = 0;
- protected static int up = 10;
- protected static int left = 20;
- protected static int right = 30;
- // Intersects
- public boolean isInsideWalls = false;
- // Is game running?
- public static boolean running = false;
- // For FPS counter
- private long nextSecond = System.currentTimeMillis() + 1000;
- private int frameInLastSecond = 0;
- private int framesInCurrentSecond = 0;
- public Game() {
- super(true);
- keyInputs();
- this.requestFocus();
- if (running = true) {
- walls();
- }
- Timer timer = new Timer();
- TimerTask task = new TimerTask() {
- @Override
- public void run() {
- if (running) {
- // Update the game
- update();
- }
- }
- };
- timer.scheduleAtFixedRate(task, 0, 33);
- }
- public void fps(Graphics g) {
- g.setColor(Color.GREEN);
- long currentTime = System.currentTimeMillis();
- if (currentTime > nextSecond) {
- nextSecond += 1000;
- frameInLastSecond = framesInCurrentSecond;
- framesInCurrentSecond = 0;
- }
- framesInCurrentSecond++;
- g.drawString(Integer.toString(frameInLastSecond) + " fps", 20, 20);
- }
- public void update() {
- playerMovement();
- // Updates the paint();
- revalidate();
- paintImmediately(0, 0, MainClass.WIDTH, MainClass.HEIGHT);
- }
- // Checks if players Rectangle his with another Rectangle (Walls)
- public boolean intersectsBox(Rectangle r, Rectangle r2) {
- return r.intersects(r2);
- }
- public Point P1, P2, P3, P4;
- public void intersectsBox2(Rectangle r, Rectangle r2) {
- P1 = new Point((int) r.getMinX(), (int) r.getMinY());;
- P2 = new Point((int) r.getMaxX(), (int) r.getMaxY());
- P3 = r2.getLocation();
- P4 = new Point((int) r2.getMaxX(), (int) r2.getMaxY());
- if ((P2.y < P3.y || P1.y > P4.y || P2.x < P3.x || P1.x > P4.x)
- && !intersectsBox(playerRectangle(), noWalls[0])) {
- isInsideWalls = true;
- }
- }
- // Gets the players rectangle
- public Rectangle playerRectangle() {
- return new Rectangle(9 + dx, 23 + dy, 54, 90);
- }
- public void walls() {
- int wallsY = 0, wallsX = 0;
- walls = new Rectangle[9][9];
- for (int i = 0; i < 9; i++) {
- for (int j = 0; j < 9; j++) {
- walls[i][j] = new Rectangle(wallsX, wallsY, 95 / 3 + 10,
- 95 / 6 - 5);
- wallsY = i * 95;
- wallsX = j * 95;
- }
- }
- noWalls = new Rectangle[1];
- noWalls[0] = new Rectangle(0, 0, 95 / 3 + 10, 95 / 6 - 5);
- }
- public void drawWalls(Graphics g) {
- // Draws Default Walls
- int wallsY = 0, wallsX = 0;
- for (int i = 0; i < 9; i++) {
- for (int j = 0; j < 9; j++) {
- g.setColor(Color.blue);
- g.fillRect(wallsX, wallsY, 95, 95);
- g.setColor(Color.black);
- g.drawRect(wallsX, wallsY, 95, 95);
- wallsY = i * 95;
- wallsX = j * 95;
- }
- }
- // UbWalls
- g.setColor(Color.orange);
- g.fillRect(walls[1][2].x, walls[1][2].y, 95, 95);
- // Temp NoWalls
- g.setColor(Color.BLACK);
- g.fillRect(0, 0, 95, 95);
- g.fillRect(95, 0, 95, 95);
- g.fillRect(0, 95, 95, 95);
- }
- public void playerMovement() {
- if (isInsideWalls) {
- System.out.println("YOU ARE IN THE BOX!");
- if (animation == down) {
- dy -= moveSpeed;
- isInsideWalls = false;
- } else if (animation == up) {
- dy += moveSpeed;
- isInsideWalls = false;
- } else if (animation == left) {
- dx += moveSpeed;
- isInsideWalls = false;
- } else if (animation == right) {
- dx -= moveSpeed;
- isInsideWalls = false;
- }
- } else {
- // Moves the player
- if (moving == downMove) {
- dy += moveSpeed;
- moving = 0;
- } else if (moving == upMove) {
- dy -= moveSpeed;
- moving = 0;
- } else if (moving == leftMove) {
- dx -= moveSpeed;
- moving = 0;
- } else if (moving == rightMove) {
- dx += moveSpeed;
- moving = 0;
- }
- }
- // Check if player reached border of screen
- if (playerRectangle().getX() + 64 > MainClass.WIDTH) {
- dx -= moveSpeed;
- } else if (playerRectangle().getX() < 0) {
- dx += moveSpeed;
- }
- if (playerRectangle().getY() + 120 > MainClass.HEIGHT) {
- dy -= moveSpeed;
- } else if (playerRectangle().getY() + 4 < 0) {
- dy += moveSpeed;
- }
- //Checks for intersection
- for (int i = 0; i < 9; i++) {
- for (int j = 0; j < 9; j++) {
- // if (intersectsBox(walls[i][j],playerRectangle())
- // && !intersectsBox(playerRectangle(), noWalls[0])) {
- // isInsideWalls = true;
- // }
- intersectsBox2(walls[i][j], playerRectangle());
- }
- }
- }
- public void keyInputs() {
- int mapName = JComponent.WHEN_IN_FOCUSED_WINDOW;
- InputMap imap = this.getInputMap(mapName);
- KeyStroke dKey = KeyStroke.getKeyStroke('d');
- imap.put(dKey, "right");
- KeyStroke aKey = KeyStroke.getKeyStroke('a');
- imap.put(aKey, "left");
- KeyStroke wKey = KeyStroke.getKeyStroke('w');
- imap.put(wKey, "up");
- KeyStroke sKey = KeyStroke.getKeyStroke('s');
- imap.put(sKey, "down");
- AbstractAction mvright = new AbstractAction() {
- private static final long serialVersionUID = 1L;
- public void actionPerformed(ActionEvent e) {
- animation = right;
- moving = rightMove;
- }
- };
- AbstractAction mvleft = new AbstractAction() {
- private static final long serialVersionUID = 1L;
- public void actionPerformed(ActionEvent e) {
- animation = left;
- moving = leftMove;
- }
- };
- AbstractAction mvup = new AbstractAction() {
- private static final long serialVersionUID = 1L;
- public void actionPerformed(ActionEvent e) {
- animation = up;
- moving = upMove;
- }
- };
- AbstractAction mvdown = new AbstractAction() {
- private static final long serialVersionUID = 1L;
- public void actionPerformed(ActionEvent e) {
- animation = down;
- moving = downMove;
- }
- };
- ActionMap amap = this.getActionMap();
- amap.put("right", mvright);
- amap.put("left", mvleft);
- amap.put("up", mvup);
- amap.put("down", mvdown);
- }
- @Override
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- // Draws Walls
- drawWalls(g);
- // Draws Players Animation
- DrawPlayer.draw(g);
- // FPS Counter(Debug purposes mainly)
- fps(g);
- // Disposes For New Frame
- g.dispose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment