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.
## 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 {
private boolean debug = true;
private Image sprite;
private int x = 400;
private int y = 512;
@ -51,6 +53,7 @@ public class Player extends JPanel implements KeyListener, ActionListener {
public void actionPerformed(ActionEvent e) {
canShoot = true; // Allow shooting again after fdelay milliseconds
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 (upPressed) y -= spd;
if (downPressed) y += spd;
if (shootPressed) playerShoot();
//Update bullet positions
for (int i = 0; i < bullets.size(); i++) {
@ -66,6 +70,7 @@ public class Player extends JPanel implements KeyListener, ActionListener {
bullet.update();
if (bullet.getY() < -32) {
bullets.remove(i);
if (debug) System.out.println("Bullet removed");
i--;
}
}
@ -76,6 +81,7 @@ public class Player extends JPanel implements KeyListener, ActionListener {
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(new File(soundFile)));
clip.start();
if (debug) System.out.println("Sound played: " + soundFile);
} catch (Exception e) {
System.out.println("Error playing sound: " + e.getMessage());
}
@ -83,19 +89,28 @@ public class Player extends JPanel implements KeyListener, ActionListener {
private void playerShoot() {
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
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;
shotTimer.start();
if (debug) System.out.println("Cannon timer started");
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(sprite, x, y, this);
//Draw player bullets
for (PlayerBullet bullet : bullets) {
bullet.draw(g);
}
}
@Override

@ -14,6 +14,12 @@ public class PlayerBullet {
this.y = startY;
ImageIcon icon = new ImageIcon("src/spr/bullet.png");
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() {
@ -21,7 +27,14 @@ public class PlayerBullet {
}
public void draw(Graphics g) {
//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() {

Loading…
Cancel
Save