Android连连看源码---百度文库

运行出来效果如下: 
 
 
MainActivity: 
package games.llk;  
import android.app.Activity; import android.os.Bundle;  
public class Main extends Activity { 

    /** Called when the activity is first created. */ 

    @Override 

    public void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);

         //setContentView(R.layout.main); 

        setContentView(new GameView(this));

     } 

}  

GameView: 

package games.llk;

import java.util.Random;  
import android.content.Context; 

import android.content.res.Resources;

 import android.graphics.Bitmap;

 import android.graphics.BitmapFactory; 

import android.graphics.Canvas; 

import android.graphics.Paint; 

import android.graphics.Point; 

import android.graphics.Rect; 
import android.graphics.Bitmap.Config; import android.os.Handler; import android.view.MotionEvent; 

import android.view.View;    
public class GameView extends View {   
 private int m_w; 

 private int m_h;  private int top_client; 

 private int right_client; 

 private int bottom_client;  

private int left_client;  private Rect client_r; 

 private Paint p_client_r; 
 private boolean display_moreInfo;  private boolean isInited;  

private Paint p; 

 private Paint p_line;  private Paint p_line_2;  

private Paint p_text; 

 private Paint p_pross_b;  

private Paint p_pross;  

private int point_w; 

 private int point_h;  

private Rect btn_r_1;  

private Rect btn_r_2;  

private Rect btn_r_close; 

 private Bitmap bmp_btn;  
private Rect pross_r;

private Rect pross_r_2; 

 private Rect t_r;  private Rect t_r_2; 
  private boolean ispause=false;  

private boolean isstart=false; 

 private String s_pause="暂停中。"; 

  private Bitmap buff;  

private Bitmap bmp_point;  private Bitmap bmp_hint;

  private Canvas buff_Canvas;  

private int flevel   private int ffen =0; 

 private int timecount=0; 
 private int timecountper_l = 480; 

 private boolean isdrawing=false   private Random random=new Random();  

private Resources res; 
 private Point[] pointlist_tmp    private int pointlist_tmp_count=0; 
 private RefreshHandler mRedrawHandler = new  RefreshHandler();  

private Point mousept   private Point lastpt   private Point hintpt_1   private Point hintpt_2  
 private boolean isontimering=false; 

 private boolean isοnmοusedοwn=false; 

 private int ontimercount=0; 
 class  RefreshHandler extends  Handler   { 

  @Override 
  public void  handleMessage(android.os.Message  msg)   {  

  GameView.this.ontimer(); 

    nexttimer(240); 
  } 
  public void nexttimer( long  delayMillis)    {  

  this.removeMessages(0); 
   sendMessageDelayed(obtainMessage(0), delayMillis);  

 } 
 }; 
 
private Pointlist fpointlist =new  Pointlist();

  {      for (int  i_1 = 1  i_1<=fxcount;i_1++ )

      {         for (int j_1 = 1 j_1<=fycount; j_1++ )

           { 
            p1.x = i_1;

             p1.y = j_1; 
             if (getimgidx(p1.x, p1.y) < 0 )

               continue; 
            if (getstat(p1.x, p1.y) < 0 )

               continue; 
             for (int i_2 = i_1 i_2<=fxcount;i_2++ )

             {  
 
              for (int j_2 = 1 j_2<=fycount;j_2++ )

                 { 
                  if ((i_2 == i_1) && (j_2 <= j_1) )

                     continue;

                    p2.x= i_2; 

                  p2.y= j_2; 
                   if (getimgidx(p2.x, p2.y) < 0 )

                      continue;

                   if (getstat(p2.x, p2.y) < 0 ) 
     
                continue; 
                   if (getimgidx(p1.x, p1.y) != getimgidx(p2.x, p2.y)) 

                    continue; 
                   if ( getpath(p1.x, p1.y, p2.x, p2.y) )

                     { 
                   return true;

                    } 

                }

             } 

             } 
     } 

     p1.x=-1;

      p1.y=-1;

      p2.x=-1;

      p2.y=-1;  
   return false; 
  } 
 } 
 public GameView(Context context) {   super(context); 
  display_moreInfo=false; 
  // TODO Auto-generated constructor stub   mousept= new Point(0,0);

   lastpt =new Point(0,0);

   p=new Paint();

   p_line=new Paint(); 
  p_line.setARGB(255, 255, 0, 0);

   p_line_2=new Paint(); 
  p_line_2.setARGB(255, 0, 0, 255);

   p_text=new Paint(); 
  p_text.setARGB(255, 0, 0, 0);

   p_text.setTextSize(13); 
  p_text.setFlags(Paint.ANTI_ALIAS_FLAG);

  p_pross_b=new Paint(); 
  p_pross_b.setARGB(255, 210, 210, 210);

   p_pross=new Paint(); 
  p_pross.setARGB(255, 0, 0, 210);

   p_client_r=new Paint(); 
  p_client_r.setARGB(255, 255, 255, 255);

   hintpt_1=new Point(-1,-1);

   hintpt_2=new Point(-1,-1);    
  res =context.getResources();

  isInited=false;

   setFocusable(true);  
 
 } 
 public void ontimer()  {

      if (isontimering)   {

    return;

   } 
  if (isonmousedown)

   {

    return;

   } 
  isontimering=true;    
 
if ((isstart)&&(! ispause))

  { 
    if (timecount>0)

        {    
  timecount=timecount-1;   

      }

       else

       { 

     beginlevel(flevel); 
         } 
  } 
  if (ontimercount>0 )

   {

    ontimercount=ontimercount-1;

   } 
    if(ontimercount<=0)

    {   
    drawall();

     invalidate(); 

    ontimercount=10;  
   }    
  isontimering=false; 
 }

     hintpt_2.x=-1;

      hintpt_1.y=-1;

      changecolor();

      ontimercount=1;  
  } 
  else   {    // changecolor();     
    ontimercount=1;

     if ( timecount>80 )

     {      timecount=timecount-80; 
    } 

    else

     {      timecount=40; 
    }      
  } 
 } 
  private int changtor_x(int x,Rect r)  { 
   if (x<r.left+2)

    {     return r.left+2; 
   } 
   if (x>r.right-2)

    {

     return r.right-2;

    } 
   return x;

  } 
 private int changtor_y(int y,Rect r)

  {   if (y<r.top+2)

     { 

     return r.top+2; 

    } 
    if (y>r.bottom-2) 

    {      return r.bottom-2;  
 
  } 
    return y; 
 } 
 private void drawlinerect(Point p1,Point p2){   
 
  Rect r1 = fpointlist.getrect(p1.x, p1.y);

   Rect r2 = fpointlist.getrect(p2.x, p2.y); 
  int x1=changtor_x((r1.left+r1.right)/2,client_r);

   int y1=changtor_y((r1.top+r1.bottom)/2,client_r);

   int x2=changtor_x((r2.left+r2.right)/2,client_r);

   int y2=changtor_y((r2.top+r2.bottom)/2,client_r);    
  buff_Canvas.drawLine(x1,y1 ,x2 ,y2 , p_line); 

   if (x1==x2) 

  {    buff_Canvas.drawLine(x1-1,y1 ,x2-1 ,y2 , p_line);

     buff_Canvas.drawLine(x1+1,y1 ,x2+1 ,y2, p_line);   
  } 
  if (y1==y2) 

  {    buff_Canvas.drawLine(x1,y1-1 ,x2 ,y2-1 , p_line);

     buff_Canvas.drawLine(x1,y1+1 ,x2 ,y2+1, p_line); 

    }

    } 
 private void dopoint(){    
  if (fpointlist.getstat(mousept.x, mousept.y)<0){

       return; 
  }    
  fpointlist.findstat(1,lastpt);   if (lastpt.x>=0)

   {    fpointlist.setstat(lastpt.x, lastpt.y, 0);

   }    
  if ( fpointlist.getstat(mousept.x, mousept.y)>= 0 )

   {

    fpointlist.setstat(mousept.x, mousept.y, 1);

   } 
  hintpt_1.x=-1;

   hintpt_1.y=-1;  
 
hintpt_2.x=-1; 

  hintpt_2.y=-1;

   drawpoint(); 
  if ((lastpt.x >= 0) && ((lastpt.x != mousept.x) || (lastpt.y != mousept.y)))       { 
   int lastimgidx = fpointlist.getimgidx(lastpt.x, lastpt.y);  
 
 
if ((fpointlist.getstat(mousept.x, mousept.y) >= 0) && (fpointlist.getstat(lastpt.x, lastpt.y)>= 0) && (lastimgidx >= 0) && (lastimgidx == fpointlist.getimgidx(mousept.x,mousept.y)) )

     { 

     if (fpointlist.getpath(lastpt.x, lastpt.y, mousept.x, mousept.y) ) 

     {    
  ffen=ffen+100; 
                if (timecount < timecountper_l - 40 )

                 { 
                 timecount =timecount+40;                 
                }

                 else

                 { 
                 timecount = timecountper_l;

                                 } 
                if (fpointlist.pcount==0)

                 { 
                 drawlinerect(lastpt,mousept);

                  } 
                if (fpointlist.pcount==1) 

                { 
                 drawlinerect(lastpt,fpointlist.pathlist[0]); 

                  drawlinerect(fpointlist.pathlist[0],mousept);        
                
                } 
                if (fpointlist.pcount==2) 

                {

                     drawlinerect(lastpt,fpointlist.pathlist[0]);                      
drawlinerect(fpointlist.pathlist[0],fpointlist.pathlist[1]);  
                    drawlinerect(fpointlist.pathlist[1],mousept);      
      }   
                fpointlist.setstat(lastpt.x, lastpt.y,-1); 
                fpointlist.setstat(mousept.x, mousept.y,-1);

                                fpointlist.setValue(lastpt.x, lastpt.y,false);

                 fpointlist.setValue(mousept.x, mousept.y,false);                
                 ontimercount=3; 
 
 
              if (fpointlist.isallok())  
                  { 
                    timecount = timecountper_l;  
                   
                    beginlevel(flevel + 1);

                   }

                 else

                   {

 
                 fpointlist.changeplace(flevel);

                                        }      }  
 
 
  
   
 } 
     } 
  invalidate(); 
 }   
 @Override 
 public boolean onTouchEvent(MotionEvent event) {

   // TODO Auto-generated method stub

   if (isonmousedown )

    return super.onTouchEvent(event); 
   
  isontimering=true;

   isοnmοusedοwn=true;    
  if (event.getAction() == MotionEvent. ACTION_DOWN ) {

    int  ax = ( int ) event.getX();

    int  ay = ( int ) event.getY();     
   if ( btn_r_close .contains(ax, ay)) {

     android.os.Process.killProcess(android.os.Process.myPid());    } 
   if ( btn_r_1 .contains(ax, ay)) {

     dobtn_1(); 
   } 
   if ( btn_r_2 .contains(ax, ay)) {

     dobtn_2(); 

   } 
   if  ( client_r .contains(ax, ay)) { 

    fpointlist.mousetoxy(ax, ay, mousept);

     if (mousept.x>=1)

     { 

     dopoint();   
 
 }

}     


  } 
  isοnmοusedοwn=false;   isontimering=false; 
  return super.onTouchEvent(event);  
 } 
}



  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
