Java 游戏 连连看 语言编写 多媒体

3 篇文章 0 订阅
2 篇文章 0 订阅
///所需文件,图片和录音应按图示层次存放,和命名!








import java.applet.Applet;
import java.awt.event.MouseListener;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;



@SuppressWarnings("serial")
public class Lianlian extends Applet implements MouseListener
{
	public int HEIGHT=8;
	public int WIDTH=8;
	public int IMAGES=14;
	public int array[][]=new int[HEIGHT][WIDTH];
	public Image images[];
	
	public int xofw;
	public int yofw;
	public int count=0;
	
	public boolean isSecondClick=false;
	public Position pos1,pos2;
	
	public Lianlian()
	{
		this.setName("连连看");
	}
	
	public void init()
	{
		this.add(new JPopupMenu("设置难度").add(new JRadioButtonMenuItem(" ITEMS")));
		images=new Image[IMAGES+1];
		for(int i=0;i<=IMAGES;i++)
		{
			images[i]=getImage(getCodeBase(),"images/"+i+".jpg");
			System.out.print("	"+"images/"+i+".jpg");
		}
		
		addMouseListener(this);
		
		System.out.println("HIGHT:"+HEIGHT+"\nWIDTH:"+WIDTH+"\nIMAGES:"+IMAGES+"\n");
		
		produceArray();
		printArray();
		this.setSize(280,280);
	}
	
	public void paint(Graphics g)
	{
		Dimension dimen=getSize();
		xofw=dimen.width/WIDTH;
		yofw=dimen.height/HEIGHT;
		g.setColor(Color.red);
		for(int i=0;i<=WIDTH;i++)
		{
			g.drawLine(i*xofw, 0, i*xofw, dimen.height);
		}
		for(int i=0;i<=HEIGHT;i++)
		{
			g.drawLine(0, i*yofw, dimen.width, i*yofw);
		}
		
		loadImages(g);
		
	}
	
	public void loadImages(Graphics g)
	{
		for(int i=0;i<HEIGHT;i++)
		{
			for(int j=0;j<WIDTH;j++)
			{
				g.drawImage(images[array[i][j]], j*xofw, i*yofw, xofw, yofw, this);
			}
		}
	}
	
	public void printArray()
	{
		for(int i=0;i<HEIGHT;i++)
		{
			for(int j=0;j<WIDTH;j++)
			{
				System.out.print(array[i][j]+"	");
			}
			System.out.println();
		}
	}
	public void produceArray()
	{
		
		int num=WIDTH*HEIGHT;
		int count=0;
		for(;count<num;count+=2)
		{
			putAPairElements(((int)(Math.random()*10000))%IMAGES+1);
		}
		
	}

	public void putAPairElements(int n)
	{
		Position p1=getAPosition();
		array[p1.getY()][p1.getX()]=n;
		Position p2=getAPosition();
		array[p2.getY()][p2.getX()]=n;
	}
	
	public Position getAPosition()
	{
		while(true)
		{
			int x=((int)(Math.random()*1000))%WIDTH;
			int y=((int)(Math.random()*1000))%HEIGHT;
			System.out.println("\n两个随机数:"+x+" " +y);
			if(array[y][x]==0)
			{
				return new Position(x,y);
			}
		}
	}
	
	public Position getPosition(int x,int y)
	{
		return new Position(x/xofw,y/yofw);
	}
	
	public void mouseClicked(MouseEvent e)
	{
	    
	}
	
	public void mouseEntered(MouseEvent e)
	{
		
	}
	
	public void mouseExited(MouseEvent e)
	{
		
	}
	
	public void mousePressed(MouseEvent e)
	{
		
	}
	
	public void mouseReleased(MouseEvent e)
	{
			System.out.println(isSecondClick);

		int x = e.getX();
		int y = e.getY();
		if(!isSecondClick)					//***获取第一个比较的Position对象
		{
			pos1=getPosition(x,y);
			if(array[pos1.y][pos1.x]==0)
				return;
			isSecondClick=true;
			playAudio(1);

			printArray();
			return;
		}
		else								//***获取第二个比较的Position对象	
		{
			pos2=getPosition(x,y);
			if(pos1.equals(pos2))		//***第二个Position对象与第一个对象是同一个
			{
				repaint();
				playAudio(3);
				isSecondClick=false;

				printArray();
				return;
			}
			else						//***第二个Position对象和第一个是不同的
			{
				int temp=array[pos1.y][pos1.x];
				
				if(array[pos1.y][pos1.x]==array[pos2.y][pos2.x])//***两个Position映射得到的数组元素的值------相等
				{
					array[pos1.y][pos1.x]=0;
					array[pos2.y][pos2.x]=0;
					
					if(isReachable(pos1,pos2))
					{
						playAudio(2);
						isSecondClick=false;
						count+=2;
						if(count==WIDTH*HEIGHT)
							playAudio(4);
					}
					else
						{
							array[pos1.y][pos1.x]=temp;
							array[pos2.y][pos2.x]=temp;
							playAudio(3);
							isSecondClick=false;
						}
					repaint();

					printArray();
				}
				else											//***两个Position映射得到的数组元素的值------不相等
				{
					playAudio(3);
					isSecondClick=false;

					printArray();
				}
				return;
				
			}
			
			
		}
		
	}
	

	
	public boolean isReachable(Position p1, Position p2)
	{
		int y1=p1.getY();
		int x1=p1.getX();
		int y2=p2.getY();
		int x2=p2.getX();
		
		if(p1.equals(p2))
			return false;
		if(x1==x2&&(int)Math.abs(y1-y2)==1||y1==y2&&(int)Math.abs(x1-x2)==1)
			return true;
		
		int min_x=3*Math.max(WIDTH, HEIGHT);//**********************************左右
		int min_y=3*Math.max(WIDTH, HEIGHT);//**********************************上下
		
		{
			int m;
			for(int i=-1;i<=WIDTH;i++)
			{
				m=getConnectLengthX(x1,x2,y1,y2,i);
				if(m<min_x)
					min_x=m;
			}
		}
		
		

		{
			int m;
			for(int i=-1;i<=HEIGHT;i++)
			{
				m=getConnectLengthY(x1,x2,y1,y2,i);
				if(m<min_y)
					min_y=m;
			}
		}
		
		if(Math.min(min_x,min_y)<3*Math.max(HEIGHT, WIDTH))
			return true;
		return false;
	}
	
