Added debug and fixed bullet instantiation

master
Aaron Johnon 1 year ago
parent 6a3f43b435
commit fc715d2d11

@ -1,3 +1,6 @@
#Tank Game # Tank Game
Blow up the other tanks, except they don't exist yet. Blow up the other tanks, except they don't exist yet.
## Compile and run:
`javac -d bin -cp bin src/main/main.java src/entities/PlayerBullet.java src/entities/player.java && java -cp bin main.Main`

@ -16,6 +16,8 @@ import entities.PlayerBullet;
public class Player extends JPanel implements KeyListener, ActionListener { public class Player extends JPanel implements KeyListener, ActionListener {
private boolean debug = true;
private Image sprite; private Image sprite;
private int x = 400; private int x = 400;
private int y = 512; private int y = 512;
@ -51,6 +53,7 @@ public class Player extends JPanel implements KeyListener, ActionListener {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
canShoot = true; // Allow shooting again after fdelay milliseconds canShoot = true; // Allow shooting again after fdelay milliseconds
shotTimer.stop(); // Stop the timer until the next shot shotTimer.stop(); // Stop the timer until the next shot
if (debug) System.out.println("Cannon reset");
} }
}); });
} }
@ -59,6 +62,7 @@ public class Player extends JPanel implements KeyListener, ActionListener {
if (rightPressed) x += spd; if (rightPressed) x += spd;
if (upPressed) y -= spd; if (upPressed) y -= spd;
if (downPressed) y += spd; if (downPressed) y += spd;
if (shootPressed) playerShoot();
//Update bullet positions //Update bullet positions
for (int i = 0; i < bullets.size(); i++) { for (int i = 0; i < bullets.size(); i++) {
@ -66,6 +70,7 @@ public class Player extends JPanel implements KeyListener, ActionListener {
bullet.update(); bullet.update();
if (bullet.getY() < -32) { if (bullet.getY() < -32) {
bullets.remove(i); bullets.remove(i);
if (debug) System.out.println("Bullet removed");
i--; i--;
} }
} }
@ -76,6 +81,7 @@ public class Player extends JPanel implements KeyListener, ActionListener {
Clip clip = AudioSystem.getClip(); Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(new File(soundFile))); clip.open(AudioSystem.getAudioInputStream(new File(soundFile)));
clip.start(); clip.start();
if (debug) System.out.println("Sound played: " + soundFile);
} catch (Exception e) { } catch (Exception e) {
System.out.println("Error playing sound: " + e.getMessage()); System.out.println("Error playing sound: " + e.getMessage());
} }
@ -83,19 +89,28 @@ public class Player extends JPanel implements KeyListener, ActionListener {
private void playerShoot() { private void playerShoot() {
if (canShoot) { if (canShoot) {
PlayerBullet newBullet = new PlayerBullet(x + sprite.getWidth(null) / 2, y); PlayerBullet newBullet = new PlayerBullet(x + sprite.getWidth(null) / 2 - 3, y);
bullets.add(newBullet); //Add to bullets list bullets.add(newBullet); //Add to bullets list
playSound("sound/shoot1.wav"); if (debug) {
System.out.println("Cannon fired: " + newBullet.getX() + ", " + newBullet.getY());
System.out.println("Bullet added. Total bullets: " + bullets.size());
}
playSound("src/sound/shoot1.wav");
canShoot = false; canShoot = false;
shotTimer.start(); shotTimer.start();
if (debug) System.out.println("Cannon timer started");
} }
} }
@Override @Override
protected void paintComponent(Graphics g) { protected void paintComponent(Graphics g) {
super.paintComponent(g); super.paintComponent(g);
g.drawImage(sprite, x, y, this); g.drawImage(sprite, x, y, this);
//Draw player bullets
for (PlayerBullet bullet : bullets) {
bullet.draw(g);
}
} }
@Override @Override

@ -14,6 +14,12 @@ public class PlayerBullet {
this.y = startY; this.y = startY;
ImageIcon icon = new ImageIcon("src/spr/bullet.png"); ImageIcon icon = new ImageIcon("src/spr/bullet.png");
sprite = icon.getImage(); sprite = icon.getImage();
if (sprite == null) {
System.out.println("Failed to load bullet.png");
} else {
System.out.println("bullet.png image loaded successfully.");
}
System.out.println("PlayerBullet created at: (" + x + ", " + y + ")");
} }
public void update() { public void update() {
@ -21,7 +27,14 @@ public class PlayerBullet {
} }
public void draw(Graphics g) { public void draw(Graphics g) {
g.drawImage(sprite, x, y, null); //g.drawImage(sprite, x, y, null);
if (sprite != null) {
g.drawImage(sprite, x, y, null);
} else {
System.out.println("Bullet sprite is null, drawing placeholder.");
g.setColor(Color.RED);
g.fillRect(x, y, 8, 8);
}
} }
public int getX() { public int getX() {

Loading…
Cancel
Save