java实现简易连连看小游戏(java 连连看)

网友投稿 311 2022-08-07


java实现简易连连看小游戏(java 连连看)

本文实例为大家分享了java实现简易连连看小游戏的具体代码,供大家参考,具体内容如下

新手上路,分享一下

直接上代码

package linkgame;

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.File;

import java.util.HashSet;

import java.util.Set;

import java.util.TreeSet;

public class LinkGame extends JFrame implements Runnable{

private JPanel panel_01,panel_02;

private String first,finnal;

private JLabel time;

private boolean isClick=true;

private int x1,y1,x2,y2;

Icon temp;

JButton firstbutton=new JButton();

JButton secondbutton=new JButton();

public LinkGame(){

setTitle("连连看");

setBounds(300,100,600,450);

panel_01=new JPanel(new GridLayout(6,6));

panel_02=new JPanel(new BorderLayout());

close();

rightPanel();

leftPanel();

add(panel_01,BorderLayout.CENTER);

add(panel_02,BorderLayout.WEST);

setVisible(true);

setResizable(false);

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

//run();

}

//关闭应用询问

private void close(){

addWindowListener(new WindowAdapter() {

@Override

public void windowClosing(WindowEvent e) {

int result = JOptionPane.showConfirmDialog(panel_01, "是否确认退出?", "确xwuyhg认",

JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

if(result == JOptionPane.OK_OPTION){

System.exit(0);

}

}

});

}

//左侧面板

private void rightPanel(){

File img=new File("src/images");//打开图片文件夹

String[] list=img.list();//获取文件中的图片名称并存入集合

Set s=new HashSet<>();//构建无序集合

Set s2=new TreeSet<>();

boolean bol=false;

//把18个图片存入set集合中以备生成界面

for(int l=0;l<18;l++){

s.add(list[l]);

s2.add(list[l]);

}

for(int i=0,count=0;i<6;i++){

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

String[] strs01=s.toArray(new String[0]);

String[] strs02=s2.toArray(new String[0]);

if(count>17){

count=0;

bol=true;

}

JButton button=new JButton();//新建按钮

if(bol){

ImageIconhttp:// imgs=new ImageIcon("src/images/"+strs02[count]);

button.setIcon(imgs);

}else{

ImageIcon imgs=new ImageIcon("src/images/"+strs01[count]);

button.setIcon(imgs);

}

button.addActionListener(new click());//添加按钮的监听事件

panel_01.add(button);//把按钮添加到左面板

count++;

}

}

}

//有面板

private void leftPanel(){

ImageIcon icon=new ImageIcon("src/images/leftback.png");//连连看字

JLabel label=new JLabel(icon);

time=new JLabel("剩余时间30秒");//倒计时计时初始化

time.setFont(new Font("楷体",Font.PLAIN,20));//设置字体样式

panel_02.add(label,BorderLayout.NORTH);

panel_02.add(time,BorderLayout.SOUTH);

}

//倒计时方法

public void run() {

int count=30;

while (count>=0) {

try {

time.setText("剩余时间"+count + "秒");

Thread.sleep(1000); //暂停1秒

count--;

} catch (InterruptedException e) {

e.printStackTrace();

}

if(count==0){

JOptionPane.showMessageDialog(panel_01,"时间用完,已结束");

System.exit(0);

}

}

}

//按钮点击内部类

class click implements ActionListener{

ImageIcon img=new ImageIcon("src/images/Img319981730_null.jpg");//空白图片

@Override

public void actionPerformed(ActionEvent e) {

//获取点击按钮的行和列

if(isClick){

firstbutton=((JButton)e.getSource());

first=firstbutton.getIcon().toString();//获取点击图片名称

temp=firstbutton.getIcon();

x1=firstbutton.getLocation().x;//获取点击图片横纵坐标

y1=firstbutton.getLocation().y;

firstbutton.setIcon(img);//点击后图片设置为空白图片

isClick=false;

}else {

secondbutton=((JButton)e.getSource());

finnal=secondbutton.getIcon().toString();//获取第二次点击图片名称

x2=secondbutton.getLocation().x;//获取点击图片横纵坐标

y2=secondbutton.getLocation().y;

isClick=true;

win();//判断是否消除或者胜利方法

}

}

private void win(){

boolean bol=first.equals(finnal);//判断两次点击的图片名字是否相同

boolean bol2=!((x1==x2)&&(y1==y2));//判断位置是否不一样,防止点击两张相同的图片

if(bol&&bol2){

firstbutton.setIcon(xwuyhgimg);

secondbutton.setIcon(img);

String str02="src/images/Img319981730_null.jpg";//把第二张图片改成空白图片

Component[] bts=panel_01.getComponents();//获取全部组件

int count=0;

for(int i=0;i

JButton btn=(JButton)bts[i];

String str01=btn.getIcon().toString();

if(str01.equals(str02)){//判断组件名称等于空白图片名称的个数

count++;

if(count==35){

//如果到达全部图片则游戏胜利

JOptionPane.showMessageDialog(panel_01,"你赢了");

System.exit(0);

}

}

}

}else{

firstbutton.setIcon(temp);//设置第一张图片还原

}

}

}

}

然后创建主类运行即可

package linkgame;

public class Play {

public static void main(String[] args) {

LinkGame linkGame=new LinkGame();

}

}

JButton btn=(JButton)bts[i];

String str01=btn.getIcon().toString();

if(str01.equals(str02)){//判断组件名称等于空白图片名称的个数

count++;

if(count==35){

//如果到达全部图片则游戏胜利

JOptionPane.showMessageDialog(panel_01,"你赢了");

System.exit(0);

}

}

}

}else{

firstbutton.setIcon(temp);//设置第一张图片还原

}

}

}

}

然后创建主类运行即可

package linkgame;

public class Play {

public static void main(String[] args) {

LinkGame linkGame=new LinkGame();

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:详解MyBatisPlus如何实现分页和查询操作(mybatisplus实现oracle分页)
下一篇:java实现连连看游戏课程设计(java连连看课程设计报告)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~