	public int getConnectLengthX(int x1, int x2, int y1, int y2, int X)
	{
		if(isConnectiable(x1, y1, X, y1)==false||isConnectiable(x2, y2, X, y2)==false||isConnectiable(X, y1, X, y2)==false)
			return 3*Math.max(WIDTH, HEIGHT);
		return Math.abs(y1-y2)+Math.abs(x1-X)+Math.abs(x2-X);
	}
	
	public int getConnectLengthY(int x1, int x2, int y1, int y2, int Y)
	{
		if(isConnectiable(x1, y1, x1, Y)==false||isConnectiable(x2, y2, x2, Y)==false||isConnectiable(x1, Y, x2, Y)==false)
			return 3*Math.max(WIDTH, HEIGHT);
		return Math.abs(x1-x2)+Math.abs(y1-Y)+Math.abs(y2-Y);
	}
	
	//判断(x1,y1),(x2,y2)是否可以直线连接。
	public boolean isConnectiable(int x1, int y1, int x2, int y2)
	{
		if(x1==x2&&x1<0||y1==y2&&y1<0||x1==x2&&x1>=WIDTH||y1==y2&&y1>=HEIGHT)
			return true;
		
		x1=x1<0?0:(x1>=WIDTH?WIDTH:x1);
		y1=y1<0?0:(y1>=HEIGHT?HEIGHT:y1);
		x2=x2<0?0:(x2>=WIDTH?WIDTH-1:x2);
		y2=y2<0?0:(y2>=HEIGHT?HEIGHT-1:y2);
		
		if(x1!=x2&&y1!=y2)
			return false;
		if(x1==x2&&y1==y2&&array[y1][x1]==0)
			return true;
		if(x1==x2&&y1==y2&&array[y1][x1]!=0)
			return false;
		if(x1==x2)
		{
			int min=Math.min(y1, y2);
			int max=Math.max(y1, y2);
			for(int i=min; i<=max; i++)
			{
				if(array[i][x1]!=0)
					return false;
			}
			return true;
		}
		if(y1==y2)
		{
			int min=Math.min(x1, x2);
			int max=Math.max(x1, x2);
			for(int i=min; i<=max; i++)
			{
				if(array[y1][i]!=0)
					return false;
			}
			return true;
		}
		return false;
		
	}
	
	public void playAudio(int i)
	{
		switch(i)
		{
			case 1:
				play(getCodeBase(),"audio/beep.au");
				break;
			case 2:
				play(getCodeBase(),"audio/ding.au");
				break;
			case 3:
				play(getCodeBase(),"audio/return.au");
				break;
			case 4:
				play(getCodeBase(),"audio/yahoo1.au");
				break;
			default:
				break;
				
				
		}
	}
	
	public String getAppletInfo()
	{
		return "这是一个连连看程序!\nCopyright by :\tzhuxiangfeicool@qq.com\n\t\tQQ:1032983642\nCSDN博客:http://blog.csdn.net/zhuxiangfeicool\n";
	}
	
	/*
	public String[][] getParameterInfo()
	{
		
	}
	*/
}

class Position
{
	public int x;
	public int y;
	
	public Position()
	{
		x=0;y=0;
	}
	
	public Position(int n,int m)
	{
		setX(n);
		setY(m);
	}
	
	public Position(Position p)
	{
		setX(p.getX());
		setY(p.getY());
	}
	public boolean equals(Position p)
	{
		if(this.getX()==p.getX()&&this.getY()==p.getY())
			return true;
		return false;
	}
	
	
	
	public void setX(int n)
	{
		x=n;
	}
	
	public void setY(int n)
	{
		y=n;
	}
	
	public int getX()
	{
		return x;
	}
	
	public int getY()
	{
		return y;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值