安卓开发--Android连连看游戏源码.zip 1. yinhe.board.AbstractBoard类: 为了初始化游戏状态,程序需要创建一个Piece[][]数组,为此程序定义一个AbstractBoard抽象类, 2. yinhe.board.GameService类 负责游戏的逻辑实现的接口。 3. yinhe.board.impl.FullBoard类: AbstractBoard的子类,实现矩形排列的方式,矩形排列的方块会填充二维数组的每个数组元素,只是把四周留空即可。 4. yinhe.board.impl.GameServiceImpl类: 游戏逻辑的实现类,实现GameService接口。 5. yinhe.board.impl.HorizontalBoard类:AbstractBoard的子类,实现横向排列的方式,横向排列的方块以水平的空行分隔开。 6. yinhe.board.impl.VerticalBoard类: AbstractBoard的子类,实现竖向排列的方式,竖向排列的方块以垂直的空列分隔开。 7. yinhe.llk.activity.LLKActivity类: 实现游戏主界面的显示,为游戏界面的按钮,GameView组建的事件提供监听器。 8. yinhe.llk.activity.StartActivity类: 游戏的开始界面,实现开始界面的显示及其中及对其中进度条进度的监听。当进度条结束后自动跳转到游戏主界面。 9. yinhe.llk.info.GameConf类: 保存游戏的初始化设置信息。 10. yinhe.llk.info.Llkinfo类: 是一个非常简单的工具类,用于封装两个方块之间的连接信息——其实就是封装一个List,List中保存了连接线需要经过的点。连连看游戏规则:两个方块最多只能用3条线段相连,也就是说最多只能有2个拐点,加上两个方块的中心,方块的连接信息最多只需要4个连接点,也就是说Linkinfo中最多需要封装4个连接点,最少需要封装2个连接点。 11. yinhe.llk.util..ImageUtil类: ImageUtil工具类的作用是自动搜寻/res/drawable-mdpi目录下的图片,并根据需要随机地读取该目录下的图片。为了让getPlayImage()方法从res\drawable-mdpi目录下随机取得N张图片,程序的实现思路分为如下几步: 1.通过反射获取R.drawable的所有Field(Android的每张图片资源会自动转换为R.drawable的静态Field),并将这些Field值添加到一个List集合中。 2.从第一步得到的List集合中随机抽取N/2个图片ID。 3.从第二步得到的N/2个图片ID全部复制一份,就得到N个图片ID,而且每个图片ID都可以找到与之配对的。 4.j将第三步得到的N个图片ID再次随机打乱,根据图片ID加载相应的Bitmap对象,最后把图片ID及对应的Bitmap封装成PieceImage后返回。 12. yinhe.llk.view.GameView类: 主要是根据游戏的状态数据绘制界面上的方块,GameView继承了View组件,重写View组件上的onDraw(Canvas canvas)方法,绘制游戏里的其它方块,还负责绘制连接方块的连接线。 13. yinhe.llk.view.Piece类: 一个Piece对象代表一个方块,用来封装方块上的图片,该方块代表二维数组中的哪个元素,获取该图片的左上角在游戏界面中的X,Y。(左上角的X,Y决定图片的绘制位置)。 14. yinhe.llk.view.PieceImage类:封装两个信息: Bitmap对象和图片资源的ID,Bitmap对象用于在游戏界面上绘制方块,而图片资源ID代表了Piece对象的标识,当两个Piece封装的图片资源的ID相等时,即可认为Piece上的图片相同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值