Java Applet游戏 - 获得多个对象的游戏

时间:2016-01-09 18:38:34

标签: java applet

我正在创建一个游戏,我陷入了这个级别,我希望得到多个对象的布尔游戏。请查看代码以便更好地理解。

public class Platform {

private int x, y , radius;
private int dx , dy;
private boolean game_over = false;                          ///HERE

public Platform(int x, int y) {
    this.x = x;
    this.y = y;
    radius = 5;
    dx = -1;
    dy = 1;
}

private void checkForCollision(Ball b){
    int ballX = b.getX();
    int ballY = b.getY();
    int ballRadius = b.getRadius();

    int a = ballX - x;
    int bb = ballY - y;
    int c = ballRadius + radius;

    double collide = Math.sqrt((double) a*a + (double) bb*bb);
    if ( c > collide){
        game_over = true;                               ///HERE
        Random r = new Random();
        y = 0 - 50 - r.nextInt(300);
    }
}

public boolean getgameOver() {                          ///HERE
    // TODO Auto-generated method stub
    return game_over;
}

这是平台类,现在在主类,我想让游戏结束,但我不知道如何,看:

public class MainClass extends Applet implements Runnable, KeyListener{

URL url;
Image city, i;
Graphics doubleG;
int cityX = 0;
Ball b;
Platform p[] = new Platform[20];            ///I DON'T KNOW HOW TO GET                                                
private int levelcheck = 0;                 ///GAMEOVER FROM AN ARRAY
private int score;
boolean GameOver = false;                 ///HERE

@Override
public void init() {
    setSize(800,400);
    addKeyListener(this);
}

@Override
public void start() {
    score = 0;
    b = new Ball();
    for (int i = 0; i< p.length; i++){
        Random r = new Random();
        p[i] = new Platform(this.getWidth() - r.nextInt(800), 0 - 100*i);
    }
    Thread thread = new Thread(this);
    thread.start();
}


@Override
public void run() {
    while (true){
        GameOver = p[0].getgameOver();      //I CAN GET GAME OVER OF ONLY  
        if (!GameOver){              //THE FIRST BALL BUT NOT THE OTHER 19      
            score++;
        }
....

提前谢谢你们

2 个答案:

答案 0 :(得分:1)

在while循环中,只需使用for循环并遍历p Platform数组中的所有Platform对象。如果任何一个游戏结束为true,则退出for循环并退出while true循环。

答案 1 :(得分:0)

弄清楚虽然它在......颈部疼痛。

public class MainClass extends Applet implements Runnable, KeyListener{

URL url;
Image city, i;
Graphics doubleG;
int cityX = 0;
Ball b;
Platform p[] = new Platform[20];
private int levelcheck = 0;
private int score;
boolean GameOver = false;
boolean GameOver1 = false;
boolean GameOver2 = false;
boolean GameOver3 = false;
boolean GameOver4 = false;
boolean GameOver5 = false;
boolean GameOver6 = false;
boolean GameOver7 = false;
boolean GameOver8 = false;
boolean GameOver9 = false;
boolean GameOver10 = false;
boolean GameOver11 = false;
boolean GameOver12 = false;
boolean GameOver13 = false;
boolean GameOver14 = false;
boolean GameOver15 = false;
boolean GameOver16 = false;
boolean GameOver17 = false;
boolean GameOver18 = false;
boolean GameOver19 = false;

@Override
public void init() {
    setSize(800,400);
    addKeyListener(this);
    try {
        url = getDocumentBase();
    }catch (Exception e){
    }
    city = getImage(url, "Images/Buildings.jpg");
}

@Override
public void start() {
    score = 0;
    b = new Ball();
    for (int i = 0; i< p.length; i++){
        Random r = new Random();
        p[i] = new Platform(this.getWidth() - r.nextInt(800), 0 - 100*i);
    }
    Thread thread = new Thread(this);
    thread.start();
}


@Override
public void run() {
    while (true){
        GameOver = p[0].getgameOver();
        GameOver1 = p[1].getgameOver();
        GameOver2 = p[2].getgameOver();
        GameOver3 = p[3].getgameOver();
        GameOver4 = p[4].getgameOver();
        GameOver5 = p[5].getgameOver();
        GameOver6 = p[6].getgameOver();
        GameOver7 = p[7].getgameOver();
        GameOver8 = p[8].getgameOver();
        GameOver9 = p[9].getgameOver();
        GameOver10 = p[10].getgameOver();
        GameOver11 = p[11].getgameOver();
        GameOver12 = p[12].getgameOver();
        GameOver13 = p[13].getgameOver();
        GameOver14 = p[14].getgameOver();
        GameOver15 = p[15].getgameOver();
        GameOver16 = p[16].getgameOver();
        GameOver17 = p[17].getgameOver();
        GameOver18 = p[18].getgameOver();
        GameOver19 = p[19].getgameOver();
        if (!GameOver && !GameOver1 && !GameOver2 && !GameOver3 && !GameOver4 && !GameOver5 && !GameOver6 && !GameOver7 && !GameOver8 &&
                !GameOver9 && !GameOver10 && !GameOver11 && !GameOver12 && !GameOver13 && !GameOver14 && !GameOver15 && !GameOver16 && !GameOver17 &&
                !GameOver18 && !GameOver19){
            score++;
        }

是的,这是冗长的:(