Mouse trap game! For this assignment, you are to write a GUI program that plays
ID: 3679255 • Letter: M
Question
Mouse trap game! For this assignment, you are to write a GUI program that plays a sick game. Here’s the idea: 1. Creates a GUI with a playing/drawing area. The size of the area should be 500 by 500 pixels. Alternatively, you can generate a dialog box(es) to let the user set the size. 2. Your program should randomly determine a position where the mouse “trap” is hiding. To do this, you would generate a random number for the x position and the y position. That point (x,y) is the “mouse trap”. However, it isn’t just the point, but any point plus or minus 10 pixels (both in the x and y coordinates). For example, if your random point was (123,345), then the “trap” is considered when the x position is from 103-133 AND the y position is from 335-355. (More on where the x,y position is to follow. 3. Produce a color picker (see chapter 13) to allow the user to pick a “mouse droppings” color. You should default to a dark brown. 4. The game begins when the mouse pointer is over your game area. The player tries to move the mouse around the board. As the mouse moves, you should capture the events (see chapter 14) and draw the mouse “droppings” in the desired color (see 3) on the screen. 5. The player continues to move the mouse, trying to color the board. But, as each mouse dropping is produced, you are to determine if the mouse has sprung the trap, i.e. has entered the trap area. 6. When the mouse enters the trap, you are to put up a message box with the total number of mouse droppings.
Explanation / Answer
The above description is about the famous mouse trap game which is designed in java. Simple modifications may be present but still using swings we can easily satisfy the above conditions.
PROGRAM CODE:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.util.Scanner;
class CatchTheRat extends JFrame
{
// The Rat
JLabel lb;
// Move it randomly!
Random r;
public CatchTheRat(int k)
{
// Set frame properties
setTitle("Catch The Rat");
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
// Set the background
setContentPane(new JLabel(new ImageIcon("background.jpg")));
// Set layout to the content pane
getContentPane().setLayout(new FlowLayout());
// Create the rat
lb=new JLabel(new ImageIcon("rat.png"));
// Add the rat
getContentPane().add(lb);
// Create Random object
r=new Random();
// Create a timer and call it for every k seconds
new Timer(k,new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
// Move the rat randomly, subtract 75, so that the rat should not meet the edges
lb.setLocation(r.nextInt(getWidth()-75),r.nextInt(getHeight()-75));
}
}).start();
// Add mouselistener, notify when user clicks it
lb.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent me)
{
// Create a beep sound when clicked to notify
Toolkit.getDefaultToolkit().beep();
// Also print it
System.out.println("Caught!");
}
});
// Maximize the frame
setExtendedState(MAXIMIZED_BOTH);
}
public static void main(String args[])
{
// Create Scanner object
Scanner s=new Scanner(System.in);
// Let the user enter his capability of catching the rat
System.out.println("Enter the speed");
// Read the input
int k=s.nextInt();
// Create the frame and send the value of k
new CatchTheRat(k);
}
}
Note: You can print the no.of times the rat is caught by simply keeping a variable say i and incrementing it every time that mouse is clicked. You can also make the game easier by writing mousePressed(MouseEvent) instead of mouseClicked(MouseEvent) and also by increasing the updation time.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.