Java3*3拼图小游戏--powered by dustin

来源:互联网 发布:js 时间轴控件 编辑:程序博客网 时间:2024/05/12 16:20
终于用了2天时间写出了这个游戏,算是对事件处理和基本绘图的一种联系吧!至于更NB的功能以后会慢慢添加的,写出属于自己的作品方可谓“璀璨的是人生”!
废话我不多说了,首先介绍一下基本功能。是一个3*3的图像,素材我选了“德国总理调戏小机器人”这幅有趣的画面,之所以选这幅图有一个原因是图像分为9块之后每一块的特征都相当明显也就是说辨识度很高,这样是符合拼图的基本要求的。Java3*3拼图小游戏 - duslish - duslish的博客
 
写了这个程序我才发现即使一个简单的拼图游戏,要实现好也并不容易,尤其是对初学者查阅大量的api甚至百度上找一些用法都是家常便饭啦,不过我终于体会到CSDN上真是牛人辈出啊!不错的网站,赞一个!首先我遇到的第一个问题是如何生成一个随机的数组,要求数组大小为9(存储9个子图像的索引),最后一位即下标为8时数组值为8(这里出现空格),其余各位(0~7)要将0~7这8个数字随机安排进去,也就是说“无序不重复”。经过思考之后我采用了生成随机数的方法,new一个大小为9的数组,然后得到一个随机数,查找数组中现在有没有这个数,如果有就在获取另一个随机数(0~7之间),如果没有就将这个随机数插入数组,数组下标加一,到下标为7的位置填充完毕之后退出循环,将下标为8的位置置为8。这样就算解决了如何得到一个随机的初始乱序图像问题。
第二个问题是如何保证得到的随机乱序图像有解?经过查阅之后我发现,有一种通过“逆序数奇偶性”的算法可以解决这个问题,所以我用循环的方法自己编写了一个判断是否有解的函数。在输出窗口如果有解会是:

                   Java3*3拼图小游戏 - duslish - duslish的博客
如果没有解则会是:
Java3*3拼图小游戏 - duslish - duslish的博客
 第三个问题是解决子图像的移动问题。由于我采用了图像索引的绘制算法,只需要交换上述数组中的两个数据,就能实现两幅子图像的交换。利用这一点,我定义了两个参数the1D和the2D用来标记空格在虚拟二维矩阵中的位置。这样空格上下左右的字块都可以简单计算出来。据此修改图像索引数组,即可完成重绘。(这一点考虑了好久)
Java3*3拼图小游戏 - duslish - duslish的博客
上图是一种有解的情况初始状态。当然了拼完之后可以自己加一些花哨的东西啦。
Java3*3拼图小游戏 - duslish - duslish的博客
其实加上计算步数或者时间的功能并不难,但是游戏而已何必搞得那么紧张呢?哈哈哈。。
代码在下面,只用了一个类,好不符合面向对象编程的要求的说。。


import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import java.awt.Font;

