如何用JAVA 编写一个连连看游戏全程设计刚试了 。。测试通过 。。
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
publicclass LianLianKan implements ActionListener {
JFrame mainFrame; // 主面板
Container thisContainer;
JPanel centerPanel, southPanel, northPanel; //子面板
JButton diamondsButton[][] = newJButton[6][5];// 游戏按钮数组
JButton exitButton, resetButton, newlyButton;// 退出 , 重列,重新开始按钮
JLabel fractionLable = newJLabel("0"); // 分数标签
JButton firstButton, secondButton; // 分别记录两次被选中的按钮
// 储存游戏按钮位置(这里其实只要6行,5列 。但是我们用了8行,7列 。是等于在这个面板按钮的周围还围
//了一层是0的按钮,这样就可以实现靠近面板边缘的两个按钮可以消去)
int grid[][] = new int[8][7];
static boolean pressInformation = false; // 判断是否有按钮被选中
int x0 = 0, y0 = 0, x = 0, y = 0, fristMsg =0, secondMsg = 0, validateLV; // 游戏按钮的位置坐标
int i, j, k, n;// 消除方法控制
public void init() {
mainFrame = new JFrame("JKJ连连看");
thisContainer = mainFrame.getContentPane();
thisContainer.setLayout(new BorderLayout());
centerPanel = new JPanel();
southPanel = new JPanel();
northPanel = new JPanel();
thisContainer.add(centerPanel,"Center");
thisContainer.add(southPanel,"South");
thisContainer.add(northPanel,"North");
centerPanel.setLayout(new GridLayout(6, 5));
for (int cols = 0; cols6; cols++) {
for (int rows = 0; rows5; rows++) {
diamondsButton[cols][rows] = newJButton(String
.valueOf(grid[cols + 1][rows + 1]));
diamondsButton[cols][rows].addActionListener(this);
centerPanel.add(diamondsButton[cols][rows]);
}
}
exitButton = new JButton("退出");
exitButton.addActionListener(this);
resetButton = new JButton("重列");
resetButton.addActionListener(this);
newlyButton = new JButton("再来一局");
newlyButton.addActionListener(this);
southPanel.add(exitButton);
southPanel.add(resetButton);
southPanel.add(newlyButton);
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable
.getText())));
northPanel.add(fractionLable);
mainFrame.setBounds(280, 100, 500, 450);
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void randomBuild() {
int randoms, cols, rows;
for (int twins = 1; twins = 15; twins++){//一共15对button,30个
randoms = (int) (Math.random() * 25 +1);//button上的数字
for (int alike = 1; alike = 2; alike++){
cols = (int) (Math.random() * 6 + 1);
rows = (int) (Math.random() * 5 + 1);
while (grid[cols][rows] != 0) {//等于0说明这个空格有了button
cols = (int) (Math.random() * 6 + 1);
rows = (int) (Math.random() * 5 + 1);
}
this.grid[cols][rows] = randoms;
}
}
}
public void fraction() {
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable
.getText()) + 100));
}
public void reload() {
int save[] = new int[30];
int n = 0, cols, rows;
int grid[][] = new int[8][7];
for (int i = 0; i = 6; i++) {
for (int j = 0; j = 5; j++) {
if (this.grid[i][j] != 0) {
save[n] = this.grid[i][j];//记下每个button的数字
n++;//有几个没有消去的button
}
}
}
n = n - 1;
this.grid = grid;
while (n = 0) {//把没有消去的button重新放一次
cols = (int) (Math.random() * 6 + 1);
rows = (int) (Math.random() * 5 + 1);
while (grid[cols][rows] != 0) {
cols = (int) (Math.random() * 6 + 1);
rows = (int) (Math.random() * 5 + 1);
推荐阅读
- 手机里的照片怎么恢复,换手机后原手机里的照片怎么恢复
- 目前新媒体岗位现状如何,目前新媒体岗位现状如何呢
- javascript替换空格,js 空格替换
- 怎么实现微信登录游戏直播,怎么实现微信登录游戏直播功能
- linux导出命令到文件 linux 导出文件
- 怎么用u盘重装电脑系统,小白一键重装系统
- 游戏先是即时打boss回合,游戏先是即时打boss回合怎么办
- 小霸王游戏太空青蛙格斗的简单介绍
- php插入表数据 php如何添加数据库