Skip to main content

Catch The Rat: Simple Game in Java

A simple, shortest and easiest game that could be written in Java using Swing components and event handling.


What's your opinion about rats and catching them? Is it easier? Well, you don't do it in practical and use some rat killers to kill them. Fine, my rat can't be killed, in fact you'll just have to catch it! It makes a beep sound when you do and i will print whether you've done it or not.

Fine, what have you done?

A simple swing program that is much easier than you what you find on the internet. It is dead simple. Just look at the code. Coding it is easy but playing it is difficult! Try how many times you can!

Catch The Rat!


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 (just for a good look)

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);


}

}

Improving the game!


You can also 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.

Video



Comments

Popular posts from this blog

Setting Background Image in JFrame - Swing

Here is sample tutorial, a simple trick that enables you to set background image for JFrame.



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class BackgroundImageJFrame extends JFrame
{
JButton b1;
JLabel l1;
public BackgroundImageJFrame()
{
setTitle("Background Color for JFrame");
setSize(400,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
/*
One way
-----------------
setLayout(new BorderLayout());
JLabel background=new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png"));
add(background);
background.setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am a button");
background.add(l1);
background.add(b1);
*/
// Another way
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png")));
setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am …

Add two numbers in JSP easily

Here is an example on adding up two numbers in JSP. This is very simple as you see below. This requires very small piece of code. All i have done here is created a HTML file containing the input fields which contain the numbers to add up.Now the values in the input field are of type String, so i have done type casting and converted them into int using Integer.parseInt(). See how easy it looks.