public class Pintu extends Frame implements Runnable{
Font messageFont1,messageFont2;
String message1,message2="congratulations!",message3="恭喜您已完成!",
message4="Powered by Dustin",message5="2014-11-20";
int temp1D=10,temp2D=10;
static int the1D=2,the2D=2; //记录空位置
int x0=0,y0=0,x1=200,y1=0,x2=400,y2=0,x3=0,y3=200,x4=200,y4=200;
int x5=400,y5=200,x6=0,y6=400,x7=200,y7=400,x8=400,y8=400;
Image[] img = new Image[9];
Image yuantu;
Image bufferPage=null; //缓冲页,用于消除闪烁现象
int[] xulie = {10,10,10,10,10,10,10,10,10};
private static Random r = new Random();


public static void main (String args[]){

Pintu workstart = new Pintu();
workstart.getAXulie();

System.out.print("you get a array : ");
for(int i=0;i<9;i++){
System.out.print(workstart.xulie[i]+" ");

}
if(workstart.judgeNiXuShu()%2==0){
System.out.print(" try your best to finish it!");
}
else
System.out.print("this problem is No solution!press start again to get a new one!");
}



public void getAXulie(){ //数组的无序不重复填充
int temp;
int j=0;
while(j<8){
temp = r.nextInt(8);
if((temp!=xulie[0])&&(temp!=xulie[1])&&(temp!=xulie[2])&&(temp!=xulie[3])&&
(temp!=xulie[4])&&(temp!=xulie[5])&&(temp!=xulie[6])){
this.xulie[j++] = temp;
}

}
xulie[j]=8;
}

public int judgeNiXuShu(){
int nixushu=0;
for(int i=7;i>=0;i--){
for(int j=0;j<i;j++){
if(xulie[j]>xulie[i]){
nixushu++;
}
}
}
return nixushu;
}

public Pintu(){
super("拼图游戏--Powered by Dustin Zhou");
setSize(1000,640);

Toolkit tk = Toolkit.getDefaultToolkit();
enableEvents(AWTEvent.KEY_EVENT_MASK);
for(int i = 0;i<9;i++){
img[i] = tk.getImage("图"+i+".jpg");
}
yuantu = tk.getImage("原图.jpg");
setVisible(true);

addWindowListener(new WindowAdapter(){ //加入窗口关闭
public void windowClosing(WindowEvent evt){
System.exit(0);
}
});
new Thread(this).start();

}

public void processKeyEvent(KeyEvent e){
if(e.getID()==KeyEvent.KEY_PRESSED){
switch(e.getKeyCode()){
case KeyEvent.VK_UP:


if(the1D!=2){

temp1D=xulie[the1D*3+the2D];
xulie[the1D*3+the2D]=xulie[the1D*3+the2D+3];
xulie[the1D*3+the2D+3]= temp1D;
the1D++;
temp1D=10;
}

break;

case KeyEvent.VK_DOWN:


if(the1D!=0){

temp1D=xulie[the1D*3+the2D];
xulie[the1D*3+the2D]=xulie[the1D*3+the2D-3];
xulie[the1D*3+the2D-3]= temp1D;
the1D--;
temp1D=10;
}

break;
case KeyEvent.VK_LEFT:

if(the2D!=2){

temp2D=xulie[the1D*3+the2D];
xulie[the1D*3+the2D]=xulie[the1D*3+the2D+1];
xulie[the1D*3+the2D+1]= temp2D;
the2D++;
temp2D=10;
}


break;
case KeyEvent.VK_RIGHT:

if(the2D!=0){

temp2D=xulie[the1D*3+the2D];
xulie[the1D*3+the2D]=xulie[the1D*3+the2D-1];
xulie[the1D*3+the2D-1]= temp2D;
the2D--;
temp2D=10;
}


} //end switch
} //end if
}

public void run(){

message1 = "目标图像";
while(true){
repaint();
if((xulie[0]==0)&&(xulie[1]==1)&&(xulie[2]==2)&&(xulie[3]==3)&&(xulie[4]==4)&&
(xulie[5]==5)&&(xulie[6]==6)&&(xulie[7]==7)&&(xulie[8]==8)){
break;
}

try{Thread.sleep(200);
}catch(InterruptedException e){;}
}
}

public void update(Graphics g){
paint(g);
}

public void paint(Graphics g){
Graphics bufferg;
if(bufferPage==null)
bufferPage = createImage(1000,640);
bufferg = bufferPage.getGraphics();

bufferg.drawImage(img[xulie[0]],x0,y0,this);
bufferg.drawImage(img[xulie[1]],x1,y1,this);
bufferg.drawImage(img[xulie[2]],x2,y2,this);
bufferg.drawImage(img[xulie[3]],x3,y3,this);
bufferg.drawImage(img[xulie[4]],x4,y4,this);
bufferg.drawImage(img[xulie[5]],x5,y5,this);
bufferg.drawImage(img[xulie[6]],x6,y6,this);
bufferg.drawImage(img[xulie[7]],x7,y7,this);
bufferg.drawImage(img[xulie[8]],x8,y8,this);
bufferg.drawImage(yuantu,680,50,this);

messageFont1 = new Font("宋体",Font.PLAIN,30);
messageFont2 = new Font("宋体",Font.PLAIN,25);
bufferg.setColor(Color.blue);
bufferg.setFont(messageFont1);
bufferg.drawString(message1, 740, 35);
if((xulie[0]==0)&&(xulie[1]==1)&&(xulie[2]==2)&&(xulie[3]==3)&&(xulie[4]==4)&&
(xulie[5]==5)&&(xulie[6]==6)&&(xulie[7]==7)&&(xulie[8]==8)){
bufferg.drawString(message2, 710, 400);
bufferg.drawString(message3,750,450);
bufferg.setFont(messageFont2);
bufferg.drawString(message4,720,550);
bufferg.drawString(message5,800,590);
}


bufferg.dispose();
g.drawImage(bufferPage,getInsets().left,getInsets().top,this);


}

}



 
 
0 0
原创粉丝点击