-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.java
More file actions
39 lines (36 loc) · 1.36 KB
/
Copy pathButton.java
File metadata and controls
39 lines (36 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import greenfoot.*;
/**
* This class represents a Button in the game.
* It changes the world or stops the game when clicked.
*
@authors (Rupert & Raymond Van Niekerk)
* @student Numbers (222894237 & 221154469)
* @version (1)
*/
public class Button extends Actor {
private String label; // The text label of the button
public Button(String label) {
// Constructor sets the label and creates the image
this.label = label;
getImage();
setImage(new GreenfootImage(label, 32, Color.YELLOW, Color.BLACK));
}
public void act() {
// Check if the button is clicked and perform the corresponding action
if (Greenfoot.mouseClicked(this)) {
if (label.equals("Start")) {
Greenfoot.setWorld(new PingPongWorld());
Greenfoot.playSound("click-for-game-menu-131903.mp3");
} else if (label.equals("Help")) {
Greenfoot.setWorld(new HelpMenu());
Greenfoot.playSound("click-for-game-menu-131903.mp3");
} else if (label.equals("Exit")) {
Greenfoot.stop();
Greenfoot.playSound("mixkit-arcade-retro-game-over-213.wav");
} else if (label.equals("Back")) {
Greenfoot.setWorld(new MainMenu());
Greenfoot.playSound("select-sound-121244.mp3");
}
}
}
}