Added pauses, bomb explosion
parent
bfa449e8a0
commit
69dc890d2d
|
@ -35,9 +35,12 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
|
||||||
// keeps track of how many ticks has elapsed since last frame change
|
// keeps track of how many ticks has elapsed since last frame change
|
||||||
public int playerFrameCounter = 0;
|
public int playerFrameCounter = 0;
|
||||||
public int enemyFrameCounter = 0;
|
public int enemyFrameCounter = 0;
|
||||||
|
public boolean isPaused;
|
||||||
|
public PauseMenu pauseMenu;
|
||||||
|
|
||||||
public static BufferedImage[][][] playerSpriteArray = new BufferedImage[2][2][11];
|
public static BufferedImage[][][] playerSpriteArray = new BufferedImage[2][2][11];
|
||||||
public static BufferedImage[][][] slimeSpriteArray = new BufferedImage[2][2][3];
|
public static BufferedImage[][][] slimeSpriteArray = new BufferedImage[2][2][3];
|
||||||
|
public static BufferedImage[] explosionArray = new BufferedImage[9];
|
||||||
|
|
||||||
//public static ArrayList<Tile>map = new ArrayList<Tile>();
|
//public static ArrayList<Tile>map = new ArrayList<Tile>();
|
||||||
|
|
||||||
|
@ -59,6 +62,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
|
||||||
this.gameFrame = gameFrame;
|
this.gameFrame = gameFrame;
|
||||||
camera = new Camera(0);
|
camera = new Camera(0);
|
||||||
background = new BackgroundImage(0, 0, backgroundImage, GAME_WIDTH, GAME_HEIGHT, 10, camera);
|
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 {
|
try {
|
||||||
// load player sprites from disk here
|
// load player sprites from disk here
|
||||||
for (int i = 0; i < 11; i++) {
|
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][0][i] = flipImageHorizontally(sprite);
|
||||||
playerSpriteArray[0][1][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
|
// load slime sprites from disk here
|
||||||
// these variables were not defined above because they are temporary variables
|
// these variables were not defined above because they are temporary variables
|
||||||
BufferedImage[] temporarySlimeArray = {getImage("img/enemy/slime/slimeWalk1.png"),
|
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
|
//call the draw methods in each class to update positions as things move
|
||||||
public void draw(Graphics g, int playerFrame, int enemyFrame){
|
public void draw(Graphics g, int playerFrame, int enemyFrame){
|
||||||
background.draw(g);
|
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
|
//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 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);
|
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(camera.x+" "+((camera.x+GAME_WIDTH)/Tile.length),100,100);
|
||||||
g.drawString(b.x+" "+((b.x+GamePanel.GAME_WIDTH)/Tile.length-4),100,200);
|
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
|
//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
|
//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(){
|
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);
|
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.
|
//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
|
//only move objects around and update screen if enough time has passed
|
||||||
if(delta >= 1){
|
if(delta >= 1){
|
||||||
|
if (!isPaused) {
|
||||||
|
// only perform game functions if game is not paused
|
||||||
move();
|
move();
|
||||||
checkCollision();
|
checkCollision();
|
||||||
repaint();
|
|
||||||
if (playerFrameCounter > 5) {
|
if (playerFrameCounter > 5) {
|
||||||
// increment sprite image to be used and keeps it below 12
|
// increment sprite image to be used and keeps it below 12
|
||||||
playerFrame = (playerFrame + 1) % 11;
|
playerFrame = (playerFrame + 1) % 11;
|
||||||
playerFrameCounter -= 5;
|
playerFrameCounter -= 5;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
repaint();
|
||||||
delta--;
|
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
|
//if a key is pressed, we'll send it over to the Player class for processing
|
||||||
public void keyPressed(KeyEvent e){
|
public void keyPressed(KeyEvent e){
|
||||||
|
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
|
||||||
|
isPaused = !isPaused;
|
||||||
|
} else {
|
||||||
player.keyPressed(e);
|
player.keyPressed(e);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//if a key is released, we'll send it over to the Player class for processing
|
//if a key is released, we'll send it over to the Player class for processing
|
||||||
public void keyReleased(KeyEvent e){
|
public void keyReleased(KeyEvent e){
|
||||||
|
|
|
@ -137,7 +137,6 @@ public class MenuPanel extends JPanel implements Runnable, KeyListener{
|
||||||
for (TextBox t: textBoxArray) {
|
for (TextBox t: textBoxArray) {
|
||||||
if (t.isHover(e.getX(), e.getY())) {
|
if (t.isHover(e.getX(), e.getY())) {
|
||||||
currentBox = textBoxArray.indexOf(t);
|
currentBox = textBoxArray.indexOf(t);
|
||||||
System.out.println(t.id);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,18 +9,22 @@ public class StickyBomb extends GenericSprite{
|
||||||
public int yVelocity;
|
public int yVelocity;
|
||||||
public boolean isMove;
|
public boolean isMove;
|
||||||
public int realX;
|
public int realX;
|
||||||
BufferedImage sprite;
|
public BufferedImage sprite;
|
||||||
|
public BufferedImage[] explosionSpriteArray;
|
||||||
|
|
||||||
public int fuse;
|
public int fuse;
|
||||||
|
public int explosionPixel = 0;
|
||||||
|
public int explosionCounter = 0;
|
||||||
|
|
||||||
public boolean alive;
|
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);
|
super(x,y,length,length);
|
||||||
this.length = length;
|
this.length = length;
|
||||||
this.xVelocity = xVelocity;
|
this.xVelocity = xVelocity;
|
||||||
this.yVelocity = yVelocity;
|
this.yVelocity = yVelocity;
|
||||||
this.sprite = sprite;
|
this.sprite = sprite;
|
||||||
|
this.explosionSpriteArray = explosionSpriteArray;
|
||||||
fuse = GlobalState.second*5;
|
fuse = GlobalState.second*5;
|
||||||
isMove = true;
|
isMove = true;
|
||||||
alive = true;
|
alive = true;
|
||||||
|
@ -99,12 +103,21 @@ public class StickyBomb extends GenericSprite{
|
||||||
public void mousePressed(MouseEvent e){
|
public void mousePressed(MouseEvent e){
|
||||||
int xx = e.getX();
|
int xx = e.getX();
|
||||||
int yy = e.getY();
|
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){
|
public void draw(Graphics g){
|
||||||
|
if (explosionCounter >= 2) {
|
||||||
|
explosionPixel += 1;
|
||||||
|
explosionCounter -= 2;
|
||||||
|
}
|
||||||
if(alive) {
|
if(alive) {
|
||||||
g.drawImage(sprite, x - GamePanel.camera.x, y, length, length, null);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue