java的连连看游戏设计和实现,基本功能包括:消除模块,重新开始模块,刷新模块,选择难度模块,计时模块。代码如下:
import java.awt.Choice;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class LianLianKan extends JFrame {
private static final long serialVersionUID = 1L;
public LianLianKan() {
LianLianKanJPanel llk = new LianLianKanJPanel();
add(llk);
}
class LianLianKanJPanel extends JPanel implements ActionListener,ItemListener {
private static final long serialVersionUID = 1L;//序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性。
private int[][] map = new int[8][8];//8*8的正方形
private int kind, randomx, randomy, randomx1, randomy1; // 种类,随机x
private int coordinatex, coordinatey, coordinatex1, coordinatey1; // 坐标X
private Point lineStart = new Point(0, 0);
private int clicktimes;
private int jishushengyu;//计数剩余
private int Kinds = 4;
private int score;
private int guanshu;//关数
loudou ld = new loudou();// 漏斗
JButton BlockButton[][] = new JButton[8][8];//
Choice difficultChoice = new Choice();
JButton newgameButton = new JButton("重新开始");
JButton reLoad = new JButton("刷新");
ImageIcon ii = new ImageIcon("src/im/bk.jpg");
ImageIcon aIcon = new ImageIcon("src/im/1.gif");
ImageIcon bIcon = new ImageIcon("src/im/2.gif");
ImageIcon cIcon = new ImageIcon("src/im/3.gif");
ImageIcon dIcon = new ImageIcon("src/im/4.gif");
ImageIcon eIcon = new ImageIcon("src/im/5.gif");
ImageIcon fIcon = new ImageIcon("src/im/6.gif");
ImageIcon gIcon = new ImageIcon("src/im/7.gif");
ImageIcon hIcon = new ImageIcon("src/im/8.gif");
ImageIcon iIcon = new ImageIcon("src/im/9.gif");
ImageIcon jIcon = new ImageIcon("src/im/10.gif");
ImageIcon kIcon = new ImageIcon("src/im/11.gif");
ImageIcon lIcon = new ImageIcon("src/im/12.gif");
ImageIcon mIcon = new ImageIcon("src/im/13.gif");
ImageIcon nIcon = new ImageIcon("src/im/14.gif");
ImageIcon oIcon = new ImageIcon("src/im/15.gif");
public LianLianKanJPanel() {
this.setLayout(null);
newMap();
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
BlockButton[i][j] = new JButton();
add(BlockButton[i][j]);
BlockButton[i][j].addActionListener(this);//监听器
BlockButton[i][j].setBounds(30 + j * 40, 30 + i * 40, 31,34);
// BlockButton[i][j].setBorderPainted(false);
// BlockButton[i][j].setVisible(true);
}
}
difficultChoice.add("简单");
difficultChoice.add("中等");
difficultChoice.add("困难");
difficultChoice.add("变态");
newgameButton.setBounds(map[0].length * 40 + 80, 40, 100, 20);
newgameButton.setBackground(Color.white);
newgameButton.setBorderPainted(false); //去边框
reLoad.setBounds(map[0].length * 40 + 100, 80, 60, 20);
reLoad.setBackground(Color.white);
reLoad.setBorderPainted(false);
difficultChoice.setBounds(map[0].length * 40 + 100, 120, 60, 20);
difficultChoice.addItemListener(this);
newgameButton.addActionListener(this);
reLoad.addActionListener(this);
this.add(newgameButton);
this.add(reLoad);
this.add(difficultChoice);
// /-------------------------漏斗
ld.setBounds(map[0].length * 40 + 100, 200, 70, 150);// 漏斗
ld.setBackground(Color.black);
this.add(ld);
}
class loudou extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
private int dijiguan;
int remainTimes = 0; // 时间
int x1 = 0;
int y1 = 30;
int x2 = 60;
int y2 = 150;
Thread nThread1;//线程
JLabel overJLabel = new JLabel();
JDialog dialog = new JDialog();
public loudou() {
nThread1 = new Thread(this);
nThread1.start();
this.setLayout(null);
this.add(overJLabel);
overJLabel.setBounds(0, 0, 200, 50);
overJLabel.setForeground(Color.white);
}
public void setdijiguan(int x) {
this.dijiguan = x;
}
public void paintComponent(Graphics g) // 画画函数
{
super.paintComponent(g);
g.setColor(Color.green);
for (int i = 0; i < 56; i++) {
g.drawLine(x1 + i / 2 + 2, y1 + i, x2 - i / 2 - 2, y1 + i);
}
if (remainTimes < 55) {
for (int i = 0; i < remainTimes; i++) {
g.drawLine(x1 + i / 2 + 2, y2 - i - 1, x2 - i / 2 - 2, y2 - i
- 1);
}
g.drawLine((x1 + x2) / 2, (y1 + y2) / 2, (x1 + x2) / 2, y2 - 2);
g.drawLine((x1 + x2) / 2 + 1, (y1 + y2) / 2 + 1, (x1 + x2) / 2 + 1,y2 - 2);//两条竖线
g.setColor(getBackground());
for (int i = 0; i < remainTimes; i++) {
g.drawLi