解冻碰撞矩形?

时间:2017-11-12 21:40:01

标签: java project intersection rectangles stddraw

我需要项目帮助。对于这个项目,我们已经创建了移动的矩形,并且#34;冻结"点击时。如果它们与另一个碰撞,我们也应该将冻结的矩形解冻。我已经完成了第一步但是如果它们发生碰撞我似乎无法解开矩形。如果这没有意义,我道歉。我是Java和堆栈溢出的新手。这是我的班级代码。

    import edu.princeton.cs.introcs.StdDraw;
    import java.util.Random;
    import java.awt.Color;

    public class MovingRectangle {

    //canvas = canvasSize;
    public int width;               //width of rectangle
    public int height;              //height of rectangle
    public double xVelocity;            //x velocity
    public double yVelocity;            //y velocity

    public int xPosition;           //x starting position
    public int yPosition;           //y starting position

    private boolean isFrozen;       //the rectangle is still moving
   // private int R, G, B;  
    Random rng = new Random();

   public int R = rng.nextInt(256);
   public int G = rng.nextInt(256);
    public  int B = rng.nextInt(256); //RGB variables
    public int canvas;
    public Color c;//canvas size

        public MovingRectangle(int x, int y, int w, int h, double xv, double yv, int canvasSize) {
                xPosition = x;
                yPosition = y;
                width = w;
                height = h;
                xVelocity = xv;
                yVelocity = yv;
                canvas = canvasSize;


                isFrozen = false;

                    c =new Color(R,G,B);



        }

        //draws rectangle
        public void draw() {
//          Random rng = new Random();
//          R = rng.nextInt(256);
//          G = rng.nextInt(256);
//          B = rng.nextInt(256);
//          c = new Color(R,G,B);
            StdDraw.setPenColor(c);

            StdDraw.filledRectangle(xPosition, yPosition, width/2, height/2);
        }

        //pick a new random color
//      public void color() {
//          Random rng = new Random();
//          
//          R = rng.nextInt(256);
//          G = rng.nextInt(256);
//          B = rng.nextInt(256);
//          c = new Color(R,G,B);
//      }

        //updates position every frame
        public void move() {

            if (isFrozen == false) {
            xPosition += xVelocity;
            yPosition += yVelocity;




//              xPosition += 0;
//              yPosition += 0;

            //Check for hitting sides of canvas
            if (xPosition > canvas) {
                xVelocity *= -1;

                //color();

            }
            if( xPosition < 0) {
                xVelocity *= -1;

                //color();
            }
            if (yPosition > canvas ) {
                yVelocity *= -1;
                //color();
            }
           if(yPosition <= 0) {
               yVelocity*=-1;
               //color();
           }


            }
            }
        public boolean ifColliding(MovingRectangle rectangle) {

            int rightedge = rectangle.xPosition + rectangle.width/2;
            int leftedge = rectangle.xPosition - rectangle.width/2;
            int topedge = rectangle.yPosition + rectangle.height/2;
            int bottomedge = rectangle.yPosition - height/2;
            int rightedge2 = this.xPosition + this.width/2;
            int leftedge2 = this.xPosition - this.width/2;
            int topedge2 = this.yPosition + this.height/2;
            int bottomedge2 = this.yPosition - this.height/2;
            if(leftedge<rightedge2 && leftedge2<rightedge && topedge>bottomedge2 && bottomedge<topedge2) {

                return true;
            }



            else {
                return false;

            }



        }


        public void freeze() {
            if(StdDraw.mousePressed() && StdDraw.mouseX() <= xPosition+width && StdDraw.mouseX() >= xPosition-width && StdDraw.mouseY() <=yPosition+height &&StdDraw.mouseY() >= yPosition-height) {
            xVelocity=0;
            yVelocity=0;
            c= new Color(100,0,0);
            isFrozen= true;




            }
        }
        public boolean isFrozen() {



            return isFrozen;


        }
        public void winState() {
            StdDraw.clear();
        }

        public void setFrozen(boolean val) {

            isFrozen = false;
            if (isFrozen == false) {
                xPosition += xVelocity;
                yPosition += yVelocity;
            }


        }





//      public void stop() {
//          isMoving = false;
//      }




}

