Aug 061999
 

Well I can’t even remember slapping this together but anyway.
You need Java to view this applet.

Seems to be a bat and ball program that will play itself if you don’t play and if you hover your mouse you can play. There are no points or serves, looks like some test I was doing for something.

import java.applet.*;
import java.awt.*;
import  java.awt.event.*;

public class AtBat extends Applet implements  Runnable, MouseMotionListener, MouseListener {

private  Image i_offscreen;
private Graphics g_offscreen;
private int  yHeight, xWidth, ballDiam, batWidth, gameDiff;
private Rectangle  rec, r_ball, r_bat[];
private boolean   mouseActive=false;

private Thread ballMotion, batMotion;

public AtBat  (){
batWidth = 20;
ballDiam = 3;
gameDiff =  1;
}

public void init(){
rec =  this.getBounds();
xWidth = rec.width;
yHeight =  rec.height;

r_ball = new Rectangle  (rec.width/2,rec.height/2,ballDiam,ballDiam);

rec.height=rec.height-r_ball.width;
rec.width=rec.width-r_ball.width;
rec.x=r_ball.width;
rec.y=r_ball.width;

setSize(200,200);
setBackground(Color.black);

this.addMouseMotionListener(this);
this.addMouseListener(this);

i_offscreen =  createImage(xWidth,yHeight);
g_offscreen =  i_offscreen.getGraphics();

ballMotion = new  Thread(this, "AtBall");
ballMotion.start();

r_bat = new Rectangle[2];
r_bat[0] = new  Rectangle(r_ball.x-batWidth/2,yHeight-15,batWidth,2);
r_bat[1] =  new Rectangle(r_ball.x-batWidth/2,15,batWidth,2);

batMotion = new Thread(this, "AtBat");
batMotion.start();
}

public void paint(Graphics g)
{
//Clear  Screen
g_offscreen.setColor(Color.black);
g_offscreen.fillRect(0,0,xWidth,yHeight);

//Draw  Ball
g_offscreen.setColor(Color.white);
synchronized  (r_ball) {
g_offscreen.fillRect(r_ball.x,r_ball.y,ballDiam,ballDiam);
}

//Draw Bat (computer)
for (int  a=0;a<r_bat.length;a++)
g_offscreen.fillRect(r_bat[a].x,r_bat[a].y,r_bat[a].width,r_bat[a].height);

//Draw Image
g.drawImage(i_offscreen,0,0,this);
//       System.out.println(p_ball);
}

public void  update(Graphics g){
paint(g);
}

public void  run (){
if (Thread.currentThread()==ballMotion)
ballMotion();
if (Thread.currentThread()==batMotion)
batMotion();
}

private void ballMotion(){
//Ball Speed x & y
double d_x_vec = 0.7;
double  d_y_vec = 1.0;
//Ball location
double d_x_pos =  (double)xWidth/2;
double d_y_pos = (double)yHeight/2;

//Recs for fine ball crash detection on bat
Rectangle  batSplit[] = new Rectangle[5];

do {
synchronized (r_ball){
d_x_pos = d_x_pos + d_x_vec;
d_y_pos = d_y_pos + d_y_vec;
r_ball.x=(int)d_x_pos;
r_ball.y=(int)d_y_pos;
}
if (!rec.intersects(r_ball)){
if  ((r_ball.y<1)||(r_ball.y>yHeight-ballDiam))
d_y_vec=d_y_vec*(-1);
if  ((r_ball.x<1)||(r_ball.x>xWidth-ballDiam))
d_x_vec=d_x_vec*(-1);
}

for (int  a=0;a<r_bat.length;a++)
if  (r_bat[a].intersects(r_ball)){
//Find where the ball  hit on the bat
batSplit[0]=new  Rectangle(r_bat[a].x,r_bat[a].y,2,r_bat[a].height);
batSplit[1]=new  Rectangle(batSplit[0].x+batSplit[0].width,r_bat[a].y,(r_bat[a].width-4)/3,r_bat[a].height);
batSplit[2]=new  Rectangle(batSplit[1].x+batSplit[1].width,r_bat[a].y,(r_bat[a].width-4)/3,r_bat[a].height);
batSplit[3]=new  Rectangle(batSplit[2].x+batSplit[2].width,r_bat[a].y,(r_bat[a].width-4)/3,r_bat[a].height);
batSplit[4]=new  Rectangle(batSplit[3].x+batSplit[3].width,r_bat[a].y,2,r_bat[a].height);

//middle 1/3
if  (r_ball.intersects(batSplit[2])){
System.out.println("Middle");
}
//left 1/3
else if  (r_ball.intersects(batSplit[1])){
System.out.println("Left 1/3");
}
//right 1/3
else if  (r_ball.intersects(batSplit[3])){
System.out.println("Right 1/3");
}
//left corner
else if  (r_ball.intersects(batSplit[0])){
d_x_vec=d_x_vec*(-1);
System.out.println("Left  corner");
}
//right side
else if (r_ball.intersects(batSplit[4])){
d_x_vec=d_x_vec*(-1);
System.out.println("Right  Corner");
}
d_y_vec=d_y_vec*(-1);
}
try {Thread.sleep(5);}
catch  (Exception e){}
repaint();
} while (true);
}

private void batMotion(){
do {
//For both  bats
for (int a=0;a<r_bat.length;a++){
//Sync computer bat with ball on x axis.
if  (mouseActive)
r_bat[0]=batBrain(r_bat[0]);
//                r_bat[0].x=p_ball.x-(batWidth/2);
else
r_bat[a].x=r_ball.x-(batWidth/2);
}
batCrash();
try {Thread.sleep(100);}
catch  (Exception e){}
} while (true);
}

private void  batCrash(){
//Left & right crash detection
for  (int a=0;a<r_bat.length;a++){
if (r_bat[a].x<0)
r_bat[a].x=0;
if  (r_bat[a].x+r_bat[a].width>xWidth)
r_bat[a].x=xWidth-r_bat[a].width;
}
}

private Rectangle batBrain(Rectangle thisBat){
//If the ball  & bat is in the top or bottom half
if  (((thisBat.y<yHeight/2)&&(r_ball.y<yHeight/2))||(thisBat.y>yHeight/2)&&(r_ball.y>yHeight/2))
if (r_ball.x<thisBat.x){
thisBat.x=thisBat.x-gameDiff;
} else {
thisBat.x=thisBat.x+gameDiff;
}
return thisBat;
}

public void mouseMoved(MouseEvent me){
r_bat[1].x=me.getX()-(batWidth/2);
batCrash();
}

public void mouseEntered(MouseEvent me){
mouseActive=true;
}
public void mouseExited(MouseEvent me){
mouseActive=false;
}
public void mouseClicked(MouseEvent  me){
mouseActive=true;
}
public void  mousePressed(MouseEvent me){
mouseActive=true;
}

public void mouseDragged(MouseEvent me){
}
public void  mouseReleased(MouseEvent me){
}
}
Share

 Leave a Reply

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=""> <s> <strike> <strong>