java连连看小游戏

本文介绍了一个用Java编写的连连看小游戏,游戏规则是在限定时间内消除所有相同图案,锻炼玩家的观察能力和反应速度。文章包含运行界面截图和部分代码示例。
摘要由CSDN通过智能技术生成

目录

运行界面图

代码如下(示例):

2.运行演示


这是一个用java编写的小游戏,连连看是一种消除类益智游戏,核心要求是在规定的时间内,消除游戏界面中选中的两张相同的图案,直至完全消除所有图案。这款游戏操作简单(只需单击鼠标左键操作)、面向人群广泛,在限时操作的游戏要求下,吸引玩家自发地锻炼观察能力、判断能力和反应能力,故从推出至今颇受欢迎。


提示:以下是本篇文章正文内容,下面案例可供参考

运行界面图

代码如下(示例)

package ui;

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 Llk_2 extends JFrame {

	private static final long serialVersionUID = 1L;

	public Llk_2() {
		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("C:\\Users\\zjjt\\Desktop\\sb/bjt2.jpeg");

		ImageIcon aIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/1.jpg");
		ImageIcon bIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/2.jpg");
		ImageIcon cIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/3.gif");
		ImageIcon dIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/4.gif");
		ImageIcon eIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/5.jpg");
		ImageIcon fIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/6.gif");
		ImageIcon gIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/7.jpeg");
		ImageIcon hIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/8.jpeg");
		ImageIcon iIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/9.jpeg");
		ImageIcon jIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/10.jpeg");
		ImageIcon kIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/13.jpg");
		ImageIcon lIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/11.jpeg");
		ImageIcon mIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/12.jpeg");
		ImageIcon nIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/5.jpg");
		ImageIcon oIcon = new ImageIcon("C:\\Users\\zjjt\\Desktop\\sb/6.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 J
  • 7
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
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); }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梓轩wdw

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值