Skip to content

Commit

Permalink
Added Some Sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
349928465 committed Jun 13, 2024
1 parent 027d563 commit e236e4b
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions Coin.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public Coin() {
*/
public void act() {
if (isTouching(Player.class)) {
playCollected();
getWorld().removeObject(this);
Level.addToTotalCoin();
}
Expand Down
25 changes: 25 additions & 0 deletions Collection.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
*/
public class Collection extends SuperSmoothMover
{
protected GreenfootSound collected = new GreenfootSound("kaching.mp3");
protected static GreenfootSound[] collectedSound;
protected static int collectedSoundIndex;
/**
* Act - do whatever the Collection wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
Expand All @@ -16,4 +19,26 @@ public void act()
{
// Add your action code here.
}

/** Plays collected sound */
protected void playCollected() {
collectedSound[collectedSoundIndex].setVolume(50);
collectedSound[collectedSoundIndex].play();
collectedSoundIndex++;
if (collectedSoundIndex >= collectedSound.length) {
collectedSoundIndex = 0;
}
}

/** Pre-loads sounds for collected up From Mr.Cohen's Demo Tutorial */
protected static void init() {
collectedSoundIndex = 0;
collectedSound = new GreenfootSound[48];
for (int i = 0; i < collectedSound.length; i++) {
collectedSound[i] = new GreenfootSound("kaching.mp3");
collectedSound[i].play();
Greenfoot.delay(1);
collectedSound[i].stop();
}
}
}
1 change: 1 addition & 0 deletions Crown.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public Crown() {
*/
public void act() {
if (isTouching(Player.class)){
playCollected();
getWorld().removeObject(this);
Level.addCrown();
}
Expand Down
8 changes: 8 additions & 0 deletions EndScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,12 @@ private void checkKeys() {
Greenfoot.setWorld(title);
}
}

public void stopped() {
TitleScreen.stopBGM();
}

public void started() {
TitleScreen.playBGM();
}
}
8 changes: 8 additions & 0 deletions GameOverScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,12 @@ private void checkKeys() {
Greenfoot.setWorld(title);
}
}

public void stopped() {
TitleScreen.stopBGM();
}

public void started() {
TitleScreen.playBGM();
}
}
3 changes: 3 additions & 0 deletions Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class Level extends World {
protected SuperDisplayLabel coinLabel = new SuperDisplayLabel(Color.BLACK, Color.WHITE, font);
protected Button saveButton = new Button();
private Orb orb;
private static GreenfootSound orbSound = new GreenfootSound("levelup.wav");

/**
* Constructor
Expand All @@ -34,6 +35,7 @@ public Level() {
saveButtonImage.scale(150, 60);
saveButton.setImage(saveButtonImage);
setPaintOrder(Button.class, SuperDisplayLabel.class, Tile.class);
Collection.init();
}

/**
Expand Down Expand Up @@ -85,6 +87,7 @@ public void spawnFloor(ImgScroll sc) {
*/
public void checkNext() {
if (orb.isBeingTouched()) {
orbSound.play();
if (level == 0) {
levelUp();
Level1 world = new Level1();
Expand Down
8 changes: 8 additions & 0 deletions Level0.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,12 @@ public void act() {
checkSaveButton();
checkNext();
}

public void stopped() {
TitleScreen.stopBGM();
}

public void started() {
TitleScreen.playBGM();
}
}
8 changes: 8 additions & 0 deletions Level1.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,12 @@ private void loseLife() {
setHP(totalHP - 5);
}
}

public void stopped() {
TitleScreen.stopBGM();
}

public void started() {
TitleScreen.playBGM();
}
}
8 changes: 8 additions & 0 deletions LoadSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,12 @@ public int[] loadSave() {
}
return options;
}

public void stopped() {
TitleScreen.stopBGM();
}

public void started() {
TitleScreen.playBGM();
}
}
4 changes: 4 additions & 0 deletions Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ public class Player extends Actor {
private boolean right, left, down, up, walking, isWaiting;
private int directionx, directiony;
protected int animIndex, animDelay, animCounter;
private GreenfootSound death = new GreenfootSound("death.mp3");

public Player() {
hp = 5;
dmg = 1;
death.setVolume(100);

R = new GreenfootImage[9];
L = new GreenfootImage[9];
Expand Down Expand Up @@ -161,6 +163,7 @@ private void boundary() {
setLocation(getX(), 0);
}
if (getY() > w.getHeight()) {
changeHP(-6);
setLocation(getX(), 0);
}
}
Expand Down Expand Up @@ -207,6 +210,7 @@ private void animate(){

private void checkHP() {
if (hp <= 0) {
death.play();
Greenfoot.setWorld(new GameOverScreen());
}
}
Expand Down
25 changes: 25 additions & 0 deletions TitleScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class TitleScreen extends World
private Button loadGame = new Button();
private GreenfootImage loadImage = new GreenfootImage("loadGameImage.png");

private static GreenfootSound bgm = new GreenfootSound("bgm.mp3");

/**
* Constructor for objects of class TitleScreen.
*
Expand All @@ -44,6 +46,9 @@ public TitleScreen()
// Load Button
loadGame.setImage(loadImage);
addObject(loadGame, 640, 450);

// Background Music
bgm.setVolume(30);
}

public void act(){
Expand All @@ -60,4 +65,24 @@ public void checkPressed(){
Greenfoot.setWorld(world);
}
}

/** Play background music if world has started */
public void started() {
bgm.playLoop();
}

/** Pause background music if world has stopped */
public void stopped() {
bgm.pause();
}

/** Method to stop background music from any world */
public static void stopBGM() {
bgm.pause();
}

/** Method to play background music from any world */
public static void playBGM() {
bgm.playLoop();
}
}
Binary file added sounds/bgm.mp3
Binary file not shown.
Binary file added sounds/death.mp3
Binary file not shown.
Binary file added sounds/kaching.mp3
Binary file not shown.
Binary file added sounds/levelup.wav
Binary file not shown.

0 comments on commit e236e4b

Please sign in to comment.