Skip to content

Commit

Permalink
Update Game.java
Browse files Browse the repository at this point in the history
greg
  • Loading branch information
5quirre1 authored Oct 27, 2024
1 parent 5e6ec9c commit 5d8644c
Showing 1 changed file with 81 additions and 27 deletions.
108 changes: 81 additions & 27 deletions Code/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ public class Game extends JFrame implements ActionListener {
private Clip clip;
private Random random;
private Clip gunshotClip;

private boolean isPaused = false;
private String[] quotes = {
"Poo poo go do baby",
"My grandma can play this without pausing",
"Your mom."
};

private JDialog pauseDialog;

public Game() {
setTitle("MurderBob");
setSize(800, 600);
Expand All @@ -42,9 +50,19 @@ public Game() {
playerY = 300;
random = new Random();
setFocusable(true);
random = new Random();

addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (isPaused) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
resumeGame();
}
return;
}
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
pauseGame();
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
shoot();
}
Expand All @@ -60,9 +78,6 @@ public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
rightPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_R && playerHealth <= 0) {
restartGame();
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
shoot();
}
Expand Down Expand Up @@ -129,15 +144,15 @@ public void mousePressed(MouseEvent e) {

private void loadSprite() {
try {
freakyBobSprite = ImageIO.read(new File("Assets\\Freakybob.png"));
freakyBobSprite = ImageIO.read(new File("Code\\Assets\\Freakybob.png"));
} catch (Exception e) {
System.err.println("Error loading sprite: " + e.getMessage());
e.printStackTrace();
}
}
private void loadGunshotSound() {
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("Assets\\gunshot.wav"));
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("Code\\Assets\\gunshot.wav"));
gunshotClip = AudioSystem.getClip();
gunshotClip.open(audioInputStream);
} catch (Exception e) {
Expand All @@ -154,7 +169,7 @@ private void playGunshotSound() {
}
private void playMusic() {
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("Assets\\awesome_ass_music_David_Fesliyan.wav"));
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("Code\\Assets\\awesome_ass_music_David_Fesliyan.wav"));
clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.loop(Clip.LOOP_CONTINUOUSLY);
Expand Down Expand Up @@ -205,7 +220,19 @@ private void spawnEnemies() {
}
currentWave++;
}

private void restartGame() {
playerHealth = 100;
currentWave = 0;
preparing = true;
prepareTime = 10;
bullets.clear();
enemies.clear();
perks.clear();
playerX = 400;
playerY = 300;
startPreparation();
timer.start();
}
private void spawnPerk() {
int perkX = random.nextInt(getWidth() - 32);
int perkY = random.nextInt(getHeight() - 32);
Expand Down Expand Up @@ -288,21 +315,48 @@ public void actionPerformed(ActionEvent e) {
}
repaint();
}

private void restartGame() {
playerHealth = 100;
currentWave = 0;
preparing = true;
prepareTime = 10;
bullets.clear();
enemies.clear();
perks.clear();
playerX = 400;
playerY = 300;
startPreparation();
private void pauseGame() {
if (!isPaused) {
isPaused = true;
timer.stop();
if (clip.isRunning()) clip.stop();
createPauseDialog();
}
}

private void resumeGame() {
isPaused = false;
timer.start();
clip.start();
pauseDialog.dispose();
}

private void createPauseDialog() {
pauseDialog = new JDialog(this, "Game Paused", true);
pauseDialog.setSize(300, 200);
pauseDialog.setLocationRelativeTo(this);
pauseDialog.setLayout(new FlowLayout());


String randomQuote = quotes[random.nextInt(quotes.length)];
JLabel quoteLabel = new JLabel(randomQuote);
pauseDialog.add(quoteLabel);

JButton resumeButton = new JButton("Resume");
resumeButton.addActionListener(e -> resumeGame());
pauseDialog.add(resumeButton);

JButton homeButton = new JButton("Home");
homeButton.addActionListener(e -> returnToHome());
pauseDialog.add(homeButton);

pauseDialog.setVisible(true);
}

private void returnToHome() {
// This will be here cause lihgkjlef/;g
}

private class GamePanel extends JPanel {
public GamePanel() {
setBackground(Color.BLACK);
Expand Down Expand Up @@ -396,20 +450,20 @@ public Enemy(int x, int y) {

private void loadSprite() {
try {
enemySprite = ImageIO.read(new File("Assets\\ScarySquirtward.png"));
enemySprite = ImageIO.read(new File("Code\\Assets\\Enemy.png"));
} catch (IOException e) {
System.err.println("Error loading enemy sprite: " + e.getMessage());
e.printStackTrace();
}
}

public void moveTowards(int playerX, int playerY) {
if (x < playerX) x += SPEED;
if (x > playerX) x -= SPEED;
if (y < playerY) y += SPEED;
if (y > playerY) y -= SPEED;
double angle = Math.atan2(playerY - y, playerX - x);
x += SPEED * Math.cos(angle);
y += SPEED * Math.sin(angle);
}


public void draw(Graphics g) {
if (enemySprite != null) {
g.drawImage(enemySprite, x, y, 32, 32, null);
Expand Down Expand Up @@ -447,9 +501,9 @@ public Perk(int x, int y, int type) {
protected void loadImage() {
try {
if (type == 0) {
image = ImageIO.read(new File("Assets/green-marijuana-leaf-png_252592.jpg"));
image = ImageIO.read(new File("Code/Assets/green-marijuana-leaf-png_252592.jpg"));
} else if (type == 1) {
image = ImageIO.read(new File("Assets/speed_perk.png"));
image = ImageIO.read(new File("Code/Assets/speed_perk.png"));
}
} catch (IOException e) {
e.printStackTrace();
Expand Down

0 comments on commit 5d8644c

Please sign in to comment.