Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.31 KB | None | 0 0
  1. package my_networked_game;
  2.  
  3. import java.awt.Point;
  4. import java.io.Serializable;
  5. import java.util.Random;
  6.  
  7. public class Box implements Serializable {
  8.  
  9.     /**
  10.      *
  11.      */
  12.     private static final long serialVersionUID = 1L;
  13.     // generic size
  14.     static final int box_width = 1000;
  15.     static final int box_height = 500;
  16.  
  17.     Point boxUpperRight;
  18.     Point boxUpperLeft;
  19.     Point boxLowerRight;
  20.     Point boxLowerLeft;
  21.  
  22.     Point rightHoleUpper;
  23.     Point rightHoleLower;
  24.  
  25.     Point leftHoleUpper;
  26.     Point leftHoleLower;
  27.  
  28.     Point ballLoc;
  29.  
  30.     Point[] paddleLoc;
  31.  
  32.     int paddleWidth;
  33.     int ballRadius = 20;
  34.     private int ballVx, ballVy;
  35.     private Random rand = new Random();
  36.  
  37.     int successCount = 0;
  38.     private boolean running = false;
  39.  
  40.     public boolean isRunning() {
  41.         return running;
  42.     }
  43.  
  44.     public Box() {
  45.         int box_top = 0;
  46.         int box_bottom = box_height;
  47.         int box_left = 0;
  48.         int box_right = box_width;
  49.  
  50.         boxUpperRight = new Point(box_right, box_top);
  51.         boxUpperLeft = new Point(box_left, box_top);
  52.         boxLowerRight = new Point(box_right, box_bottom);
  53.         boxLowerLeft = new Point(box_left, box_bottom);
  54.         rightHoleUpper = new Point(box_right, box_top + (box_bottom - box_top) / 4);
  55.         rightHoleLower = new Point(box_right, box_top + 3 * (box_bottom - box_top) / 4);
  56.         leftHoleUpper = new Point(box_left, box_top + (box_bottom - box_top) / 4);
  57.         leftHoleLower = new Point(box_left, box_top + 3 * (box_bottom - box_top) / 4);
  58.  
  59.         paddleWidth = (rightHoleLower.y - rightHoleUpper.y) / 3;
  60.         setGame(false);
  61.  
  62.     }
  63.  
  64.     void setGame(boolean startRunning) {
  65.         int box_top = 0;
  66.         int box_bottom = box_height;
  67.         int box_left = 0;
  68.         int box_right = box_width;
  69.  
  70.         // Start the ball out at a random spot
  71.         ballLoc = new Point(box_left + rand.nextInt(box_right - box_left),
  72.                 box_top + rand.nextInt(box_bottom - box_top));
  73.  
  74.         // Heuristic for generating random starting velocities ... maybe not the best
  75.         ballVx = (int) (50 - 100 * Math.random());
  76.         ballVy = (int) (50 - 100 * Math.random());
  77.  
  78.         paddleLoc = new Point[2];
  79.         paddleLoc[0] = new Point(box_right, (rightHoleUpper.y + rightHoleLower.y) / 2);
  80.         paddleLoc[1] = new Point(box_left, (leftHoleUpper.y + leftHoleLower.y) / 2);
  81.         if (startRunning)
  82.             running = true;
  83.     }
  84.  
  85.     public void setPaddleY(int yLoc, int clientIndex) {
  86.         paddleLoc[clientIndex].y = yLoc;
  87.         if (paddleLoc[clientIndex].y - paddleWidth / 2 < rightHoleUpper.y)
  88.             paddleLoc[clientIndex].y = rightHoleUpper.y + paddleWidth / 2;
  89.         if (paddleLoc[clientIndex].y + paddleWidth / 2 > rightHoleLower.y)
  90.             paddleLoc[clientIndex].y = rightHoleLower.y - paddleWidth / 2;
  91.  
  92.     }
  93.  
  94.     public void update() {
  95.         if (!running)
  96.             return;
  97.         ballLoc.x = ballLoc.x + ballVx;
  98.         ballLoc.y = ballLoc.y + ballVy;
  99.  
  100.         // check against right wall
  101.         if (ballLoc.x + ballRadius > boxUpperRight.x) {
  102.             if (ballLoc.y <= rightHoleUpper.y || ballLoc.y >= rightHoleLower.y) {
  103.                 // hits wall
  104.                 ballVx *= -1;
  105.                 ballLoc.x = boxUpperRight.x - ballRadius;
  106.             } else if (ballLoc.y >= paddleLoc[0].y - paddleWidth / 2 && ballLoc.y <= paddleLoc[0].y + paddleWidth / 2) {
  107.                 successCount += 1; // In hole but bounces off right paddle
  108.                 ballVx *= -1;
  109.                 ballLoc.x = boxUpperRight.x - ballRadius;
  110.                 System.out.println("In Hole and hits paddle");
  111.             } else {
  112.                 // In hole and missed by paddle
  113.                 running = false;
  114.                 System.out.println("In Hole and missed by paddle");
  115.             }
  116.         }
  117.  
  118.         // check against left wall
  119.         if (ballLoc.x + ballRadius > boxUpperLeft.x) {
  120.             if (ballLoc.y <= leftHoleUpper.y || ballLoc.y >= leftHoleLower.y) {
  121.                 // hits wall
  122.                 ballVx *= -1;
  123.                 ballLoc.x = boxUpperLeft.x - ballRadius;
  124.             } else if (ballLoc.y >= paddleLoc[1].y - paddleWidth / 2 && ballLoc.y <= paddleLoc[1].y + paddleWidth / 2) {
  125.                 successCount += 1; // In hole but bounces off right paddle
  126.                 ballVx *= -1;
  127.                 ballLoc.x = boxUpperLeft.x - ballRadius;
  128.                 System.out.println("In Hole and hits paddle");
  129.             } else {
  130.                 // In hole and missed by paddle
  131.                 running = false;
  132.                 System.out.println("In Hole and missed by paddle");
  133.             }
  134.         }
  135.  
  136.         // check against the bottom wall
  137.         if (ballLoc.y + ballRadius > boxLowerRight.y) {
  138.             ballVy *= -1;
  139.             ballLoc.y = boxLowerRight.y - ballRadius;
  140.         }
  141.  
  142.         // check against the top wall
  143.         if (ballLoc.y - ballRadius < boxUpperRight.y) {
  144.             ballVy *= -1;
  145.             ballLoc.y = boxUpperRight.y + ballRadius;
  146.         }
  147.  
  148.     }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement