Advertisement
NW200010000

Test 1

Sep 23rd, 2021
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. package rovingdots3;
  2.  
  3. import java.awt.Rectangle;
  4. import java.awt.geom.Rectangle2D;
  5. import java.util.Random;
  6.  
  7. public class Dot {
  8.    
  9.     private String name = "";
  10.     private int x = 300;
  11.     private int y = 300;
  12.     private int deltaX = 1;
  13.     private int deltaY = 1;
  14.     private int width = 30;
  15.     private int height = 30;
  16.     private int speedX = 1;
  17.     private int speedY = 1;
  18.     private int speedLimit = 12;
  19.     Random rand = new Random();
  20.     long lastGeneration = 0;
  21.     int waitTime = 2000;
  22.    
  23.     public Dot(String newName, int newX, int newY) {
  24.         name = newName;
  25.         x = rand.nextInt(1200);
  26.         y = rand.nextInt(700);
  27.         deltaX = nonZeroRandom();
  28.         deltaY = nonZeroRandom();
  29.         speedX = nonZeroRandom();
  30.         speedY = nonZeroRandom();
  31.     }  
  32.  
  33.     public String getName() {
  34.         return name;
  35.     }
  36.  
  37.     public int getX() {
  38.         return x;
  39.     }
  40.  
  41.     public int getY() {
  42.         return y;
  43.     }
  44.  
  45.     public int getDeltaX() {
  46.         return deltaX;
  47.     }
  48.  
  49.     public int getDeltaY() {
  50.         return deltaY;
  51.     }
  52.  
  53.     public int getWidth() {
  54.         return width;
  55.     }
  56.  
  57.     public int getHeight() {
  58.         return height;
  59.     }
  60.  
  61.     public int getSpeedX() {
  62.         return speedX;
  63.     }
  64.  
  65.     public int getSpeedY() {
  66.         return speedY;
  67.     }
  68.  
  69.     void moveIt(int newW, int newH) {
  70.         x = x + deltaX * speedX;
  71.         y = y + deltaY * speedY;
  72.        
  73.         if (x > (newW - width)) {
  74.             deltaX = -1;
  75.             speedUp();
  76.         }
  77.         if (x < 0) {
  78.             deltaX = 1;
  79.             speedUp();
  80.         }
  81.         if (y > (newH - height)) {
  82.             deltaY = -1;
  83.             speedUp();
  84.         }
  85.         if (y < 0) {
  86.             deltaY = 1;
  87.             speedUp();
  88.         }
  89.     }
  90.  
  91.     Rectangle2D getBounds() {
  92.         Rectangle r = new Rectangle(x, y, width, height);
  93.         return r;
  94.     }
  95.  
  96.     private int nonZeroRandom() {
  97.         int rando = rand.nextInt(3)+1;
  98.         boolean rb = rand.nextBoolean();
  99.         if (rb) {
  100.             rando  = 0 - rando;
  101.         }
  102.         return rando;
  103.     }
  104.  
  105.     boolean generateOK() {
  106.         long currentTime = System.currentTimeMillis();
  107.         if (currentTime - lastGeneration > waitTime) {
  108.             return true;
  109.         }
  110.         return false;
  111.     }
  112.  
  113.     void changeDirection() {
  114.         x = x - deltaX;
  115.         y = y - deltaY;
  116.         deltaX = 0 - deltaX;
  117.         deltaY = 0 - deltaY;
  118.        
  119.     }
  120.  
  121.     void generated() {
  122.         lastGeneration = System.currentTimeMillis();
  123.     }
  124.  
  125.     private void speedUp() {
  126.         if (speedX <= speedLimit) {
  127.             speedX++;
  128.             speedY++;
  129.         }
  130.     }
  131.    
  132.    
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement