我正在用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);
但是,从图片中可以看到,这些分数没有更新。
有什么想法吗?
谢谢!
最佳答案
您将需要分开关注点,以便可以共享它们。考虑为基于GUI的逻辑和数据创建一个类,并说您将此类称为Model类。然后,您可以为其指定一个级别和一个得分字段,并使它们成为“绑定属性”,这意味着其他类可以侦听这些字段的更改。我通常通过给我的模型一个SwingPropertyChangeSupport对象并给它一个addPropertyChangeListener(PropertyChangeListener listener)
和removePropertyChangeListener(PropertyChangeListener listener)
来实现,然后我通过调用PropertyChangeSupport的通知通知所有已注册的PropertyChangeListener。例如。,
import java.beans.PropertyChangeListener;
import javax.swing.event.SwingPropertyChangeSupport;
public class Model {
public static final String SCORE = "score";
public static final String LEVEL = "level";
private SwingPropertyChangeSupport pcSupport =
new SwingPropertyChangeSupport(this);
private int score;
private int level;
public void addPropertyChangeListener(PropertyChangeListener listener) {
pcSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
pcSupport.removePropertyChangeListener(listener);
}
public int getScore() {
return score;
}
public void setScore(int score) {
int oldValue = this.score;
int newValue = score;
this.score = score;
pcSupport.firePropertyChange(SCORE, oldValue, newValue);
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
int oldValue = this.level;
int newValue = level;
this.level = level;
pcSupport.firePropertyChange(LEVEL, oldValue, newValue);
}
}
然后,任何希望监听值更改的GUI或视图组件都可以这样做。如果您正在研究MVC结构,则下面的类将“视图”与“控件”结合在一起:
import java.awt.FlowLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class UseModelGui {
private static void createAndShowGui() {
Panel1 panel1 = new Panel1();
Panel2 panel2 = new Panel2();
Model model = new Model();
panel1.setModel(model);
panel2.setModel(model);
JFrame frame = new JFrame("UseModelGui");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(panel1);
frame.getContentPane().add(panel2);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class Panel1 extends JPanel {
private JTextField scoreField = new JTextField(2);
private JTextField levelField = new JTextField(2);
public Panel1() {
scoreField.setFocusable(false);
scoreField.setEditable(false);
levelField.setFocusable(false);
levelField.setEditable(false);
add(new JLabel("score:"));
add(scoreField);
add(new JLabel("Level:"));
add(levelField);
setBorder(BorderFactory.createTitledBorder("Check Values"));
}
public void setModel(Model model) {
model.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent pcEvt) {
if (Model.LEVEL.equals(pcEvt.getPropertyName())) {
String level = pcEvt.getNewValue().toString();
levelField.setText(level);
} else if (Model.SCORE.equals(pcEvt.getPropertyName())) {
String score = pcEvt.getNewValue().toString();
scoreField.setText(score);
}
}
});
}
}
class Panel2 extends JPanel {
private JSpinner scoreSpinner = new JSpinner(new SpinnerNumberModel(0, 0,
20, 1));
private JSpinner levelSpinner = new JSpinner(new SpinnerNumberModel(0, 0,
10, 1));
private Model model;
public Panel2() {
add(new JLabel("score:"));
add(scoreSpinner);
add(new JLabel("Level:"));
add(levelSpinner);
setBorder(BorderFactory.createTitledBorder("Set Values"));
scoreSpinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent evt) {
int score = ((Integer) scoreSpinner.getValue()).intValue();
if (model != null) {
model.setScore(score);
}
}
});
levelSpinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent evt) {
int level = ((Integer) levelSpinner.getValue()).intValue();
if (model != null) {
model.setLevel(level);
}
}
});
}
public void setModel(Model model) {
this.model = model;
}
}
这样做的好处是,Panel1不了解Panel2,而Model不了解其中之一。 Panel1唯一知道的是Model是否更改。 Panel2唯一知道的是它正在更改模型的状态。该模型只知道其状态可以更改,并且其值可以被监听。
没错,在这个简单的示例中,这是过头了,但是,一旦您开始拥有复杂的数据和状态,这就会变得有意义并且很有帮助。