简体   繁体   English

如何在Java图形上使用布尔值使积木消失?

[英]how do i make bricks disappear using boolean on java graphics?

I am making a demo version of Atari Arkanoid with Java at school. 我在学校用Java制作Atari Arkanoid的演示版。 How would I make bricks that disappear once hit using boolean? 我如何制作使用布尔值击中后消失的砖块? It has to collide first, then disappear and not reappear. 它必须先发生碰撞,然后消失并且不再出现。

    //brickbase
    import java.awt.*;
    public class Brickbase
    {
       private int myX; // x and y coordinates of center
       private int myY;
       private int myXWidth;
       private int myYWidth;
       private Color myColor;

       public Brickbase(int x, int y, int xWidth, int yWidth, Color c)
       {
          myX = x;
          myY = y;
          myXWidth = xWidth;
          myYWidth = yWidth;
          myColor = c;  
       }
        // accessor methods
       public int getX()
       {
          return myX;
       }
       public int getY()
       {
          return myY;
       }
       public int getXWidth()
       {
          return myXWidth;
       }
       public int getYWidth()
       {
          return myYWidth;
       }
       public Color getColor() 
       { 
          return myColor;
       }
       // modifier methods
       public void setX(int x)
       {
          myX= x;
       }

       public void setXWidth(int xWidth)
       {
          myXWidth = xWidth;
       }

       public void setY(int y)
       { 
          myY=y;
       }

       public void setYWidth(int yWidth)
       {
          myYWidth =yWidth;
       }
       public void jump(int rightEdge, int bottomEdge)
       {
          myX= rightEdge;
          myY= bottomEdge;

       }

       public void setColor(java.awt.Color c)
       {
          myColor = c;
       }
        //   instance methods

       public void draw(Graphics myBuffer) 
       {
            myBuffer.setColor(getColor());
             myBuffer.fillRect(getX(), getY(), getXWidth(),                 getYWidth());

       }   

       public boolean inBrickbase(arkBall ark)
       {
          for(int x = getX(); x <= getX() + getXWidth(); x++)           //starts at upper left corner(x,y)
             for(int y = getY(); y <= getY() + getYWidth(); y++)
                if(distance(x, y, ark.getX(), ark.getY()) <= ark.getRadius() ) //checks every point on the bumper
                   return true;            
          return false;
       }  



       private double distance(double x1, double y1, double x2, double y2)
       {
          return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
       }    
    }

When the ball named ark hits the brick, it bounces off, but the brick is not affected. 当名为方舟的球撞到砖头时,它反弹了,但砖头不受影响。 I know I'm supposed to use boolean, but what would I do? 我知道我应该使用布尔值,但是我该怎么办? My teacher asks us to do research, but my Java textbook does not incorporate booleans or graphics because it is not part of the AP Curriculum. 我的老师要求我们进行研究,但是我的Java教科书没有包含布尔值或图形,因为它不是AP课程的一部分。 The sample code for Arkanoid are available, but they are too complicated and sometimes cannot be applied to my code. 可以使用Arkanoid的示例代码,但是它们太复杂了,有时不能应用于我的代码。

So, you need (what's commonly known as a) "flag" 因此,您需要(通常称为a)“标志”

The purpose of this "flag" is to manage a state and provide a means by which you can check for that specific state. 此“标志”的目的是管理状态并提供一种可以检查特定状态的方法。

In this case, we want to make the brick invisible when some condition is met. 在这种情况下,我们希望在满足某些条件时使砖不可见。

So, let's start by introducing our "flag" as an instance field to the Brickbase class... 因此,让我们从在Brickbase类中引入“标志”作为实例字段开始...

public class Brickbase {

    private boolean visible = true;

Sweet, we know have a means to determine if the brick is visible or not and possibly change that state. 亲爱的,我们知道有一种方法可以确定砖块是否可见,并可能更改该状态。

So, obviously, the first thing we might want to do is determine if the brick should be painted or not, based on the state of the "flag", maybe something like... 因此,显然,我们可能要做的第一件事是根据“标志”的状态确定是否应该对砖进行粉刷,例如……

public void draw(Graphics myBuffer) {
    if (visible) {
        myBuffer.setColor(getColor());
        myBuffer.fillRect(getX(), getY(), getXWidth(), getYWidth());
    }
}

Next, we want to someway to change the state of our "flag", in your case when the brick is hit, maybe something like... 接下来,我们希望以某种方式更改“标志”的状态,以您遇到砖块的情况为例,例如...

public boolean inBrickbase(arkBall ark) {
    if (visible) {
        for (int x = getX(); x <= getX() + getXWidth(); x++) //starts at upper left corner(x,y)
        {
            for (int y = getY(); y <= getY() + getYWidth(); y++) {
                if (distance(x, y, ark.getX(), ark.getY()) <= ark.getRadius()) //checks every point on the bumper
                {
                    visible = false;
                    return true;
                }
            }
        }
    }
    return false;
}

Now, before someone points it out, I might consider removing the brick from what ever model is managing it when inBrickbase returns true , thus saving all the hassle and reducing the overall computation overhead, but I don't have enough context to derive a solution for that 现在,在有人指出之前,我可能会考虑在inBrickbase返回true时从模型正在管理的模型中删除砖,从而节省了所有麻烦并减少了总体计算开销,但是我没有足够的上下文来得出解决方案为了那个原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM