2010年03月10日 星期三

使用Google Web Toolkit写的小游戏连连看0.1版

最近几天在看Google Web Toolkit,感觉还不错吧。突然想用它写个游戏玩。 我看到网上有好多javascript写的连连看,所以就自己想用gwt写一个。 花了一晚上功夫,写了个简单的,主要连接功能还没实现,目前只能把2个相同的消掉。 让大家先睹为快。 截图如下:   在线演示地址:http://www.pinfantuan.com/ 代码写的很垃圾,本来就不想贴了,可是,为了让大家多给我出主意,我还是贴出来吧 
代码
public class LianLianKan implements EntryPoint {

    
/**
     * This is the entry point method.
     
*/
    
public void onModuleLoad() {
        
final DecoratorPanel decPanel=new DecoratorPanel();
        
final Label clickLable=new Label("0");
        Grid grid 
= new Grid(65);
        grid.addStyleName(
"llktable");
        String[] rand
=new String[]{"1","2","3","4","5","6","1","2","3","4","5","6"};
        
int[] r=noRepeatRandom();
        
int k=0;
        
for(int i=0;i<6;i++){
            
for(int j=0;j<5;j++){
                
if(i!=0&&i!=5&&j!=0&&j!=4){
                    
final Image image=new Image("/image/"+rand[r[k]-1]+".gif");
                    k
=k+1;
                    image.addClickHandler(
new ClickHandler(){

                        
public void onClick(ClickEvent event) {
                            String tmpUrl
=image.getUrl();
                            tmpUrl
=tmpUrl.substring(tmpUrl.lastIndexOf("/")+1, tmpUrl.lastIndexOf("."));
                            
if(tmpUrl.indexOf("s")==-1){
                                image.setUrl(
"/image/"+tmpUrl+"s.gif");
                            }
else{
                                image.setUrl(
"/image/"+tmpUrl.substring(0, tmpUrl.indexOf("s"))+".gif");
                            }
                            
for(int i=0;i<6;i++){
                                
for(int j=0;j<5;j++){
                                    
if(i!=0&&i!=5&&j!=0&&j!=4){
                                        Image img
=(Image)((Grid)image.getParent()).getWidget(i, j);
                                        String sl
=img.getUrl();
                                        sl
=sl.substring(sl.lastIndexOf("/")+1, sl.lastIndexOf("."));
                                        
if(sl.indexOf("s")!=-1&&!img.equals(image)){
                                            
if(img.getUrl().equals(image.getUrl())){
                                                image.setVisible(
false);
                                                img.setVisible(
false);
                                                clickLable.setText(String.valueOf(Integer.parseInt(clickLable.getText())
+1));
                                            }
else{
                                                img.setUrl(img.getUrl().replaceAll(
"s.gif"".gif"));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        
                    });
                    grid.setWidget(i, j,image);
                }
else{
                    grid.setWidget(i, j, 
new Image("/image/0.gif"));
                }
            }
        }
        decPanel.add(grid);
        RootPanel.get(
"clickLable").add(clickLable);
        RootPanel.get(
"llk").add(decPanel);
    }
    
private int[] noRepeatRandom(){
         
int[] intRet = new int[12];
         
int intRd = 0//存放随机数
         int count = 0//记录生成的随机数个数
         int flag = 0//是否已经生成过标志
         while(count<12){
              Random rdm 
= new Random(System.currentTimeMillis());
              intRd 
= Math.abs(rdm.nextInt())%12+1;
              
for(int i=0;i<count;i++){
                  
if(intRet[i]==intRd){
                      flag 
= 1;
                      
break;
                  }
else{
                      flag 
= 0;
                  }
              }
              
if(flag==0){
                  intRet[count] 
= intRd;
                  count
++;
              }
     }
        
return intRet;
    }
}
   多多指教!!!