Automated game of pong using Java Robot class

Using the Java Robot class, I have written a small utility that plays an automated game of pong against the computer.

It constantly monitors the left side of the game, and when a ball enters the field, it moves the mouse cursor to the position of the ball.

Demo video:


This is a simple introduction to image-recognition and mouse-movement in Java. Feel free to use the code, optimize or improve.

Usage of the RobotPong object::

  • Create an object using the coordinate of the part of the screen that needs to be monitored.
  • Start the object using the start() method, this will start the thread containing the object.
  • It is possible to run multiple threads, each playing their own game of pong.

The game itself can be found at the MegaFunGames webpage.

The code:

import java.awt.*;
import java.awt.image.*;
 
/*
 
©2010 Bobbie Smulders
 
RobotPong plays an automated game of Pong.
Original game can be found here:
http://www.megafungames.com/games/flash_pong/aff.php
 
*/
 
public class RobotPong extends Thread {
 
	public Rectangle rect;
 
	public RobotPong(int x, int y, int width, int height) {
		rect = new Rectangle(x, y, width, height);
	}
 
	public void run() {
		try{
			Robot robot = new Robot();
 
			while(true) {
				BufferedImage bufferedImage = robot.createScreenCapture(rect);
 
				search:
				for (int x_scan = 0; x_scan < rect.width; x_scan++) {
					for(int y_scan = 0; y_scan < rect.height; y_scan++) {
						if(bufferedImage.getRGB(x_scan, y_scan) == -1) {
							robot.mouseMove(rect.x + x_scan, y_scan + rect.y);
							break search;
						}
					}
				}
				robot.delay(40);
			}
		}
		catch(AWTException e) {
			e.printStackTrace();
		}
	}
 
	public static void main(String[] args) {
		new RobotPong(44, 90, 100, 255).start();
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>