我的司机。

    import edu.princeton.cs.introcs.StdDraw;
    import java.util.Random;
    import java.util.Scanner;
    import java.awt.Color.*;

    import edu.princeton.cs.introcs.StdDraw;

    public class RectangleDriver {

    public static final int canvas = 500;

    public static void main(String[] args) {

        //set canvas size
        StdDraw.setCanvasSize(canvas,canvas);
        StdDraw.setXscale(canvas, 0);
        StdDraw.setYscale(0, canvas);

        //assign variables
        Random rng = new Random();


        //make a rectangle
        MovingRectangle[] rectangles   = new MovingRectangle[5]; //(x, y, w, h, xv, yv, canvas);
        for(int i =0;i<rectangles.length;i++) {
            int x = rng.nextInt(canvas);            //xPosition
            int y = rng.nextInt(canvas);            //yPosition
            int w = rng.nextInt(100)+20;        //width
            int h = rng.nextInt(100)+20;        //height
            double xv = rng.nextInt(5)+2;           //xVelocity
            double yv = rng.nextInt(5)+2;           //yVelocity
            rectangles[i]= new MovingRectangle(x,y,w,h,xv,yv,canvas);

        }


        //while isMoving, the rectangles move

        while (true) {
            StdDraw.clear();
            for(int i =0; i<rectangles.length;i++) {
                rectangles[i].move();
                rectangles[i].draw();
                rectangles[i].freeze();
                System.out.println(rectangles[i].isFrozen());

                //for 0
                if(rectangles[i].ifColliding(rectangles[1])){
                    if(rectangles[0].isFrozen()) {
                        rectangles[0].setFrozen(false);
                    }

                }
                if(rectangles[i].ifColliding(rectangles[2])){
                    if(rectangles[0].isFrozen()) {
                        rectangles[0].setFrozen(false);
                    }

                }
                if(rectangles[i].ifColliding(rectangles[3])){
                    if(rectangles[0].isFrozen()) {
                        rectangles[0].setFrozen(false);
                    }

                }
                if(rectangles[i].ifColliding(rectangles[4])){
                    if(rectangles[0].isFrozen()) {
                        rectangles[0].setFrozen(false);
                    }

                }

                //for 1 

                if(rectangles[1].ifColliding(rectangles[0])){
                    if(rectangles[1].isFrozen()) {
                        rectangles[1].setFrozen(false);
                    }

                }
                if(rectangles[1].ifColliding(rectangles[2])){
                    if(rectangles[1].isFrozen()) {
                        rectangles[1].setFrozen(false);
                    }

                }
                if(rectangles[1].ifColliding(rectangles[3])){
                    if(rectangles[1].isFrozen()) {
                        rectangles[1].setFrozen(false);
                    }

                }
                if(rectangles[1].ifColliding(rectangles[4])){
                    if(rectangles[1].isFrozen()) {
                        rectangles[1].setFrozen(false);
                    }


                }
               // for 2
                if(rectangles[2].ifColliding(rectangles[0])){
                    if(rectangles[2].isFrozen()) {
                        rectangles[2].setFrozen(false);
                    }

                }
                if(rectangles[2].ifColliding(rectangles[1])){
                    if(rectangles[2].isFrozen()) {
                        rectangles[2].setFrozen(false);
                    }

                }
                if(rectangles[2].ifColliding(rectangles[3])){
                    if(rectangles[2].isFrozen()) {
                        rectangles[2].setFrozen(false);
                    }

                }
                if(rectangles[2].ifColliding(rectangles[4])){
                    if(rectangles[2].isFrozen()) {
                        rectangles[2].setFrozen(false);
                    }

                }         
                //for 3
                if(rectangles[3].ifColliding(rectangles[1])){
                    if(rectangles[3].isFrozen()) {
                        rectangles[3].setFrozen(false);
                    }

                }
                if(rectangles[3].ifColliding(rectangles[2])){
                    if(rectangles[3].isFrozen()) {
                        rectangles[3].setFrozen(false);
                    }

                }
                if(rectangles[3].ifColliding(rectangles[0])){
                    if(rectangles[3].isFrozen()) {
                        rectangles[3].setFrozen(false);
                    }

                }
                if(rectangles[3].ifColliding(rectangles[4])){
                    if(rectangles[3].isFrozen()) {
                        rectangles[3].setFrozen(false);
                    }

                }
                //for 4
                if(rectangles[4].ifColliding(rectangles[1])){
                    if(rectangles[4].isFrozen()) {
                        rectangles[4].setFrozen(false);
                    }

                }
                if(rectangles[4].ifColliding(rectangles[2])){
                    if(rectangles[4].isFrozen()) {
                        rectangles[4].setFrozen(false);
                    }

                }
                if(rectangles[4].ifColliding(rectangles[3])){
                    if(rectangles[4].isFrozen()) {
                        rectangles[4].setFrozen(false);
                    }

                }
                if(rectangles[4].ifColliding(rectangles[0])){
                    if(rectangles[4].isFrozen()) {
                        rectangles[4].setFrozen(false);
                    }

                }

               }




            if(rectangles[0].isFrozen()==true && rectangles[1].isFrozen()==true && rectangles[2].isFrozen()==true && rectangles[3].isFrozen()==true && rectangles[4].isFrozen()==true) {
                StdDraw.clear();
                StdDraw.text(250, 250, "You win!");

            }
            StdDraw.show(100);
        }


    }
}       

0 个答案:

没有答案