Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 碰撞检测与反应_Java_Arrays_Object_Collision - Fatal编程技术网

Java 碰撞检测与反应

Java 碰撞检测与反应,java,arrays,object,collision,Java,Arrays,Object,Collision,我正在写一个小游戏,在屏幕上创建20个气球,释放鼠标扩展气球。当一个气球接触到另一个气球时,它们应该会“弹出”,但目前,当我单击一个气球时,它会弹出一个随机气球,并抛出一个“数组索引超出范围”异常。我绞尽脑汁想弄明白为什么我的代码不起作用,但就是弄不明白。以下是导致问题的一些代码: import comp102.*; import java.util.*; import java.awt.Color; public class BalloonGame implements UIButtonLi

我正在写一个小游戏,在屏幕上创建20个气球,释放鼠标扩展气球。当一个气球接触到另一个气球时,它们应该会“弹出”,但目前,当我单击一个气球时,它会弹出一个随机气球,并抛出一个“数组索引超出范围”异常。我绞尽脑汁想弄明白为什么我的代码不起作用,但就是弄不明白。以下是导致问题的一些代码:

import comp102.*;
import java.util.*;
import java.awt.Color;

public class BalloonGame implements UIButtonListener, UIMouseListener{
// Fields
private final int numBalloons = 20;
private int currentScore;   // the score for the current game
private int highScore = 0;  // highest score in all games so far.
private int totalPopped = 0;
Balloon balloons[] = new Balloon[numBalloons];

// Constructor 
/** Set up the GUI, start a new game.
 */
public BalloonGame(){
    UI.setMouseListener(this);
    UI.addButton("New Game", this);
    UI.addButton("Lock Score", this);
    this.newGame();
}

// GUI Methods to respond to buttons and mouse
/** Respond to button presses, to start a new game and to end the current game  */
public void buttonPerformed(String cmd){
    if (cmd.equals("New Game")) { this.newGame(); }
    else if (cmd.equals("Lock Score")) { this.endGame(); }
}

/** Respond to mouse released with the main action of the game*/
public void mousePerformed(String action, double x, double y) {
    if (action.equals("released")) {
        this.doAction(x, y);
    }
}

/** Start the game:
Clear the graphics pane
Initialise the score information 
Make a new set of Balloons at random positions
 */
public void newGame(){
    UI.clearGraphics();
    this.currentScore = 0;
    this.totalPopped = 0;
    for (int i = 0; i < this.balloons.length; i++) {
        this.balloons[i] = new Balloon(50 + Math.random()*400, 50 + Math.random()*400);
        this.balloons[i].draw();
    }
    UI.printMessage("New game: click on a balloon.  High score = "+this.highScore);
}

/** Main game action.
Find the balloon at (x,y) if any,
Expand it 
Check whether it is touching another balloon,
If so, update totalPopped, pop both balloons, and remove them from the list
Recalculate the score.
If there are no balloons left, end the game.
 */
public void doAction(double x, double y) {
    for (int i = 0; i < this.balloons.length; i++) {
        if (this.balloons[i].on(x, y) && !this.balloons[i].isPopped()) {
            this.balloons[i].expand();
        }
        for (int j = 1; j <this.balloons.length; j++) {
            if (this.balloons[i].isTouching(this.balloons[j]) && this.balloons[j] != null)
            {
                this.totalPopped +=2;
                this.balloons[i].pop();
                this.balloons[j].pop();
                this.balloons[i] = null;
                this.balloons[j] = null;
            }
        }
    }
    this.calculateScore();
    if (totalPopped == numBalloons) {
        this.endGame();
    }
}

/** Find a balloon that the point (x, y) is on.
 *  Returns null if point is not on any balloon*/
public Balloon findBalloon(double x, double y){
    return null;
}

/** Find and return another balloon that is touching this balloon
 * Returns null if no such Balloon. */
public Balloon findTouching(Balloon balloon){
    return null;
}

/** Calculate the score: sum of the sizes of current ballons, minus
the total of the popped balloons (totalPopped).
Report the score as a message */
public void calculateScore(){
    for (Balloon b: balloons) {
        this.currentScore += b.size();
    }
    if (currentScore >= highScore) {
        this.highScore = this.currentScore;
    }
    UI.printMessage("Score = "+this.currentScore+"    High score = "+this.highScore);
}

/** Returns true if all the balloons have been popped,
 *  Returns false if any of the balloons is not popped */
public boolean allPopped(){
    for (Balloon b : this.balloons){
        if (!b.isPopped()){
            return false;
        }
    }
    return true;
}

/** End the current game.
Record the the score as the new high score if it is better 
Print a message
Clear the list of balloons (so the player can't keep playing)
 */
public void endGame(){
    this.highScore = this.currentScore;
    UI.println("High score = " + this.highScore);
    Arrays.fill(balloons, null);
}

// Main
public static void main(String[] arguments){
    BalloonGame ob = new BalloonGame();
}   

}
导入comp102.*;
导入java.util.*;
导入java.awt.Color;
公共类BalloodGame实现UIButtonListener、UIMouseListener{
//田地
私人最终int NUMBALLONS=20;
private int currentScore;//当前游戏的分数
private int highScore=0;//迄今为止所有游戏中的最高分数。
private int totalPopped=0;
气球气球[]=新气球气球[numBalloons];
//建造师
/**设置GUI,开始新游戏。
*/
公共气球游戏(){
UI.setMouseListener(本);
UI.addButton(“新游戏”,这个);
UI.addButton(“锁定分数”,此按钮);
这个.newGame();
}
//响应按钮和鼠标的GUI方法
/**响应按钮按下,开始新游戏并结束当前游戏*/
已执行公共作废按钮(字符串cmd){
if(cmd.equals(“新游戏”){this.newGame();}
如果(cmd.equals(“Lock Score”){this.endGame();}
}
/**用鼠标发布回应游戏的主要动作*/
已执行公用无效鼠标(字符串操作,双x,双y){
if(动作等于(“释放”)){
这是doAction(x,y);
}
}
/**开始游戏:
清除图形窗格
初始化分数信息
在随机位置创建一组新引出序号
*/
公共游戏{
UI.clearGraphics();
此.currentScore=0;
this.totalPopped=0;
for(int i=0;i
还使用balloon类:

import comp102.*;
import java.util.*;
import java.awt.Color;
import java.io.*;


/** Represents a balloon that can grow until it pops.
A Balloon can say whether a particular point is on it, and
whether it is touching another balloon.
It can also return its size.
Once it has popped, no point is on it, and it can't touch another balloon.
Also, its size is reported as a negative value.

*/
public class Balloon{
// Fields
private double radius = 10;
private double centerX, centerY;
private Color color;
private boolean popped = false;


// Constructors
/** Construct a new Balloon object. 
    Parameters are the coordinates of the center of the balloon
    Does NOT draw the balloon yet.
*/
public Balloon(double x, double y){
    this.centerX = x;
    this.centerY = y;
    this.color = Color.getHSBColor((float)Math.random(), 1.0f, 1.0f);
}

public void draw(){
    UI.setColor(color);
    UI.fillOval(centerX-radius, centerY-radius, radius*2, radius*2);
    if (!this.popped){
        UI.setColor(Color.black);
        UI.drawOval(centerX-radius, centerY-radius, radius*2, radius*2);
    }
}

/** Make the balloon larger by a random amount between 4 and 10*/
public void expand(){
    if (! this.popped){
        this.radius = this.radius + (Math.random()*6 + 4);
        this.draw();
    }
}

/** pop the balloon (changes colour to gray, draws, and pauses briefly)*/
public void pop(){
    this.color = Color.lightGray;
    this.popped = true;
    this.draw();
    UI.sleep(20);
}

/** Returns true if the balloon has been popped */
public boolean isPopped(){
    return this.popped;
}

/** Returns true if the point (x,y) is on the balloon, and false otherwise */
public boolean on(double x, double y){
    if (popped) return false;
    double dx = this.centerX - x;
    double dy = this.centerY - y;
    return ((dx*dx + dy*dy) < (this.radius * this.radius));
}

/** Returns true if this Balloon is touching the other balloon, and false otherwise
 *  Returns false if either balloon is popped. */
public boolean isTouching(Balloon other){
    if (this.popped || other.popped) return false;
    double dx = other.centerX - this.centerX;
    double dy = other.centerY - this.centerY;
    double dist = other.radius + this.radius;
    return (Math.hypot(dx,dy) < dist);
}

/** Calculates and returns the area of the balloon
 *  Returns it in "centi-pixels" (ie, number of pixels/100)
 *  to keep them in a reasonable range.
 *  Returns a negative size if it is popped.*/
public int size(){
    int s = (int) ((this.radius * this.radius * Math.PI)/100);
    if (popped) { s = 0 - s; }
    return s;
}



}
导入comp102.*;
导入java.util.*;
导入java.awt.Color;
导入java.io.*;
/**表示一个气球,它可以一直膨胀到爆炸为止。
气球可以说某个特定点是否在其上,以及
它是否正在接触另一个气球。
它还可以返回其大小。
一旦它爆开了,上面就没有点了,它就不能再碰另一个气球了。
此外,其大小报告为负值。
*/
公营气球{
//田地
私人双半径=10;
私人双centerX、centerY;
私人色彩;
private boolean popped=false;
//建设者
/**构造一个新的气球对象。
参数是引出序号中心的坐标
还没有画气球。
*/
公共气球(双x,双y){
这个.centerX=x;
这个y=y;
this.color=color.getHSBColor((float)Math.random(),1.0f,1.0f);
}
公众抽签(){
UI.setColor(颜色);
UI.圆角(中心半径、中心半径、半径*2、半径*2);
如果(!this.popped){
UI.setColor(Color.black);
UI.Draw椭圆形(中心半径、中心半径、半径*2、半径*2);
}
}
/**将气球放大4到10之间的随机数量*/
公共空间扩展(){
如果(!this.popped){
this.radius=this.radius+(Math.random()*6+4);
这个.draw();
}
}
/**弹出气球(将颜色更改为灰色、绘制并短暂暂停)*/
公共空间{
this.color=color.lightGray;
this.popped=true;
这个.draw();
UI.睡眠(20);
}
/**如果气球已弹出,则返回true*/
公共布尔值isPopped(){
把这个还给我;
}
/**如果点(x,y)位于引出序号上,则返回true,否则返回false*/
公共布尔开启(双x,双y){
如果(弹出)返回false;
double dx=this.centerX-x;
double dy=this.centerY-y;
返回((dx*dx+dy*dy)<(this.radius*this.radius));
}
/**如果此引出序号与其他引出序号相接触,则返回true,否则返回false
*如果任一气球弹出,则返回false*/
公共布尔值IsTouch(引出序号其他){
如果(this.popped | | other.popped)返回false;
double dx=other.centerX-this.centerX;
double dy=other.centerY-this.centerY;
双距离=其他半径+此半径;
返回值(数学形差(dx,dy) for (int j = 1; j <this.balloons.length; j++) {
  if (this.balloons[i].isTouching(this.balloons[j]) && this.balloons[j] != null)