Home | Tools and articles | Donate
Israeli Space Force | Santa Presents Delivery
 

Ping Pong with GameApplet / Alex Zak


This is the first step of the ping-pong java game, as described here





And here is the code:

package pingpong1;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

public class PingPong extends Game {
public static final int BATWIDTH=60;
public static final int BATHEIGHT=20;
private int batX=150, batY=200;

public void init(){
resize(600,600);
setSleepTime(50);

}
public Color getBackgroundColor(){
return Color.black;
}

public void paint(Graphics g) {
g.setColor(Color.red); //To make the bat red
Point mp=gp.getMousePosition(); //point mp will hold the coordinates of the mouse
if(mp!=null)
{
//Then, if the mouse is in the game window, it's coordinates are copied to
//batX and batY. If the mouse is out of the game window, batX and batY will not change.
batX=mp.x;
batY=mp.y;
}
//finaly, to draw the bat, I call the Garphics method fillRect, that draws and fills
//a rectangle.
g.fillRect(batX-BATWIDTH/2,batY-BATHEIGHT/2,BATWIDTH,BATHEIGHT);
}
}


Questions, complaints and requests? Send them to admin@mysuperiorgames.com