连连看代码,直接编译运行即可
可以自己修改,把汉字改成图片import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class Lianliankan extends JFrame{
public static final int SQUARE_SIDE = 30; //每个格的大小
public static final int JFRAME_WIDTH = SQUARE_SIDE * 10; //窗休宽
public static final int JFRAME_HEIGHT = SQUARE_SIDE * 10; //窗体高
public static final String[] data = new String[]{
"田","畕","畾","土","圭","垚","十","卄","卅","卌",
"屮","艸","芔","茻","水","沝","淼","皕","兟","兢",
"競","夶","棗","棘","玨","竝","臸","玆","臦","林"};//所有显示的数据 "字"
public static int[][] map = new int[][]{
// x 0 1 2 3 4 5 6 7 8 9 y
{0,0,0,0,0,0,0,0,0,0},//0
{0,1,1,1,1,1,1,1,1,0},//1
{0,1,1,1,1,1,1,1,1,0},//2
{0,1,1,1,1,1,1,1,1,0},//3
{0,1,1,1,1,1,1,1,1,0},//4
{0,1,1,1,1,1,1,1,1,0},//5
{0,1,1,1,1,1,1,1,1,0},//6
{0,1,1,1,1,1,1,1,1,0},//7
{0,1,1,1,1,1,1,1,1,0},//8
{0,0,0,0,0,0,0,0,0,0} //9
}; //虚拟地图
public static String[][] fonts = new String.length-2]; //实际抽出来的 "字"
public static List<Point> coordlist = new ArrayList<Point>(4); //存放已经找到的点
public static List<Point> sticklist = new ArrayList<Point>(2); //存放两个比较点
private GameArithmetic gameArithmetic = new GameArithmetic(); //查找点的算
private SquareMap square; //图形类
private Timer tirmer = new Timer(); //定时器
public static int totalTime = 60000*5; //游戏时间
public Lianliankan(){
//启动定时器来监听时间
TimerTask task = new TimerTask(){
@Override
public void run() {
totalTime -= 1000;
square.repaint();
}
};
tirmer.scheduleAtFixedRate(task, 1000, 1000);
//初始化代码
square = new SquareMap();
square.addMouseListener(new MouseListener());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(JFRAME_WIDTH+5, JFRAME_HEIGHT + 60);
setTitle("Lianliankan");
setLocationRelativeTo(null);
setResizable(false);
JMenuBar menu = new JMenuBar();
JMenu game = new JMenu("游戏");
JMenuItem newgame = game.add("新游戏");
JMenuItem pause = game.add("暂停");
JMenuItem goon = game.add("继续");
JMenuItem exit = game.add("退出");
JMenu help = new JMenu("帮助");
JMenuItem about = help.add("关于");
menu.add(game);
menu.add(help);
setJMenuBar(menu);
add(square);
setVisible(true);
init();
}
//初始化数据
public void init(){
boolean[] fontFalgs = new boolean;
int length = ((map.length-2)*(map.length-2))/4;
Random rn = new Random();
for(int i = 0; i < length; i++){
int num = 0;
do{
num = rn.nextInt(data.length);
}while(fontFalgs);
String str = data;
fontFalgs = true;
int colIndex = 0,rowIndex = 0;
for (int j = 0; j < 4; j++) { //随机放到4个不同位置
do{
rowIndex = rn.nextInt(fonts.length);
colIndex = rn.nextInt(fonts.length);
}while(fonts != null);
fonts = str;
}
}
}
//鼠标监听
class MouseListener extends MouseAdapter{
@Override
public void mouseClicked(MouseEvent e) {
if(e.getX() < SQUARE_SIDE || e.getX() > SQUARE_SIDE * 9 ||
e.getY() < SQUARE_SIDE || e.getY()> SQUARE_SIDE * 9 ||
map[(e.getY()/SQUARE_SIDE)][(e.getX()/SQUARE_SIDE)] == 0){
return;
} // 出了实际地图界限和已经找到就反回
//System.out.println("x : " + (e.getX()/SQUARE_SIDE) + " y : " + (e.getY()/SQUARE_SIDE));
if(sticklist.size() > 0){ //比较两个点
Point p1 = new Point(sticklist.get(0));
Point p2 = new Point((e.getX()/SQUARE_SIDE),(e.getY()/SQUARE_SIDE));
map = 2; //比较两个点时把这两个点设成其它标识
map = 2;
//查询两点是否相通如果找到 设成该两点为0
List<Point> ps = gameArithmetic.meet(p1.x < p2.x ? p1 : p2, p2.x > p1.x ? p2 : p1);
if(ps == null){
sticklist.clear();
sticklist.add(new Point((e.getX()/SQUARE_SIDE),(e.getY()/SQUARE_SIDE)));
map = 1;
map = 1;
}else{
coordlist.addAll(ps);
sticklist.add(p2);
square.repaint();
map = 0;
map = 0;
}
}else {
sticklist.add(new Point((e.getX()/SQUARE_SIDE),(e.getY()/SQUARE_SIDE)));
}
square.repaint();
}
}
public static void main(String[] args) {
new Lianliankan();
}
}
//图形类
class SquareMap extends JPanel{
Date startTime = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Color c = g.getColor();
g.setColor(Color.GREEN);
draw(g);
g.setColor(c);
}
//画显示在面板上的图形与时间
private void draw(Graphics g){
Font font = new Font("宋体",Font.PLAIN,25);
g.setFont(font);
for(int i = Lianliankan.SQUARE_SIDE ; i < Lianliankan.JFRAME_HEIGHT-Lianliankan.SQUARE_SIDE ; i+=Lianliankan.SQUARE_SIDE){
for (int j = Lianliankan.SQUARE_SIDE; j < Lianliankan.JFRAME_WIDTH - Lianliankan.SQUARE_SIDE; j+=Lianliankan.SQUARE_SIDE) {
if(Lianliankan.map!=0){
g.draw3DRect(j, i, Lianliankan.SQUARE_SIDE, Lianliankan.SQUARE_SIDE, true);
g.drawString(Lianliankan.fonts[(i/Lianliankan.SQUARE_SIDE)-1][(j/Lianliankan.SQUARE_SIDE)-1], j+2, i+25);
}
}
}
if(Lianliankan.sticklist.size() > 0){
g.setColor(Color.RED);
for (int j2 = 0; j2 < Lianliankan.sticklist.size(); j2++) {
Point p = Lianliankan.sticklist.get(j2);
g.draw3DRect(p.x * (Lianliankan.SQUARE_SIDE),(p.y *Lianliankan.SQUARE_SIDE) , Lianliankan.SQUARE_SIDE, Lianliankan.SQUARE_SIDE, true);
}
if(Lianliankan.sticklist.size() >= 2){
Lianliankan.sticklist.clear();
}
}
if(Lianliankan.coordlist.size() > 0){
g.setColor(Color.RED);
int num = Lianliankan.SQUARE_SIDE/2;
for (int j2 = 1; j2 < Lianliankan.coordlist.size(); j2++) {
Point p1 = Lianliankan.coordlist.get(j2 - 1);
Point p2 = Lianliankan.coordlist.get(j2);
g.drawLine((p1.x * Lianliankan.SQUARE_SIDE + num) , (p1.y * Lianliankan.SQUARE_SIDE + num)
, (p2.x * Lianliankan.SQUARE_SIDE + num) , (p2.y * Lianliankan.SQUARE_SIDE + num));
}
if(Lianliankan.coordlist.size() >= 2){
Lianliankan.coordlist.clear();
}
}
g.setColor(Color.GRAY);
font = new Font("宋体",Font.PLAIN,15);
g.setFont(font);
startTime.setTime(Lianliankan.totalTime);
g.drawString(sdf.format(startTime), Lianliankan.JFRAME_WIDTH-Lianliankan.SQUARE_SIDE*2-5, 12);
}
}
//算法类
class GameArithmetic{
public List<Point> meet(Point p1,Point p2){
if(p1.equals(p2) || !Lianliankan.fonts.equals(Lianliankan.fonts)) return null;
int index = 0;
boolean left = true,right = true,up = true,down = true;
//分 上下左右四个方向查找 每次+1 移动的找有障碍物返回
while(left || right || up || down){
if(right && meetCols(p1,new Point(p1.x+index,p1.y))==0){
if(meetCols(p2,new Point(p1.x+index,p2.y))==0 &&
meetRows(new Point(p1.x+index,p1.y),new Point(p1.x+index,p2.y)) == 0){
return surveyPointer(p1,p2,new Point(p1.x+index,p1.y),new Point(p1.x+index,p2.y));
}
}else{
right = false;
}
if(left && meetCols(p1,new Point(p1.x-index,p1.y))==0){
if(meetCols(p2,new Point(p1.x-index,p2.y))==0 &&
meetRows(new Point(p1.x-index,p1.y),new Point(p1.x-index,p2.y)) == 0){
return surveyPointer(p1,p2,new Point(p1.x-index,p1.y),new Point(p1.x-index,p2.y));
}
}else{
left = false;
}
if(down && meetRows(p1,new Point(p1.x,p1.y+index))==0){
if(meetRows(p2,new Point(p2.x,p1.y+index))==0 &&
meetCols(new Point(p1.x,p1.y+index),new Point(p2.x,p1.y+index)) == 0){
return surveyPointer(p1,p2,new Point(p1.x,p1.y+index),new Point(p2.x,p1.y+index));
}
}else{
down = false;
}
if(up && meetRows(p1,new Point(p1.x,p1.y-index))==0){
if(meetRows(p2,new Point(p2.x,p1.y-index))==0 &&
meetCols(new Point(p1.x,p1.y-index),new Point(p2.x,p1.y-index)) == 0){
return surveyPointer(p1,p2,new Point(p1.x,p1.y-index),new Point(p2.x,p1.y-index));
}
}else{
up = false;
}
index ++;
}
return null;
}
private int meetCols(Point p1,Point p2){
int start = p1.x < p2.x ? p1.x : p2.x;
int end = p1.x < p2.x ? p2.x : p1.x;
if(start < 0 || end >= Lianliankan.map.length) return 1;
for (int i = start; i <= end ; i++) {
if(Lianliankan.map==1)
return Lianliankan.map;
}
return 0;
}
private int meetRows(Point p1,Point p2){
int start = p1.y < p2.y ? p1.y : p2.y;
int end = p1.y < p2.y ? p2.y : p1.y;
if(start < 0 || end >= Lianliankan.map.length) return 1;
for (int i = start; i <= end ; i++) {
if(Lianliankan.map==1)
return Lianliankan.map;
}
return 0;
}
private List<Point> surveyPointer(Point p1,Point p2,Point proxy1,Point proxy2){
List<Point> pList = new ArrayList<Point>();
pList.add(p1);
if(proxy1.equals(p1) && proxy2.equals(p2)){
}else if(proxy1.equals(p1)){
pList.add(proxy2);
}else if(proxy2.equals(p2)){
pList.add(proxy1);
}else{
pList.add(proxy1);
pList.add(proxy2);
}
pList.add(p2);
return pList;
}
} java的还真没有试过,套入html5就可以用手机设备玩了。谢谢分享 {:301_1001:} 还真可以玩....换个图片试试
不错不错{:301_1003:} 吾爱有你更精彩 吾爱有你更精彩 支持原创代码! 感谢楼主 绿色背景看着眼睛都瞎了
页:
[1]
2