package shotcaller; import java.awt.*; import java.applet.*; import java.util.*; import javax.swing.*; import java.math.*; import java.awt.event.*; import java.awt.event.MouseMotionListener; import javax.swing.event.MouseInputListener; public class shot extends Applet { //main class for game named shot, extends applet class int x1; //stores x position of the mouse int y1; //stores y position of the mouse double power; // stores the power of the shot double deltax; // stores the distance between the mouse and the white ball on the X plane double deltay; // stores the distance between the mouse and the white ball on the Y plane double x2; //stores the power after being translated into X coordinates; double y2; //stores the power after being translated into Y coordinates; int r; // stores x position of the ball int h; //stores y position of the ball double angle; // stores the angle of the shot double dist1; //stores the real distance of the shot boolean canshoot = true; //used to test if it is legal to shoot int i; //used in the i loop int j; //used in the j loop int count=0; // stores amout of balls sunk int shots =0; //stores amount of shots int bmax = 8; // sets maximum number of balls private final int WIDTH =550; //used to set size of window private final int HEIGHT = 300; //used to set size of window java.util.Timer timer; //declacres timer as a Timer object Graphics doublebuffer; //declares doublebuffer as a Graphics object Image offscreen; //declares offscreen as an image object Object hole; //declares hole ans a object hole[] H = new hole[6]; //creates a object array of the object hole Object ball; // declares ball as a object ball[] b = new ball [8]; //creates a object array of the object ball public void shoot(){ //creates shoot() method if(canshoot){ //checks the canshoot boolean deltax = (r -x1) ; // gets distance between ball's and mouse's x positions deltay = (h - y1) ; // gets distance between ball's and mouse's y positions angle = Math.atan2(deltax,deltay)/(Math.PI/180); //converts degrees into an angle dist1 = Math.sqrt(deltax+deltay); //calculates squared distance between the ball and mouse x2 = (power*Math.sin(angle*(Math.PI/180))); //converts angle back into x coordinates and times it by power y2 = (power*Math.cos(angle*(Math.PI/180))); //converts angle back into y coordinates and times it by power x2*=-1; //inverts x2; y2*=-1; //inverts y2; b[0].vx = b[0].vx + x2; //adds power onto the balls X speed b[0].vy = b[0].vy + y2; // adds power onto the balls y speed power=0; //rests power shots+=1; //adds 1 to the shot counter canshoot = false; // makes shooting after this function illegal } } public class motion implements MouseInputListener { public void add(){ addMouseMotionListener(this); addMouseListener(this); } public void mouseReleased(MouseEvent mouseEvent) { shoot(); } public void mousePressed(MouseEvent mouseEvent) { //if mouse pressed if(b[0].vx==0){ // if ball is not moving on the x plane if(b[0].vy==0){ //if ball is not moving on the y plane canshoot=true; //makes it legal for ball to shoot power=0; //resets power } } } public void mouseMoved(MouseEvent mouseEvent) { r = (int) b[0].x +12; // fetches x position of the ball and puts it into r variable h = (int) b[0].y +12 ;// fetches y position of the ball and puts it into h variable x1 = mouseEvent.getX(); // fetches x position of the mouse and puts it into x1 variable y1 = mouseEvent.getY(); // fetches y position of the mouse and puts it into y1 variable } public void mouseClicked(MouseEvent mouseEvent) {} //empty functions need to be declared for it to be possible to implement Mouseinputlistner public void mouseEntered(MouseEvent mouseEvent) {} public void mouseExited(MouseEvent mouseEvent) {} public void mouseDragged(MouseEvent mouseEvent) {} } public void create(){ //method that creats the ball and hole objects for(i = 0; i<6; i++){ // i for loop H[i]= new hole(); //creates an instance of the hole class and puts it into the H array } for (i= 0; i < bmax; i++){ // i for loop b[i] = new ball(); //creates an instance of the ball class and puts it into the b array } //set postions of the holes H[0].x=0; //top left H[0].y=0; H[1].x=520; //topright H[1].y=0; H[2].x=520; //bottom right H[2].y=270; H[3].x=260; //top middle H[3].y=0; H[4].x=260;//bottom middle H[4].y=270; H[5].x=0; //bottom left H[5].y=270; //sets positions of the balls b[0].x=70; b[1].x=387; b[2].x=414; b[3].x=444; b[4].x=444; b[5].x=444; b[6].x=414; b[7].x=-100; b[0].y=132; b[1].y=132; b[2].y=117; b[3].y=103; b[4].y=134; b[5].y=165; b[6].y=150; } public void init(){ //init method, used to start program create(); //runs creat method to create the balls motion m = new motion(); //creates an instance of the motion class m.add(); //runs method used to create the balls this.setSize(WIDTH,HEIGHT); // sets the size of the applet window timer = new java.util.Timer(); //creates an instance of the Timer class timer.schedule(new gameTask(),0,1*10); // creates a thread using TimerTask offscreen = createImage(640,480); //creats an image in the offscreen object doublebuffer = offscreen.getGraphics(); //defines double buffer } class gameTask extends TimerTask { public void run(){ power+=.5; //adds .5 to power if(power>40){ //sets dosent let power exceed 40 power=40; } for (i = 0; i<7; i++){ // i loop used to interact with all balls using one method if((b[i].x>0&&b[i].x<15&&b[i].y>0&&b[i].y<15) //big IF statment, used to check if ball has it any of the holes ||(b[i].x>520&&b[i].x<535&&b[i].y>0&&b[i].y<15) ||(b[i].x>520&&b[i].x<535&&b[i].y>270&&b[i].y<285) ||(b[i].x>260&&b[i].x<290&&b[i].y>0&&b[i].y<30) ||(b[i].x>260&&b[i].x<290&&b[i].y>270&&b[i].y<300) ||(b[i].x>0&&b[i].x<15&&b[i].y>270&&b[i].y<285)){ if(i>0){ //tests if ball is not the white ball b[i].dead= true ; H[0].sunk=1; } else { b[i].x=70; b[i].y=132; b[i].vx=0; b[i].vy=0; } } if(b[i].dead){ //tests if ball is "dead" b[i].dead(); //runs dead method used to get rid of ball } if(b[i].vx<0.1 && b[i].vx>-.1){ // used to stop balls xspeed b[i].vx=0; } if(b[i].vy<0.1 && b[i].vy>-.1){ //used to stop balls yspeed b[i].vy=0; } //these tests the ball against the boundries and get it to bounce back if(b[i].x>520){b[i].vx*=-.9;b[i].x-=1;} if(b[i].y>270){b[i].vy*=-.9;b[i].y-=1;} if(b[i].y<10){b[i].vy*=-.9;b[i].y+=1;} if(b[i].x<10){b[i].vx*=-.9;b[i].x+=1;} //applies physics to ball. i.e xspeed,yspeed, and decelleration b[i].vx*=b[i].damp; b[i].vy*=b[i].damp; b[i].x +=b[i].vx; b[i].y +=b[i].vy; for (j=1; j<7; j++){ //loop to test balls against other balls double dx = b[j].x-b[i].x; //gets x distance double dy = b[j].y-b[i].y; //gets y distance double dist = Math.sqrt(dx*dx+dy*dy); //finds real distance if(dist