import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
// Main application class
class MiniGameApp {
public static void main(String[] args) {
// Start with Whack-A-Mole
new WhackAMole();
}
}
// Whack-A-Mole Game Class
class WhackAMole extends Frame implements ActionListener {
private Button[] buttons = new Button[9];
private int moleIndex = -1;
private Random random = new Random();
private Timer timer;
private int score = 0;
private Label scoreLabel;
public WhackAMole() {
// Frame setup
setTitle("Whack-A-Mole");
setSize(500, 500);
setLayout(new GridLayout(4, 3));
setBackground(new Color(255, 228, 196)); // Light peach background
// Initialize buttons
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new Button("");
buttons[i].setBackground(new Color(211, 211, 211)); // Light gray
buttons[i].setFont(new Font("Arial", Font.BOLD, 16));
buttons[i].addActionListener(this);
add(buttons[i]);
}
scoreLabel = new Label("Score: 0", Label.CENTER);
scoreLabel.setFont(new Font("Arial", Font.BOLD, 18));
scoreLabel.setBackground(new Color(255, 255, 224)); // Light yellow
scoreLabel.setForeground(Color.BLUE); // Blue text
add(scoreLabel);
// Timer to spawn mole every second
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
spawnMole();
}
}, 0, 1000);
// Button to switch to Rock-Paper-Scissors
Button switchGameBtn = new Button("Switch to Rock-Paper-Scissors");
switchGameBtn.setBackground(new Color(100, 149, 237)); // Cornflower blue
switchGameBtn.setForeground(Color.WHITE); // White text
switchGameBtn.setFont(new Font("Arial", Font.BOLD, 16));
switchGameBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose(); // Close the current game frame
new RockPaperScissors(); // Open the Rock-Paper-Scissors game
}
});
add(switchGameBtn);
setVisible(true);
}
private void spawnMole() {
// Reset previous mole
if (moleIndex != -1) {
buttons[moleIndex].setBackground(new Color(211, 211, 211)); // Light gray
buttons[moleIndex].setLabel("");
}
// Spawn new mole at random index
moleIndex = random.nextInt(buttons.length);
buttons[moleIndex].setBackground(Color.GREEN); // Green for mole
buttons[moleIndex].setLabel("Mole!");
}
@Override
public void actionPerformed(ActionEvent e) {
Button clickedButton = (Button) e.getSource();
// Check if the clicked button is the mole
if (clickedButton == buttons[moleIndex]) {
score++;
scoreLabel.setText("Score: " + score);
clickedButton.setBackground(new Color(211, 211, 211)); // Reset to light gray
clickedButton.setLabel("");
}
}
}
// Rock-Paper-Scissors Game Class
class RockPaperScissors extends Frame implements MouseListener {
Image rockImg, paperImg, scissorImg;
Canvas rockCanvas, paperCanvas, scissorCanvas;
TextField userChoiceField, computerChoiceField, resultField;
String userChoice, computerChoice;
Random random;
RockPaperScissors() {
random = new Random();
setSize(600, 600);
setTitle("Rock Paper Scissors Game");
setLayout(new BorderLayout());
setBackground(new Color(135, 206, 250)); // Sky blue background
// Create a panel for the images
Panel imagePanel = new Panel();
imagePanel.setLayout(new GridLayout(1, 3));
rockCanvas = createCanvas("rock.png");
paperCanvas = createCanvas("paper.png");
scissorCanvas = createCanvas("scissors.png");
imagePanel.add(rockCanvas);
imagePanel.add(paperCanvas);
imagePanel.add(scissorCanvas);
// Create a panel for user choice, computer choice, and result
Panel infoPanel = new Panel();
infoPanel.setLayout(new GridLayout(4, 1));
userChoiceField = new TextField("Your Choice: ");
userChoiceField.setEditable(false); // Make it read-only
computerChoiceField = new TextField("Computer's Choice: ");
computerChoiceField.setEditable(false); // Make it read-only
resultField = new TextField("Result: ");
resultField.setEditable(false); // Make it read-only
infoPanel.add(userChoiceField);
infoPanel.add(computerChoiceField);
infoPanel.add(resultField);
// Button to switch back to Whack-A-Mole
Button switchGameBtn = new Button("Switch to Whack-A-Mole");
switchGameBtn.setBackground(new Color(255, 99, 71)); // Tomato color
switchGameBtn.setForeground(Color.WHITE); // White text
switchGameBtn.setFont(new Font("Arial", Font.BOLD, 16));
switchGameBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose(); // Close the current game frame
new WhackAMole(); // Open the Whack-A-Mole game
}
});
infoPanel.add(switchGameBtn);
// Add panels to the frame
add(imagePanel, BorderLayout.CENTER);
add(infoPanel, BorderLayout.SOUTH);
setVisible(true);
}
private Canvas createCanvas(String imgPath) {
Canvas canvas = new Canvas() {
public void paint(Graphics g) {
Image img = Toolkit.getDefaultToolkit().getImage(imgPath);
g.drawImage(img, 0, 0, 100, 100, this);
}
};
canvas.setSize(100, 100);
canvas.addMouseListener(this);
return canvas;
}
public void mouseClicked(MouseEvent e) {
// Determine user choice based on the clicked canvas
if (e.getSource() == rockCanvas) {
userChoice = "rock";
} else if (e.getSource() == paperCanvas) {
userChoice = "paper";
} else if (e.getSource() == scissorCanvas) {
userChoice = "scissor";
}
// Update user choice field
userChoiceField.setText("Your Choice: " + userChoice);
// Set computer choice only after user choice is made
setComputerChoice();
// Determine the result based on user and computer choices
String result = determineWinner(userChoice, computerChoice);
resultField.setText("Result: " + result);
computerChoiceField.setText("Computer's Choice: " + computerChoice);
}
private void setComputerChoice() {
int b = random.nextInt(3);
if (b == 0) computerChoice = "rock";
else if (b == 1) computerChoice = "paper";
else computerChoice = "scissor";
}
public String determineWinner(String userChoice, String computerChoice) {
if (userChoice.equals(computerChoice)) return "It's a tie!";
if (userChoice.equals("rock")) return computerChoice.equals("scissor") ? "You win!" : "You lose!";
if (userChoice.equals("paper")) return computerChoice.equals("rock") ? "You win!" : "You lose!";
return computerChoice.equals("paper") ? "You win!" : "You lose!";
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}