0

For my project we have to have four square moving and bouncing off the sides and when it hits a side it changes color. When you click on one of the squares it freezes and turns red. So far i have the four squares and when I click them it says it freezes them. However, I'm not positive how to get them to move and bounce off the sides. Below is the code i have written so far.

MAIN:

import java.util.Random;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        StationarySquare[] squares = new StationarySquare[4];
        Random rng = new Random();

        for (int i = 0; i < squares.length; i++) {
            squares[i] = new StationarySquare(rng.nextDouble(), rng.nextDouble());
        }

        while (true) {
            StdDraw.clear();

            int count = 0;

            for (int i = 0; i < squares.length; i++) {
                squares[i].draw();

                if (StdDraw.mousePressed()
                        && squares[i].containsPoint(StdDraw.mouseX(), StdDraw.mouseY())) {
                    squares[i].freeze();
                }

                if (squares[i].isFrozen()) {
                    count++;
                }
            }
            StdDraw.text(0.5, 0.5, "Frozen: " + count);

            StdDraw.show(25);
        }
    }
}

My other class

import java.util.Random;

public class StationarySquare {
    private double x;
    private double y;
    private double halfLength;
    private int red;
    private int green;
    private int blue;
    private boolean isFrozen;

    public StationarySquare(double x, double y) {
        this.x = x;
        this.y = y;
        halfLength = 0.1;
        isFrozen = false;
        randomColor();
    }

    public void draw() {
        if (isFrozen) {
            StdDraw.setPenColor(StdDraw.RED);
        } else {
            StdDraw.setPenColor(red, green, blue);
        }
        StdDraw.filledSquare(x, y, halfLength);
    }

    public void randomColor() {
        Random rng = new Random();

        red = rng.nextInt(256);
        green = rng.nextInt(256);
        blue = rng.nextInt(256);
    }

    public void freeze() {
        isFrozen = true;
    }

    public boolean containsPoint(double a, double b) {
        return a > x - halfLength &&
                a < x + halfLength &&
                b > y - halfLength &&
                b < y + halfLength;
    }

    public boolean isFrozen() {
        return isFrozen;
    }
}
Lavaman65
  • 863
  • 1
  • 12
  • 22
  • depends in which way you want them to move, as in horizontally, vertically or randomly. Anyway the clue is obviously you can achieve that using the 'x' and 'y' fields – Ousmane D. Mar 03 '17 at 21:41
  • Each rect needs direction variables for x and y. Each time you refresh the display, change the rects x and y by these values. (They can be negative or positive). – 001 Mar 03 '17 at 21:49
  • I want them to move randomly around the screen. I just dont know how to get that. I know I have to add velocity and stuff i just dont know which class or where to put it – jack.cleary Mar 04 '17 at 01:20
  • I was working on the same project, you can see my solution here http://stackoverflow.com/questions/42587492/freeze-moving-rectangles/42587949?noredirect=1#comment72391754_42587949 – donkey brian Mar 07 '17 at 21:22

0 Answers0