【基于Swing+Java的连连看小游戏的设计与实现(效果+源代码+论文 获取~~)

// /-------------------------漏斗

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.drawLine(x1 + i / 2 + 2, y1 + i, x2 - i / 2 - 2, y1 + i);//覆盖上边的倒三角

}

}

if (remainTimes >= 50 && remainTimes <= 55)

overJLabel.setText(55-remainTimes +“s”);

if (remainTimes == 56)

overJLabel.setText(“OVER”);

}

public void setTimes(int x) {

this.remainTimes = x;

}

public int getTimes() {

return remainTimes;

}

public void run() {

while (dijiguan < 20) {

if (remainTimes == 0) {

JOptionPane.showMessageDialog(null, “游戏开始?”);

}

if (remainTimes == 56) {

JOptionPane.showMessageDialog(null, “时间到!游戏结束!”);

}

remainTimes++;

repaint();

try {

if (dijiguan < 6)

Thread.sleep(1500 - dijiguan * 100);

if (dijiguan >= 6 && dijiguan <= 8)

Thread.sleep(1000 - (dijiguan - 5) * 50);

if (dijiguan > 8)

Thread.sleep(850 - (dijiguan - 8) * 20);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

public void paintComponent(Graphics g) {

super.paintComponent(g);

//是父类JPanel里的方法,会把整个面板用背景色重画一遍,起到清屏的作用

g.drawImage(ii.getImage(), 0, 0, this);

//绘制两个文本字符串

g.setColor(Color.white);

g.drawString("得分: " + score, 430, 165);

g.drawString(“第 " + (guanshu + 1) + " 关”, 430, 190);

for (int i = 0; i < 8; i++) {

for (int j = 0; j < 8; j++) {

switch (map[i][j]) {

case 0:

BlockButton[i][j].setVisible(false);

break;

case 1:

BlockButton[i][j].setIcon(aIcon);

break;

case 2:

BlockButton[i][j].setIcon(bIcon);

break;

case 3:

BlockButton[i][j].setIcon(cIcon);

break;

case 4:

BlockButton[i][j].setIcon(dIcon);

break;

case 5:

BlockButton[i][j].setIcon(eIcon);

break;

case 6:

BlockButton[i][j].setIcon(fIcon);

break;

case 7:

BlockButton[i][j].setIcon(gIcon);

break;

case 8:

BlockButton[i][j].setIcon(hIcon);

break;

case 9:

BlockButton[i][j].setIcon(iIcon);

break;

case 10:

BlockButton[i][j].setIcon(jIcon);

break;

case 11:

BlockButton[i][j].setIcon(kIcon);

break;

case 12:

BlockButton[i][j].setIcon(lIcon);

break;

case 13:

BlockButton[i][j].setIcon(mIcon);

break;

case 14:

BlockButton[i][j].setIcon(nIcon);

break;

case 15:

BlockButton[i][j].setIcon(oIcon);

break;

default:

break;

}

}

}

}

//重载

public void chongzai() {

jishushengyu = 0;

for (int i = 0; i < 8; i++) {

for (int j = 0; j < 8; j++) {

if (map[i][j] > 0) {

jishushengyu++;

}

}

}

int[][] map1 = new int[8][8];

this.map = map1;

Random random = new Random();

for (int i = 0; i < jishushengyu / 2; i++) {

kind = random.nextInt(Kinds) + 1;//0~3+1 === 1~4

do {

randomx1 = random.nextInt(8);//0-8随机数

randomy1 = random.nextInt(8);

} while (map[randomy1][randomx1] > 0);

map[randomy1][randomx1] = kind;

do {

randomx = random.nextInt(8);

randomy = random.nextInt(8);

} while (map[randomy][randomx] > 0);

map[randomy][randomx] = kind;

}

repaint();

for (int i = 0; i < 8; i++) {

for (int j = 0; j < 8; j++) {

BlockButton[i][j].setVisible(true);

}

}

}

public void newGame() {

// JOptionPane.showMessageDialog(null,“你按了开始按钮”);

for (int i = 0; i < 8; i++) {

for (int j = 0; j < 8; j++) {

BlockButton[i][j].setEnabled(true);

BlockButton[i][j].setVisible(true);

}

}

int[][] map = new int[8][8];

this.map = map;

newMap();

ld.setTimes(0);

score = 0;

guanshu = 0;

ld.setdijiguan(guanshu);

}

public void guoguan() {

int jishushengyu2 = 0;

for (int i = 0; i < 8; i++) {

for (int j = 0; j < 8; j++) {

if (map[i][j] > 0) {

jishushengyu2++;

}

}

}

if (jishushengyu2 == 0) {

for (int i = 0; i < 8; i++) {

for (int j = 0; j < 8; j++) {

BlockButton[i][j].setEnabled(true);

BlockButton[i][j].setVisible(true);

}

}

int[][] map = new int[8][8];

this.map = map;

newMap();

ld.setTimes(0);

guanshu++;

ld.setdijiguan(guanshu);

reLoad.setEnabled(true);

}

}

public void newMap() {

ArrayList numbers = new ArrayList();//链表

for (int i = 0; i < Kinds; i++) {

numbers.add(i + 1);//加到列表尾部

numbers.add(i + 1);

}//每一次重新布局的时候,能保证一定有前几种难度中的图片类型

Random random = new Random();

int temp = 0;

for (int i = 0; i < 32- Kinds; i++) {

temp = random.nextInt(Kinds) + 1;//0~kinds-1之间的随机数在加1

numbers.add(temp);

numbers.add(temp);

}

Collections.shuffle(numbers);//随机打乱原来的顺序

map = new int[8][8];

temp = 0;

for (int i = 0; i < 8; i++) {

for (int j = 0; j < 8; j++) {

//JOptionPane.showMessageDialog(null, numbers.get(temp));

map[i][j] = numbers.get(temp++).intValue();//get方法返回第i个元素,intvalue 返回int类型

}

}

}

public void itemStateChanged(ItemEvent e) {

// TODO 自动生成的方法存根

if (e.getSource() == difficultChoice) {

String selected = difficultChoice.getSelectedItem();

if (selected == “简单”) {

Kinds = 4;

newGame();

repaint();

} else if (selected == “中等”) {

Kinds = 8;

newGame();

repaint();

} else if (selected == “困难”) {

Kinds = 12;

newGame();

repaint();

} else if (selected == “变态”) {

Kinds = 15;

newGame();

repaint();

}

}

}

public void actionPerformed(ActionEvent e) {

// TODO 自动生成的方法存根

if (ld.getTimes() >56) {

for (int i = 0; i < 8; i++) {

for (int j = 0; j < 8; j++) {

BlockButton[j][i].setEnabled(false);

}

}

}

if (e.getSource() == reLoad) {

chongzai();

reLoad.setEnabled(false);

}

if (e.getSource() == newgameButton) {

newGame();

reLoad.setEnabled(true);

}

for (int i = 0; i < 8; i++) {

for (int j = 0; j < 8; j++) {

if (e.getSource() == BlockButton[j][i]) {

clicktimes++; // 点击的次数

lineStart.move(i, j);

if (clicktimes % 2 == 1) {

coordinatex1 = i;

coordinatey1 = j;

BlockButton[coordinatey1][coordinatex1].setEnabled(false);

BlockButton[coordinatey][coordinatex].setEnabled(true);

// BlockButton[j][i].setEnabled(false);

}

if (clicktimes % 2 == 0) {

coordinatex = i;

coordinatey = j;

BlockButton[coordinatey][coordinatex].setEnabled(false);

BlockButton[coordinatey1][coordinatex1].setEnabled(true);

}

}

}

}

this.requestFocus();

clearBlock();

/*

  • for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) {

  • BlockButton[j][i].setEnabled(true); }

  • }

*/

repaint();

}

// 判断在一列之内两图片之间是否全部是空白或直接相邻

private boolean containsAllOrNoneZeroInColumn(int posX1, int posY1,

int posX2, int posY2) {

// 直接相连,因而不包含空白

if (Math.abs(posY1 - posY2) == 0) {

return true;

}

int a = posY1 < posY2 ? posY1 : posY2;

int b = posY1 < posY2 ? posY2 : posY1;//y值:a小 b大

for (int j = a + 1; j < b; j++) {

if (map[posX1][j] != 0) {

return false;

}

}

return true;

}

// 判断在一行之内两图片之间是否全部是空白或直接相邻

private boolean containsAllOrNoneZeroInRow(int posX1, int posY1,

int posX2, int posY2) {

// 直接相连,因而不包含空白

if (Math.abs(posX1 - posX2) == 0) {

return true;

}

int a = posX1 < posX2 ? posX1 : posX2;

int b = posX1 < posX2 ? posX2 : posX1;

for (int i = a + 1; i < b; i++) {

if (map[i][posY1] != 0) {

return false;

}

}

return true;

}

// 是否可以一直线相连

private boolean isLinkByOneLine(int posX1, int posY1, int posX2,

int posY2) {

if (posX1 != posX2 && posY1 != posY2) {

return false;

}

if (posX1 == posX2) {

if (containsAllOrNoneZeroInColumn(posX1, posY1, posX2, posY2)) {

return true;

}

}

if (posY1 == posY2) {

if (containsAllOrNoneZeroInRow(posX1, posY1, posX2, posY2)) {

return true;

}

}

return false;

}

// 是否可以两直线相连

private boolean isLinkByTwoLines(int posX1, int posY1, int posX2,

int posY2) {

if (posX1 != posX2 && posY1 != posY2) {

// x1,y1 to x2,y1 to x2,y2

if (containsAllOrNoneZeroInRow(posX1, posY1, posX2, posY1)

&& map[posX2][posY1] == 0

&& containsAllOrNoneZeroInColumn(posX2, posY1, posX2,

posY2)) {

return true;

}

// x1,y1 to x1,y2 to x2,y2

if (containsAllOrNoneZeroInColumn(posX1, posY1, posX1, posY2)

&& map[posX1][posY2] == 0

&& containsAllOrNoneZeroInRow(posX1, posY2, posX2,

posY2)) {

return true;

}

}

return false;

}

// 是否可以三直线相连

private boolean isLinkByThreeLines(int posX1, int posY1, int posX2,

int posY2) {

if (isOnSameEdge(posX1, posY1, posX2, posY2)) {

return true;

}

if (isOnThreeLinesLikeArc(posX1, posY1, posX2, posY2)) {

return true;

}

if (isOnThreeLinesLikeZigzag(posX1, posY1, posX2, posY2)) {

return true;

}

return false;

}

// 是否可以三直线相连,似U形

private boolean isOnThreeLinesLikeArc(int posX1, int posY1, int posX2,

int posY2) {

if (isOnUpArc(posX1, posY1, posX2, posY2)) {

return true;

}

if (isOnDownArc(posX1, posY1, posX2, posY2)) {

return true;

}

if (isOnLeftArc(posX1, posY1, posX2, posY2)) {

return true;

}

if (isOnRightArc(posX1, posY1, posX2, posY2)) {

return true;

}

return false;

}

private boolean isOnUpArc(int posX1, int posY1, int posX2, int posY2) {

// Y --> 0

int lessY = posY1 < posY2 ? posY1 : posY2; //找小y

for (int j = lessY - 1; j >= 0; j–) {

if (containsAllOrNoneZeroInRow(posX1, j, posX2, j)

&& containsAllOrNoneZeroInColumn(posX1, posY1, posX1, j)

&& containsAllOrNoneZeroInColumn(posX2, posY2, posX2, j)

&& map[posX1][j] == 0 && map[posX2][j] == 0) {

return true;

}

}

if (isOnSameEdge(posX1, 0, posX2, 0)

&& containsAllOrNoneZeroInColumn(posX1, posY1, posX1, 0)

&& containsAllOrNoneZeroInColumn(posX2, posY2, posX2, 0)

&& (map[posX1][0] == 0 && map[posX2][0] == 0

|| map[posX1][0] == 0

&& map[posX2][0] == map[posX2][posY2] || map[posX1][0] == map[posX1][posY1]

&& map[posX2][0] == 0)) {

return true;

}

return false;

}

private boolean isOnDownArc(int posX1, int posY1, int posX2, int posY2) {

int moreY = posY1 < posY2 ? posY2 : posY1;

for (int j = moreY + 1; j <= 8 - 1; j++) {

if (containsAllOrNoneZeroInRow(posX1, j, posX2, j)

&& containsAllOrNoneZeroInColumn(posX1, posY1, posX1, j)

&& containsAllOrNoneZeroInColumn(posX2, posY2, posX2, j)

&& map[posX1][j] == 0 && map[posX2][j] == 0) {

return true;

}

}

if (isOnSameEdge(posX1, 8 - 1, posX2, 8 - 1)

&& containsAllOrNoneZeroInColumn(posX1, posY1, posX1, 8 - 1)

&& containsAllOrNoneZeroInColumn(posX2, posY2, posX2, 8 - 1)

&& (map[posX1][8 - 1] == 0 && map[posX2][8 - 1] == 0

|| map[posX1][8 - 1] == map[posX1][posY1]

&& map[posX2][8 - 1] == 0 || map[posX1][8 - 1] == 0

&& map[posX2][8 - 1] == map[posX2][posY2])) {

return true;

}

return false;

}

// ﹚

private boolean isOnLeftArc(int posX1, int posY1, int posX2, int posY2) {

int lessX = posX1 < posX2 ? posX1 : posX2;

for (int i = lessX - 1; i >= 0; i–) {

if (containsAllOrNoneZeroInColumn(i, posY1, i, posY2)

&& containsAllOrNoneZeroInRow(i, posY1, posX1, posY1)

&& containsAllOrNoneZeroInRow(i, posY2, posX2, posY2)

&& map[i][posY1] == 0 && map[i][posY2] == 0) {

return true;

}

}

if (isOnSameEdge(0, posY1, 0, posY2)

&& containsAllOrNoneZeroInRow(0, posY1, posX1, posY1)

&& containsAllOrNoneZeroInRow(0, posY2, posX2, posY2)

&& (map[0][posY1] == 0 && map[0][posY2] == 0

|| map[0][posY1] == map[posX1][posY1]

&& map[0][posY2] == 0 || map[0][posY1] == 0

&& map[0][posY2] == map[posX2][posY2])) {

return true;

}

return false;

}

// (

private boolean isOnRightArc(int posX1, int posY1, int posX2, int posY2) {

int moreX = posX1 < posX2 ? posX2 : posX1;

for (int i = moreX + 1; i <= 8 - 1; i++) {

if (containsAllOrNoneZeroInColumn(i, posY1, i, posY2)

&& containsAllOrNoneZeroInRow(i, posY1, posX1, posY1)

&& containsAllOrNoneZeroInRow(i, posY2, posX2, posY2)

&& map[i][posY1] == 0 && map[i][posY2] == 0) {

return true;

}

}

if (isOnSameEdge(8 - 1, posY1, 8 - 1, posY2)

&& containsAllOrNoneZeroInRow(posX1, posY1, 8 - 1, posY1)

&& containsAllOrNoneZeroInRow(posX2, posY2, 8 - 1, posY2)

&& (map[8 - 1][posY1] == 0 && map[8 - 1][posY2] == 0

|| map[8 - 1][posY1] == map[posX1][posY1]

&& map[8 - 1][posY2] == 0 || map[8 - 1][posY1] == 0

&& map[8 - 1][posY2] == map[posX2][posY2])) {

return true;

}

return false;

}

// 是否可以三直线相连,似之字形N

private boolean isOnThreeLinesLikeZigzag(int posX1, int posY1,
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

一线互联网大厂Java核心面试题库

image

正逢面试跳槽季,给大家整理了大厂问到的一些面试真题,由于文章长度限制,只给大家展示了部分题目,更多Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等已整理上传,感兴趣的朋友可以看看支持一波!
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
// 是否可以三直线相连,似之字形N

private boolean isOnThreeLinesLikeZigzag(int posX1, int posY1,
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。[外链图片转存中…(img-vowEsYHC-1713545949888)]

[外链图片转存中…(img-0t2wonm3-1713545949890)]

[外链图片转存中…(img-uK9TILD6-1713545949890)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

一线互联网大厂Java核心面试题库

[外链图片转存中…(img-B51eEG83-1713545949891)]

正逢面试跳槽季,给大家整理了大厂问到的一些面试真题,由于文章长度限制,只给大家展示了部分题目,更多Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等已整理上传,感兴趣的朋友可以看看支持一波!
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

java连连看代码 import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import javax.swing.*; public class Game extends JFrame{ private int num[][]; //实现数组 private JButton gamebutton[][]; //游戏Button组 private Container cont; //内容面板 private JLabel timelabel; //时间标签 private JButton play; //开始游戏按钮 private JButton exit; //退出按钮 private JButton replay; //重新开始按钮 private JButton back; //返回主界面按钮 private JComboBox choice;//游戏等级 private JButton chongle; //游戏按钮重列 private JPanel gamepanel; //游戏按钮面板 private JPanel panel; //开始游戏面板 private int time=0; //时间记录 private Time T; //记时线程 private Thread t=new Thread(T); //记时线程 private int shu=1; //记录获取游戏按钮事件的个数 private int x=0,y=0,a=0,b=0;//按钮的坐标 private JButton button1=null,button2=null;//点击了的按钮 private boolean can;//能否消除 private int lvx,lvy; //游戏等级数组标列数 private int framex,framey; //框架大小 private int again=1;//记录重列次数 public Game(){ itincomponent(); } private void itincomponent(){ //设置面板属性 if(cont!=null) cont.removeAll(); this.setSize(500, 500); this.setLocationRelativeTo(null); //获取内容面板 cont=this.getContentPane(); //开始和退出按钮 panel=new JPanel(); play=new JButton("开始游戏"); choice=new JComboBox(); choice.addItem("低级"); choice.addItem("中级"); choice.addItem("高级"); choice.setSelectedItem("低级"); exit=new JButton("退出游戏"); panel.add(choice); panel.add(play); panel.add(exit); cont.add(panel,"South"); exit.addActionListener(new Exit());//结束事件监听 play.addActionListener(new Play());//开始事件监听 this.setResizable(false); this.setVisible(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); } ////获得新的面板 private void p(){ //设置面板属性 this.setSize(framex, framey); this.setLocationRelativeTo(null); //获取内容面板 cont=this.getContentPane(); //开始和退出按钮 panel=new JPanel(); exit=new JButton("退出游戏"); replay=new JButton("重新开始"); back=new JButton("返回主界面"); chongle=new JButton("重列"); panel.add(chongle); panel.add(replay); panel.add(exit); panel.add(back); cont.add(panel,"South"); exit.addActionListener(new Exit());//结束事件监听 replay.addActionListener(new Playbutton());//重新开始事件监听 back.addActionListener(new Back());//返回主界面事件监听 chongle.addActionListener(new Chongle());//重列事件监听 //时间面板 timelabel=new JLabel("游戏时间:"+time+"s",JLabel.CENTER); cont.add(timelabel,"North"); //生成游戏操作面板 gamepanel=new JPanel(); gamepanel.setLayout(new GridLayout(lvx,lvy,3,3)); //设置为表格布局 for (int i = 0; i < lvx; i++) { for (int j = 0; j <lvy; j++) { if(gamebutton[i][j].isVisible()){ gamebutton[i][j].setIcon(new ImageIcon(num[i][j]+".jpg")); } gamepanel.add(gamebutton[i][j]); gamebutton[i][j].addActionListener(new Gamebutton()); } } cont.add(gamepanel, "Center"); this.setResizable(false); this.setVisible(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值