Added pauses, bomb explosion

master
John 2022-06-08 13:36:47 -04:00
parent bfa449e8a0
commit 69dc890d2d
4 changed files with 74 additions and 14 deletions

View File

@ -35,9 +35,12 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
// keeps track of how many ticks has elapsed since last frame change
public int playerFrameCounter = 0;
public int enemyFrameCounter = 0;
public boolean isPaused;
public PauseMenu pauseMenu;
public static BufferedImage[][][] playerSpriteArray = new BufferedImage[2][2][11];
public static BufferedImage[][][] slimeSpriteArray = new BufferedImage[2][2][3];
public static BufferedImage[] explosionArray = new BufferedImage[9];
//public static ArrayList<Tile>map = new ArrayList<Tile>();
@ -59,6 +62,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
this.gameFrame = gameFrame;
camera = new Camera(0);
background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT, 10, camera);
pauseMenu = new PauseMenu(GAME_HEIGHT/2, 100, 400, 400, GAME_WIDTH, new Font(Font.MONOSPACED, Font.BOLD, 60), "Paused");
try {
// load player sprites from disk here
for (int i = 0; i < 11; i++) {
@ -69,6 +73,9 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
playerSpriteArray[0][0][i] = flipImageHorizontally(sprite);
playerSpriteArray[0][1][i] = flipImageHorizontally(sprite);
}
for (int i = 0; i < 9; i++) {
explosionArray[i] = getImage("img/misc/bomb/sonicExplosion0" + i + ".png");
}
// load slime sprites from disk here
// these variables were not defined above because they are temporary variables
BufferedImage[] temporarySlimeArray = {getImage("img/enemy/slime/slimeWalk1.png"),
@ -126,6 +133,13 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
//call the draw methods in each class to update positions as things move
public void draw(Graphics g, int playerFrame, int enemyFrame){
background.draw(g);
if (isPaused) {
// set player frame to 0 to prevent frame from changing when player moves
playerFrame = 0;
Graphics2D g2d = (Graphics2D)(g);
// remove extraneous details from game when paused
g2d.setPaint(Color.white);
}
//Don't want to draw off screen items
int xMin = Math.max(0,((GamePanel.camera.x+GAME_WIDTH)/Tile.length)-(GAME_WIDTH/(2*Tile.length))-5);
int xMax = Math.min(map.length, 7+xMin + GAME_WIDTH/Tile.length);
@ -150,6 +164,12 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
}
g.drawString(camera.x+" "+((camera.x+GAME_WIDTH)/Tile.length),100,100);
g.drawString(b.x+" "+((b.x+GamePanel.GAME_WIDTH)/Tile.length-4),100,200);
if (isPaused) {
g.setColor(new Color(255, 255, 255, 100));
g.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
pauseMenu.draw(g, Color.white, Color.black);
}
}
//call the move methods in other classes to update positions
@ -208,7 +228,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
//run() method is what makes the game continue running without end. It calls other methods to move objects, check for collision, and update the screen
public void run(){
b = new StickyBomb(600, 100, 20, 1,-5, bomb);
b = new StickyBomb(600, 100, 20, 1,-5, bomb, explosionArray);
LevelManager.setLevel(1);
//the CPU runs our game code too quickly - we need to slow it down! The following lines of code "force" the computer to get stuck in a loop for short intervals between calling other methods to update the screen.
@ -225,14 +245,17 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
//only move objects around and update screen if enough time has passed
if(delta >= 1){
if (!isPaused) {
// only perform game functions if game is not paused
move();
checkCollision();
repaint();
if (playerFrameCounter > 5) {
// increment sprite image to be used and keeps it below 12
playerFrame = (playerFrame + 1) % 11;
playerFrameCounter -= 5;
}
}
repaint();
delta--;
}
}
@ -240,8 +263,12 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
//if a key is pressed, we'll send it over to the Player class for processing
public void keyPressed(KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
isPaused = !isPaused;
} else {
player.keyPressed(e);
}
}
//if a key is released, we'll send it over to the Player class for processing
public void keyReleased(KeyEvent e){

View File

@ -137,7 +137,6 @@ public class MenuPanel extends JPanel implements Runnable, KeyListener{
for (TextBox t: textBoxArray) {
if (t.isHover(e.getX(), e.getY())) {
currentBox = textBoxArray.indexOf(t);
System.out.println(t.id);
return true;
}
}

21
src/PauseMenu.java Normal file
View File

@ -0,0 +1,21 @@
import java.awt.*;
public class PauseMenu extends TextBox {
public PauseMenu(int y, int textYOffset, int xWidth, int yHeight, int totalWidth, Font font, String text) {
super(y, xWidth, yHeight, totalWidth, font, text, null);
this.y -= textYOffset;
}
public void drawCenteredTextBox(Graphics g, String text, Color backgroundColor, Color textColor) {
if (backgroundColor != null) {
g.setColor(textColor);
// TODO: make drawn line widths consistent
((Graphics2D)g).setStroke(new BasicStroke(4f));
g.drawRect(newX, newY, xWidth, yHeight);
g.setColor(backgroundColor);
g.fillRect(newX, newY, xWidth, yHeight);
}
g.setColor(textColor);
drawCenteredString(g, y, newX, xWidth, text);
}
}

View File

@ -9,18 +9,22 @@ public class StickyBomb extends GenericSprite{
public int yVelocity;
public boolean isMove;
public int realX;
BufferedImage sprite;
public BufferedImage sprite;
public BufferedImage[] explosionSpriteArray;
public int fuse;
public int explosionPixel = 0;
public int explosionCounter = 0;
public boolean alive;
public StickyBomb(int x, int y, int length, int xVelocity, int yVelocity, BufferedImage sprite){
public StickyBomb(int x, int y, int length, int xVelocity, int yVelocity, BufferedImage sprite, BufferedImage[] explosionSpriteArray){
super(x,y,length,length);
this.length = length;
this.xVelocity = xVelocity;
this.yVelocity = yVelocity;
this.sprite = sprite;
this.explosionSpriteArray = explosionSpriteArray;
fuse = GlobalState.second*5;
isMove = true;
alive = true;
@ -99,12 +103,21 @@ public class StickyBomb extends GenericSprite{
public void mousePressed(MouseEvent e){
int xx = e.getX();
int yy = e.getY();
GamePanel.b = new StickyBomb(GamePanel.player.x+GamePanel.camera.x,GamePanel.player.y,35, (xx-GamePanel.player.x)/20, (yy-GamePanel.player.y)/10, sprite);
GamePanel.b = new StickyBomb(GamePanel.player.x+GamePanel.camera.x,GamePanel.player.y,35,
(xx-GamePanel.player.x)/20, (yy-GamePanel.player.y)/10, sprite, explosionSpriteArray);
}
public void draw(Graphics g){
if (explosionCounter >= 2) {
explosionPixel += 1;
explosionCounter -= 2;
}
if(alive) {
g.drawImage(sprite, x - GamePanel.camera.x, y, length, length, null);
} else if (explosionPixel < explosionSpriteArray.length - 1) {
g.drawImage(explosionSpriteArray[explosionPixel], x - GamePanel.camera.x - 10*explosionPixel,
y-10*explosionPixel, length+10*explosionPixel, length+10*explosionPixel, null);
explosionCounter += 1;
}
}
}