2

私は Java で Tetris を作成しており、ゲームを左側に、得点、ボタン、および nextPiece を右側に配置したいと考えています。

ここに画像の説明を入力

ゲーム パネルのスコアは更新されていますが、スコア パネル (右側) のスコアは更新されていません。

ゲーム パネルには、score と level: のグローバル変数がありprivate int level, totalScore;、0 に初期化されています。

そしてこれは私の中でpaint component():

        g.setColor(Color.RED);
        g.drawString("Level: " + level, this.getWidth()/2+110, this.getHeight()/2-200);
        g.drawString("Score: " + totalScore, this.getWidth()/2+110, this.getHeight()/2-170);

次に、ゲーム パネル内に、レベルとスコアを計算する次のコードがあります。

public void changeLevel () {
    int max = (level+1)*100;
    if (totalScore >= max) {
        System.out.println(max + "reached... next level");
        level++;
        totalScore = 0;
        timer();
    }
}

public int tallyScore(int totalLines) {
    int score = 0;
    switch (totalLines) {
        case 1: score = 40 * (level + 1);
                break;
        case 2: score = 100 * (level + 1);
                break;
        case 3: score = 300 * (level + 1);
                break;
        case 4: score = 1200 * (level + 1); 
                break;
        default: break;
    }
    return score;
}

//loop through all rows starting at bottom (12 rows)
public void checkBottomFull() {
    int lines = 0;
    for(int row = totalRows-1; row > 0; row--) {
        while (isFull(row)) {       
            lines++;
            clearRow(row);   
        }
    }
    totalScore += tallyScore(lines);    
    //check if level needs to be changed based on current score...
    changeLevel();
    //reset lines after score has been incremented
    lines=0;
}

また、スコア パネルにスコアを表示したいので、グローバル変数を返す次の 2 つのメソッドをゲーム パネルに用意しました。

public int getScore() {
    return totalScore;
}
public int getLevel() {
    return level;
}

Score PanelpaintComponent()にはboard.getLevel()and board.getScore()( boardclass は Game Panel) があるので、Game Panel のスコアを Score Panel にフィードできます。

    g.setColor(Color.BLACK);
    g.drawString("Level: " + board.getLevel(), this.getWidth()/2, this.getHeight()/2-130);
    g.drawString("Score: " + board.getScore(), this.getWidth()/2, this.getHeight()/2-100);

しかし、写真からわかるように、これらのスコアは更新されていません。

何かご意見は?

ありがとう!

4

2 に答える 2

3

ビジネス ロジックをプレゼンテーションから分離する必要があります。参照してください: Model View Controllerパターン。

于 2013-05-15T13:42:50.790 に答える