Java小游戏源码(JavaSE拼图游戏)

1.概述

大一时用Java写的一个拼图游戏,主要是对Swing组件的应用,使用了JFrame,JPanel,JButton等组件,对于学习Swing有一定的帮助。

2.界面截图

3.核心代码

	//获取到当前所点击的小方格163*152
	Cell button =(Cell)e.getSource();
	//获取所点击方格的X,Y坐标
	int clickX = button.getBounds().x;
	int clickY = button.getBounds().y;	
	//获取当前空方格的X,Y坐标
	int nullX = nullCell.getBounds().x;
	int nullY = nullCell.getBounds().y;
	//进行比较,如果满足条件进行位置的交换	
	if(clickX == nullX && clickY - nullY == 152){//点击的为空方格下面的方格
	button.move("UP");//所点击的方格向上移动	
	} else if(clickX == nullX && clickY - nullY == -152){
	button.move("DOWN");//所点击的方格向上移动	
	} else if(clickY == nullY && clickX -nullX == 163){
	button.move("LEFT");	
	} else if(clickY == nullY && clickX -nullX == -163){
	button.move("RIGHT");
	} else{
		return ;
	}
	//更新空方格的位置
	nullCell.setLocation(clickX, clickY);
	//拼图区界面重新绘制
	this.repaint();
	//更新步数,将游戏状态区的步数更新
	stepNum++;
	PictureMainFrame.step.setText("步数:"+stepNum);
	
	//判断当前游戏是否完成,若完成,给玩家一个友好的提示
	if(this.isFinish()){
		//弹出一个窗口提示
		JOptionPane.showMessageDialog(this, "恭喜你完成拼图!!!\n所用步"+ stepNum);
		//撤销每一个小方格上的鼠标点击监听,让鼠标点击小方格不再起作用
		for(int i=0;i<11;i++){
			cell[i].removeMouseListener(this);
		}
		
		//更新方格的动作监听器的状态
		hasAddActionListener = false;		
	}
	
	}
	//判断当前游戏是否完成,根据坐标判断拼图是否完成
	private boolean isFinish(){
		for(int i=0 ;i<11; i++){
			//获取每一个方格的位置
			int x = cell[i].getBounds().x;
			int y = cell[i].getBounds().y;
			if((y-20)/152*3+(x/163)!=i){
			return false;	
			}
			
			
		}

关注微信公众号:小诸葛的博客,回复097获取项目源码,如在学习过程中遇到问题,也可联系本人qq:2370775541

import java.awt.*; import java.applet.*; import java.awt.event.*; public class PPuzzle extends Applet{ Image imgPuzzle,buff; Point fifteen=new Point(3,3); int[][] map={{0,4,8,12},{1,5,9,13},{2,6,10,14},{3,7,11,15}}; int sx,sy; Canvas screen; Graphics gs,gb; boolean running=false; Button bStart= new Button("新游戏"); Button bSee=new Button("显示正确图像"); public void init(){ prepareImage(); sx=imgPuzzle.getWidth(this)/4; sy=imgPuzzle.getHeight(this)/4; setBackground(Color.blue); initScreen(); initButtons(); add(screen); add(bStart); add(bSee); } void prepareImage(){ imgPuzzle=getImage(getCodeBase(),"images/3.jpg");// MediaTracker mt=new MediaTracker(this); mt.addImage(imgPuzzle, 0); try{ mt.waitForAll(); }catch(Exception e){} //创建buffer并获取graphics对象 buff=createImage(imgPuzzle.getWidth(this),imgPuzzle.getHeight(this)); gb=buff.getGraphics(); } void initMap(){ java.util.Random rnd=new java.util.Random(); int temp,x1,x2,y1,y2; for(int i=0;i<100;i++){ x1=rnd.nextInt(4); x2=rnd.nextInt(4); y1=rnd.nextInt(4); y2=rnd.nextInt(4); temp=map[x1][y1]; map[x1][y1]=map[x2][y2]; map[x2][y2]=temp; } outer:for(int j=0;j<4;j++) for(int i=0;i<4;i++) if(map[i][j]==15){ fifteen.setLocation(i,j); break outer; } } void initScreen(){ screen=new Canvas(){ public void paint(Graphics g){ if(gs==null) gs=getGraphics(); if(running) drawScreen(); else g.drawImage(imgPuzzle,0,0,this); } }; screen.setSize(imgPuzzle.getWidth(this), imgPuzzle.getHeight(this)); screen.addMouseListener(new MouseAdapter(){ public void mousePressed(MouseEvent me){ if(!running)return; int x=me.getX()/sx,y=me.getY()/sy; int fx=(int)fifteen.getX(),fy=(int)fifteen.getY(); if (Math.abs(fx-x)+Math.abs(fy-y)>=2)return; map[fx][fy]=map[x][y]; map[x][y]=15; fifteen.setLocation(x,y); drawScreen(); } }); } void initButtons(){ //新游戏 按钮事件的处理 bStart.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ initMap(); drawScreen(); running=true; bSee.setLabel("显示正确图像"); } }); //显示正确图像 按钮事件处理 bSee.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ //bsee 按钮标题为”继续游戏“ if(bSee.getLabel().equals("继续游戏")){ drawScreen(); bSee.setLabel("继续游戏"); } else{ //bsee的标题显示为”显示正确图像“ gs.drawImage(imgPuzzle,0,0,screen); bSee.setLabel("继续游戏"); } } }); } void drawScreen(){ gb.clearRect(0, 0, sx*4, sy*4); //将指定位置的图像块绘制到Buffer中 for(int j=0;j<4;j++) for(int i=0;i<4;i++) if(map[i][j]!=15) drawSegment(map[i][j],i,j); //向Screen绘制buffer中的图像 gs.drawImage(buff,0,0,screen); } void drawSegment(int seg,int x,int y){ int dx=seg%4*sx,dy=seg/4*sy; //可能有错误 gb.drawImage(imgPuzzle, x*sx,y*sy ,x*sx+sx-1 ,y*sy+sy-1 , dx , dy,dx+sx-1,dy+sy-1 ,screen ); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值