您的位置:首页 > 移动开发 > Android开发

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

2016-03-03 23:52 429 查看
运行出来效果如下:

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 isonmousedown=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;

isonmousedown=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();

}

}

}

isonmousedown=false; isontimering=false;

return super.onTouchEvent(event);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: