我正在用 Java 制作俄罗斯方块,并希望在左边玩游戏,在右边玩得分、按钮和 nextPiece,如下所示:
您会注意到游戏面板上的分数正在更新,但分数面板(右侧)上的分数并未更新。
在游戏面板上,我有分数和级别的全局变量: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;
}
因为我希望分数面板显示分数,所以我在游戏面板中有这两个方法返回全局变量:
public int getScore() {
return totalScore;
}
public int getLevel() {
return level;
}
在我的分数面板中,paintComponent()
我有board.getLevel()
和board.getScore()
(board
类是游戏面板),所以我可以将游戏面板分数提供给分数面板。
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);
然而,正如您从图片中看到的那样,这些分数并没有更新。
有什么想法吗?
谢谢!