Bomb arraylist

master
Chara1236 2022-06-09 00:07:44 -04:00
parent 0fbe9cf522
commit 6f12ed702f
3 changed files with 49 additions and 24 deletions

View File

@ -49,15 +49,23 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
public static ArrayList<Tile>particleTiles = new ArrayList<Tile>(); public static ArrayList<Tile>particleTiles = new ArrayList<Tile>();
public static ArrayList<NonPlayer>enemy = new ArrayList<NonPlayer>(); public static ArrayList<NonPlayer>enemy = new ArrayList<NonPlayer>();
public static ArrayList<StickyBomb>bombs = new ArrayList<>();
public static ArrayList<Particle>particles = new ArrayList<Particle>(); public static ArrayList<Particle>particles = new ArrayList<Particle>();
public static StickyBomb b;
public static Camera camera; public static Camera camera;
// image imports begin here // image imports begin here
public BufferedImage backgroundImage = getImage("img/backgrounds/pointyMountains.png"); public BufferedImage backgroundImage = getImage("img/backgrounds/pointyMountains.png");
public BufferedImage box = getImage("img/tiles/boxes/box.png"); public BufferedImage box = getImage("img/tiles/boxes/box.png");
public BufferedImage boxCoin = getImage("img/tiles/boxes/boxCoin.png"); public BufferedImage boxCoin = getImage("img/tiles/boxes/boxCoin.png");
public BufferedImage bomb = getImage("img/misc/bomb.png"); public static BufferedImage bomb;
static {
try {
bomb = getImage("img/misc/bomb.png");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public GamePanel(JPanel gameFrame) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException { public GamePanel(JPanel gameFrame) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException {
@ -112,7 +120,6 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
addMouseListener(new MouseAdapter() { addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
player.mousePressed(e); player.mousePressed(e);
b.mousePressed(e);
} }
}); });
this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT)); this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
@ -156,7 +163,15 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
enemy.get(i).draw(g, enemyFrame); enemy.get(i).draw(g, enemyFrame);
} }
playerFrameCounter += player.draw(g, playerFrame); playerFrameCounter += player.draw(g, playerFrame);
b.draw(g); for(int i=0; i<bombs.size(); i++){
if(i<bombs.size()) {
if (bombs.get(i).erase) {
bombs.remove(i);
} else {
bombs.get(i).draw(g);
}
}
}
for(int i=0; i<particles.size(); i++){ for(int i=0; i<particles.size(); i++){
if(i<particles.size()) { if(i<particles.size()) {
particles.get(i).draw(g); particles.get(i).draw(g);
@ -167,7 +182,6 @@ 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);
if (isPaused) { if (isPaused) {
g.setColor(new Color(255, 255, 255, 100)); g.setColor(new Color(255, 255, 255, 100));
@ -183,7 +197,9 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
for (NonPlayer n: enemy) { for (NonPlayer n: enemy) {
n.move(); n.move();
} }
b.move(); for(int i=0; i<bombs.size(); i++){
bombs.get(i).move();
}
for(int i=0; i<particles.size(); i++){ for(int i=0; i<particles.size(); i++){
particles.get(i).move(); particles.get(i).move();
} }
@ -232,7 +248,6 @@ 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, 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.

View File

@ -6,6 +6,7 @@ import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException; import javax.sound.sampled.UnsupportedAudioFileException;
import java.awt.*; import java.awt.*;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver; import java.awt.image.ImageObserver;
import java.io.IOException; import java.io.IOException;
@ -182,7 +183,8 @@ public class Player extends GenericSprite {
} }
yVelocity = -10; yVelocity = -10;
} }
xVelocity*=0.93; xVelocity *= 0.93;
if(!isGrounded) { if(!isGrounded) {
yVelocity += 0.3; yVelocity += 0.3;
if(downPressed){ if(downPressed){
@ -202,6 +204,12 @@ public class Player extends GenericSprite {
y = LevelManager.ySpawn; y = LevelManager.ySpawn;
} }
public void mousePressed(MouseEvent e){
int xx = e.getX();
int yy = e.getY();
GamePanel.bombs.add(new StickyBomb(GamePanel.player.x+GamePanel.camera.x,GamePanel.player.y,35,
(xx-GamePanel.player.x)/20, (yy-GamePanel.player.y)/10, GamePanel.bomb, GamePanel.explosionArray));
}
public void addParticle(int x) throws IOException { public void addParticle(int x) throws IOException {
if(GlobalState.randInt(1,3)==3) { if(GlobalState.randInt(1,3)==3) {
GamePanel.particles.add(new Particle(this.x + GamePanel.camera.x + WIDTH / 2 + GlobalState.randInt(-PLAYER_WIDTH / 2, PLAYER_WIDTH / 2) GamePanel.particles.add(new Particle(this.x + GamePanel.camera.x + WIDTH / 2 + GlobalState.randInt(-PLAYER_WIDTH / 2, PLAYER_WIDTH / 2)

View File

@ -9,8 +9,8 @@ public class StickyBomb extends GenericSprite{
public int yVelocity; public int yVelocity;
public boolean isMove; public boolean isMove;
public int realX; public int realX;
public BufferedImage sprite; public static BufferedImage sprite ;
public BufferedImage[] explosionSpriteArray; public static BufferedImage[] explosionSpriteArray;
public int fuse; public int fuse;
public int explosionPixel = 0; public int explosionPixel = 0;
@ -18,6 +18,8 @@ public class StickyBomb extends GenericSprite{
public boolean alive; public boolean alive;
public boolean erase;
public StickyBomb(int x, int y, int length, int xVelocity, int yVelocity, BufferedImage sprite, BufferedImage[] explosionSpriteArray){ 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;
@ -28,6 +30,7 @@ public class StickyBomb extends GenericSprite{
fuse = GlobalState.second*5; fuse = GlobalState.second*5;
isMove = true; isMove = true;
alive = true; alive = true;
erase = false;
} }
public void update(){ public void update(){
@ -36,16 +39,18 @@ public class StickyBomb extends GenericSprite{
public void explode(){ public void explode(){
double yDis = GamePanel.player.y+Player.PLAYER_HEIGHT/2-(y+(double)length/2); double yDis = GamePanel.player.y+Player.PLAYER_HEIGHT/2-(y+(double)length/2);
double xDis = GamePanel.player.x+Player.PLAYER_HEIGHT/2-(realX+(double)length/2); double xDis = GamePanel.player.x+Player.PLAYER_WIDTH/2-(realX+(double)length/2);
double hypo = Math.sqrt(yDis*yDis+xDis+xDis); double hypo = Math.sqrt(yDis*yDis+xDis*xDis);
if(yDis!=0){ if(hypo<300) {
GamePanel.player.yVelocity += 10000/(yDis*hypo); if (yDis != 0) {
} GamePanel.player.yVelocity += 10 * (yDis) / (hypo);
if(xDis!=0) { }
GamePanel.player.xVelocity += 100000/(xDis*hypo); if (xDis != 0) {
GamePanel.player.xVelocity += 10 * (xDis) / (hypo);
}
} }
GamePanel.player.capSpeed(); GamePanel.player.capSpeed();
alive = false; alive = false;
} }
public void move(){ public void move(){
update(); update();
@ -100,12 +105,7 @@ 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, explosionSpriteArray);
}
public void draw(Graphics g){ public void draw(Graphics g){
if (explosionCounter >= 2) { if (explosionCounter >= 2) {
@ -118,6 +118,8 @@ public class StickyBomb extends GenericSprite{
g.drawImage(explosionSpriteArray[explosionPixel], x - GamePanel.camera.x - 10*explosionPixel, g.drawImage(explosionSpriteArray[explosionPixel], x - GamePanel.camera.x - 10*explosionPixel,
y-10*explosionPixel, length+10*explosionPixel, length+10*explosionPixel, null); y-10*explosionPixel, length+10*explosionPixel, length+10*explosionPixel, null);
explosionCounter += 1; explosionCounter += 1;
} else {
erase = true;
} }
} }
} }