2

我正在用 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);

然而,正如您从图片中看到的那样,这些分数并没有更新。

有什么想法吗?

谢谢!

4

2 回答 2

4

您将需要分离关注点,以便可以共享它们。考虑为你的 GUI 基础的逻辑和数据创建一个类,并说你称这个类为模型类。然后你可以给它一个 level 和一个 score 字段并使它们成为“绑定属性”,这意味着其他类可以监听这些字段的变化。我通常通过给我的模型一个 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 所知道的只是模型是否更改。Panel2 所知道的是它正在改变模型的状态。模型所知道的是它的状态可以改变,它的值可以被监听。

你是对的,在这个简单的例子中这有点过头了,但是一旦你开始拥有复杂的数据和状态,这就是有意义的并且变得非常有用。

于 2013-05-15T14:09:26.243 回答
3

您应该将业务逻辑与演示文稿分开。请阅读:模型视图控制器模式。

于 2013-05-15T13:42:50.790 回答