换了个老师,这个老师让人有点小无语
直接给全部代码给你,最基本的实现思路都不说
我只好一个一个函数去找度娘了,之后写了些注释。
相比之前,现在加的功能有:游戏开始时有登录框,要求输入用户名和密码;有进度条,并在消除的同时加时间;重置按钮等
把之前的GameData.java 文件中的各功能分成模块,再进行调用
/**
* 显示
* @author miemie
*
*/
public class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
private static final int height = 400;
private static final int width = 400;
JButton button1 = null;
JButton button2 = null;
GameFile gameFile= new GameFile();
final GameData gameData = new GameData();
JButton[] buttons = new JButton[GameData.rows * GameData.cols];
//分数
JLabel lblScroe = new JLabel();
int score=gameFile.readScore();
//时间
final int timeLimit = 20;
int timeLeft = timeLimit;
public MainFrame() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
//this.resize(width + 17, height + 38);//过时
this.setBounds(1, 1, width + 17, height + 38);
this.setLocation((screenSize.width - width) / 2,
(screenSize.height - height) / 2);
this.setTitle("连连看");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// this.setLayout(null);
initButtons();//
showImage();
initPane();
initTimePane();
}
以下为同个页面中的其他模块
/**
* 面板,显示在窗口上方
*/
private void initPane() {
JPanel scorePane = new JPanel(new FlowLayout());
scorePane.add(new JLabel("玩家:"+UserEmpty.username));
scorePane.add(new JLabel(" "));
scorePane.add(new JLabel("得分:"));
lblScroe.setText("" + score);
JButton btnResort = new JButton("重排");
btnResort.addActionListener(new ActionListener() {
//因为没有实现ActionListener接口,重写方法
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
gameData.shuffle(); //打乱数据
showImage(); //再次显示所有图片(重置data中的数字)
timeLeft=timeLeft-10;
}
});
scorePane.add(lblScroe);
scorePane.add(btnResort);
this.getContentPane().add(scorePane, BorderLayout.NORTH);
}
/**
* 初始化面板上面的按钮。
* @param gameData
*/
private void initButtons() {
JPanel gamePanel = new JPanel(new GridLayout(GameData.rows,
GameData.cols));
for (int i = 0; i < GameData.rows * GameData.cols; ++i) {
buttons[i] = new JButton();
buttons[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
// 获取点击的按钮是哪个
JButton button = (JButton) e.getSource();
if (button1 == null) { // 以前没点过
button1 = button;
} else { // 已经点过
button2 = button;
int index1 = button1.getY() / button1.getHeight()
* GameData.cols + button1.getX()
/ button1.getWidth();
int index2 = button2.getY() / button2.getHeight()
* GameData.cols + button2.getX()
/ button2.getWidth();
if (GameRule.isConnect(gameData.data, index1, index2)) {
button1.setVisible(false);
button2.setVisible(false);
gameData.data[index1] = 0;
gameData.data[index2] = 0;
//---加分处理,每消除一对就加10分
score += 10;
lblScroe.setText("" + score);
timeLeft += 5;
if (timeLeft > timeLimit) {
timeLeft = timeLimit;
}
}
button1 = null;
button2 = null;
}
}
});
gamePanel.add(buttons[i]);
this.getContentPane().add(gamePanel, BorderLayout.CENTER);
}
}
/**
* 显示图片,设置路径并显示
* @param showImage
*/
private void showImage() {
for (int i = 0; i < buttons.length; ++i) {
buttons[i].setVisible(true);
String path = this.getClass().getResource("/image").getPath();
ImageIcon icon = new ImageIcon(path + "/fruit_" + gameData.data[i]
+ ".png");
icon.setImage(icon.getImage().getScaledInstance(
width / GameData.cols, height / GameData.rows,
Image.SCALE_DEFAULT));
buttons[i].setIcon(icon);
if (gameData.data[i] == 0) {
buttons[i].setVisible(false);
}
}
}
/**
* 进度条设置
* @param initTimePane
*/
private void initTimePane() {
JPanel timePane = new JPanel(new BorderLayout());
final JProgressBar timeLine = new JProgressBar();
timeLine.setMaximum(timeLimit);
//将进度条的最大值(存储在进度条的数据模型中)设置为timeLimit。
timeLine.setValue(timeLimit);
//将进度条的当前值(存储在进度条的数据模型中)设置为timeLimit
timePane.add(timeLine);
this.getContentPane().add(timePane, BorderLayout.SOUTH);
final Timer gameTimer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (timeLeft == 0) {
((Timer) arg0.getSource()).stop();
Object[] options = { " 确定 ", " 取消 " };
int response = JOptionPane.showOptionDialog(null,
"时间到了,重新开始游戏吗?", "游戏结束 ", JOptionPane.YES_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options,
options[0]);
if (response == 0) { // 确定
gameFile.saveScore(String.valueOf(score));
setVisible(false);
new MainFrame().setVisible(true);
dispose();
}else if (response == 1) { // 取消
System.exit(0);
}
}
--timeLeft;
timeLine.setValue(timeLeft);
}
});
gameTimer.start();
}
}
在同级目录view层下编写LoginFrame
package linkgame.view;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import linkgame.data.UserEmpty;
public class LoginFrame extends JFrame {
private static final long serialVersionUID = -5939053840817397626L;
private static final int height = 200;
private static final int width = 350;
private JTextField username = new JTextField();
private JPasswordField password = new JPasswordField();
public LoginFrame() {
// TODO 自动生成的构造函数存根
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setBounds(1, 1, width + 17, height + 38);
this.setLocation((screenSize.width - width) / 2,
(screenSize.height - height) / 2);
this.setTitle("登录");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initPane();
}
private void initPane() {
final JPanel fieldPanel = new JPanel();
fieldPanel.setLayout(null);
JLabel a1 = new JLabel("用户名:");
a1.setBounds(80,70,50,20);
JLabel a2 = new JLabel("密 码:");
a2.setBounds(80,90,50,20);
fieldPanel.add(a1);
fieldPanel.add(a2);
username.setBounds(130, 70, 120, 20);
password.setBounds(130, 90, 120, 20);
fieldPanel.add(username);
fieldPanel.add(password);
JButton jbu1= new JButton("确定");
JButton jbu2= new JButton("取消");
jbu1.setBounds(100, 120, 60, 20);
jbu2.setBounds(180, 120, 60, 20);
fieldPanel.add(jbu1);
fieldPanel.add(jbu2);
jbu1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
String name = username.getText();
String pwd = String.valueOf(password.getPassword());
if(!"".equals(name)&&!"".equals(pwd)){
setVisible(false);
UserEmpty.username=name;
MainFrame mainFrame = new MainFrame();
mainFrame.setVisible(true);
dispose();
}else{
JOptionPane.showMessageDialog(null, "用户名和密码不能为空", "出错啦", JOptionPane.ERROR_MESSAGE);
}
}
});
jbu2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
System.exit(0);
}
});
this.getContentPane().add(fieldPanel, BorderLayout.CENTER);
}
}
在data层下编写GameFile,用于读写文件
package linkgame.data;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class GameFile {
// 读取数据
String path = this.getClass().getResource("/").getPath();
File file = new File(path + "score.txt");
/**
* 读取分数
* @return
*/
public int readScore() {
int score = 0;
try {
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileInputStream fis = new FileInputStream(file);
System.out.println(file.length());
if (file.length() > 0) {
byte[] buf = new byte[(int) (file.length())];
fis.read(buf); // 读取文件中的数据存放到字节数组中
String str = new String(buf); // 利用字节数组创建字符串
System.out.println(str); // 打印字符串
score = Integer.parseInt(str);
}
fis.close();
} catch (Exception e) {
System.out.println("文件打开失败。");
e.printStackTrace();
}
return score;
}
/**
* 保存分数
* @param score
*/
public void saveScore(String score) {
try {
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileOutputStream fos = new FileOutputStream(file, false);
// 逐个将字符写入到文件中
for (int i = 0; i < score.length(); i++) {
fos.write(score.charAt(i));
}
fos.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
在data下编写UserEmpty
package linkgame.data;
public class UserEmpty {
private int id;
public static String username;
private String password;
private String status;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
代码中不少组件是我第一次见,一个一个百度查
这次实训虽然还有两天,但感觉不怎么充实,算法方面又几乎没有什么,连深搜都没有
也许是学校可以提供的平台不高,资源实在有限