Android海贼王连连看源代码_第1页
Android海贼王连连看源代码_第2页
Android海贼王连连看源代码_第3页
Android海贼王连连看源代码_第4页
Android海贼王连连看源代码_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Android海贼王连连看游戏源代码一个海贼王连连看的安卓游戏源码项目组织结构如图所示:layout布局文件main.xml values文件夹color.xml#ffffffff#64c6d4ef#6456648f#ff000000#64ff0000#6400ff80#2000ff80#64ff8000strings.xml Hello World, ConnectGame! 剩余时间(秒): 再次挑战 胜败乃兵家常事,不要气馁哟! 好厉害,进入下一关的话,时间会变快哟! 下一关 海贼王人物连连看v1.0 新游戏 重排列 退出游戏图片放在了drawable文件夹下AndroidManifes

2、t.xml Class文件(Java文件)CtrlView.javapackage com.tyj.onepiece;import java.util.ArrayList;import java.util.LinkedList;import java.util.List;import java.util.Random;import android.content.Context;import android.os.Handler;import android.os.Message;import android.util.AttributeSet;import android.view.Moti

3、onEvent;public class CtrlView extends GameView public final int GAMETIME = 300;public final int UPTIME = 1;public int PROCESS_VALUE = 300;public static boolean CURRENT_CH = false;public int CURRENT_TYPE = 0;private Point C_POINT;private Point P_POINT;LinkedList li;public CtrlView(Context context, At

4、tributeSet attrs) super(context, attrs);initType();initGrid();much = (row - 2) * (col - 2);public CtrlView(Context context, AttributeSet attrs, int defStyle) super(context, attrs, defStyle);initType();initGrid();much = (row - 2) * (col - 2);public boolean onTouchEvent(MotionEvent event) if (event.ge

5、tAction() != MotionEvent.ACTION_DOWN)return super.onTouchEvent(event);int selX = (int) (event.getX() / width);int selY = (int) (event.getY() / height);if (gridselXselY = 0)return true;else if (CURRENT_CH = false) select(selX, selY);CURRENT_CH = true;P_POINT = new Point(selX, selY); else C_POINT = ne

6、w Point(selX, selY);lineType = 0;if (checkLink(P_POINT, C_POINT) isLine = true;much = much - 2;if (0 PROCESS_VALUE& (PROCESS_VALUE + UPTIME) GAMETIME) PROCESS_VALUE = PROCESS_VALUE + UPTIME;invalidate();mRedrawHandler.sleep(300);CURRENT_CH = false;return true;public void reset() CURRENT_CH = false;C

7、URRENT_TYPE = 0;C_POINT = null;P_POINT = null;lineType = 0;isLine = false;Point p = null;initType();initGrid();much = (row - 2) * (col - 2);invalidate();public void rearrange() CURRENT_CH = false;CURRENT_TYPE = 0;C_POINT = null;P_POINT = null;lineType = 0;isLine = false;Point p = null;List temp = ne

8、w ArrayList();for (int i = 0; i row; i+) for (int j = 0; j col; j+) if (gridij != 0) temp.add(gridij);type.clear();Random ad = new Random();for (int i = 0; i temp.size(); i+) type.add(temp.get(i);temp.clear();temp = null;for (int i = 0; i row; i+) for (int j = 0; j col; j+) if (gridij != 0) int inde

9、x = ad.nextInt(type.size();gridij = type.get(index);type.remove(index);invalidate();private RefreshHandler mRedrawHandler = new RefreshHandler();class RefreshHandler extends Handler Overridepublic void handleMessage(Message msg) isLine = false;gridP_POINT.xP_POINT.y = 0;gridC_POINT.xC_POINT.y = 0;Ct

10、rlView.this.invalidate();public void sleep(long delayMillis) this.removeMessages(0);/ 移除信息队列中最顶部的信息(从顶部取出信息)sendMessageDelayed(obtainMessage(0), delayMillis);/ 获得顶部信息并延时发送;public class Point public int x;public int y;public Point(int newx, int newy) this.x = newx;this.y = newy;public boolean equals(

11、Point p) if (p.x = x & p.y = y)return true;elsereturn false;private boolean horizon(Point a, Point b) if (a.x = b.x & a.y = b.y)return false;int x_start = a.y = b.y ? a.y : b.y;int x_end = a.y = b.y ? b.y : a.y;for (int x = x_start + 1; x x_end; x+)if (grida.xx != 0) return false;p = new Point a, b

12、;lineType = H_LINE;return true;private boolean vertical(Point a, Point b) if (a.x = b.x & a.y = b.y)return false;int y_start = a.x = b.x ? a.x : b.x;int y_end = a.x = b.x ? b.x : a.x;for (int y = y_start + 1; y y_end; y+)if (gridya.y != 0)return false;p = new Point a, b ;lineType = V_LINE;return tru

13、e;private boolean oneCorner(Point a, Point b) Point c = new Point(a.x, b.y);Point d = new Point(b.x, a.y);if (gridc.xc.y = 0) boolean method1 = horizon(a, c) & vertical(b, c);p = new Point a, new Point(c.x, c.y), b ;lineType = ONE_C_LINE;return method1;if (gridd.xd.y = 0) boolean method2 = vertical(

14、a, d) & horizon(b, d);p = new Point a, new Point(d.x, d.y), b ;lineType = ONE_C_LINE;return method2; else return false;class Line public Point a;public Point b;public int direct;public Line() public Line(int direct, Point a, Point b) this.direct = direct;this.a = a;this.b = b;private LinkedList scan

15、(Point a, Point b) li = new LinkedList();for (int y = a.y; y = 0; y-)if (grida.xy = 0 & gridb.xy = 0& vertical(new Point(a.x, y), new Point(b.x, y)li.add(new Line(0, new Point(a.x, y), new Point(b.x, y);for (int y = a.y; y = 0; x-)if (gridxa.y = 0 & gridxb.y = 0& horizon(new Point(x, a.y), new Point

16、(x, b.y)li.add(new Line(1, new Point(x, a.y), new Point(x, b.y);for (int x = a.x; x col; x+)if (gridxa.y = 0 & gridxb.y = 0& horizon(new Point(x, a.y), new Point(x, b.y)li.add(new Line(1, new Point(x, a.y), new Point(x, b.y);return li;private boolean twoCorner(Point a, Point b) li = scan(a, b);if (l

17、i.isEmpty()return false;for (int index = 0; index li.size(); index+) Line line = (Line) li.get(index);if (line.direct = 1) if (vertical(a, line.a) & vertical(b, line.b) p = new Point a, line.a, line.b, b ;lineType = TWO_C_LINE;return true; else if (horizon(a, line.a) & horizon(b, line.b) p = new Poi

18、nt a, line.a, line.b, b ;lineType = TWO_C_LINE;return true;return false;public boolean checkLink(Point a, Point b) if (grida.xa.y != gridb.xb.y)/ 如果图案不同,直接为falsereturn false;if (a.x = b.x & horizon(a, b)return true;if (a.y = b.y & vertical(a, b)return true;if (oneCorner(a, b)return true;elsereturn t

19、woCorner(a, b);GameView.javapackage com.tyj.onepiece;/画出网格,并对应的画上分布好的图像import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.Random;import com.tyj.onepiece.CtrlView.Point;import android.content.Context;import android.graphics.Bitmap;import android.graphics.Canvas;

20、import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.View;public class GameView extends View public final int row = 10;public final int col = 10;public float width;publi

21、c float height;private int selY;private int selX;public boolean isLine = false;public int grid = new introwcol;private Rect selRect = new Rect();public int lineType = 0;public final int V_LINE = 1;public final int H_LINE = 1;public final int ONE_C_LINE = 2;public final int TWO_C_LINE = 3;public int

22、much = 0;Point p;public int imageType = new int R.drawable.aa, R.drawable.bb,R.drawable.cc, R.drawable.dd, R.drawable.ee, R.drawable.ff,R.drawable.gg, R.drawable.hh, R.drawable.ii, R.drawable.jj,R.drawable.kk, R.drawable.ll, R.drawable.mm, R.drawable.nn,R.drawable.oo, R.drawable.pp ;public Bitmap im

23、age;public List type = new ArrayList();public GameView(Context context, AttributeSet attrs) super(context, attrs);this.setFocusable(true);this.setFocusableInTouchMode(true);public GameView(Context context, AttributeSet attrs, int defStyle) super(context, attrs, defStyle);this.setFocusable(true);this

24、.setFocusableInTouchMode(true);public void reset() public void fillImage(Context context) int lth = imageType.length;image = new Bitmaplth;for (int i = 0; i lth; i+) Bitmap bitmap = Bitmap.createBitmap(int) width, (int) height,Bitmap.Config.ARGB_8888);Drawable drw;Canvas canvas = new Canvas(bitmap);

25、drw = context.getResources().getDrawable(imageTypei);drw.setBounds(1, 1, 30, 30);drw.draw(canvas);imagei = bitmap;public void initType() int size = (row - 2) * (col - 2);int count = size / imageType.length;for (int j = 0; j imageType.length; j+) for (int i = 0; i count; i+) type.add(imageTypej);publ

26、ic void select(int x, int y) invalidate(selRect);selX = Math.min(Math.max(x, 0), 9);selY = Math.min(Math.max(y, 0), 9);getRect(selX, selY, selRect);invalidate(selRect);private void getRect(int x, int y, Rect rect) rect.set(int) (x * width), (int) (y * height),(int) (x * width + width), (int) (y * he

27、ight + height);Overrideprotected void onDraw(Canvas canvas) Paint background = new Paint();background.setColor(Color.WHITE);canvas.drawRect(0, 0, getWidth(), getHeight(), background);Paint hilite = new Paint();hilite.setColor(getResources().getColor(R.color.hilite);Paint light = new Paint();light.se

28、tColor(getResources().getColor(R.color.light);/画出网格for (int i = 0; i = 9; i+) canvas.drawLine(0, i * height, getWidth(), i * height, light);canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1,hilite);canvas.drawLine(i * width, 0, i * width, getHeight(), light);canvas.drawLine(i * width + 1

29、, 0, i * width + 1, getHeight(),hilite);if (CtrlView.CURRENT_CH) Paint selected = new Paint();selected.setColor(getResources().getColor(R.color.puzzle_selected);canvas.drawRect(selRect, selected);for (int i = 0; i 9; i+) for (int j = 0; j 9; j+) if (gridij != 0) canvas.drawBitmap(imageArrays.binaryS

30、earch(imageType,gridij), i * width, j * height, null);if (isLine) Paint lineColor = new Paint();lineColor.setColor(Color.RED);switch (lineType) case V_LINE:canvas.drawLine(p0.x * width + width / 2, p0.y * height+ height / 2, p1.x * width + width / 2, p1.y* height + height / 2, lineColor);break;case

31、ONE_C_LINE:canvas.drawLine(p0.x * width + width / 2, p0.y * height+ height / 2, p1.x * width + width / 2, p1.y* height + height / 2, lineColor);canvas.drawLine(p1.x * width + width / 2, p1.y * height+ height / 2, p2.x * width + width / 2, p2.y* height + height / 2, lineColor);break;case TWO_C_LINE:c

32、anvas.drawLine(p0.x * width + width / 2, p0.y * height+ height / 2, p1.x * width + width / 2, p1.y* height + height / 2, lineColor);canvas.drawLine(p1.x * width + width / 2, p1.y * height+ height / 2, p2.x * width + width / 2, p2.y* height + height / 2, lineColor);canvas.drawLine(p3.x * width + widt

33、h / 2, p3.y * height+ height / 2, p2.x * width + width / 2, p2.y* height + height / 2, lineColor);break;default:break;super.onDraw(canvas);Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) width = w / row;height = h / col;/ getRect(1,1,selRect);fillImage(this.getContext();super.

34、onSizeChanged(w, h, oldw, oldh);public void initGrid() Random ad = new Random();for (int i = 0; i row; i+) for (int j = 0; j 0) int index = ad.nextInt(type.size();gridij = type.get(index);type.remove(index);OnePieceGame.javapackage com.tyj.onepiece;import android.app.Activity;import android.util.Att

35、ributeSet;import android.app.AlertDialog;import android.content.DialogInterface;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.Menu;import android.view.MenuItem;import android.widget.ProgressBar;import android.widget.TextView;public class OnePieceGam

36、e extends Activity /* Called when the activity is first created. */private ProgressBar pb;private TextView show_RemainTime;private CtrlView cv;public static final int START_ID = Menu.FIRST;public static final int REARRARY_ID = Menu.FIRST + 1;public static final int END_ID = REARRARY_ID + 1;private i

37、nt dormant = 1000;private boolean isCancel=true;Overridepublic void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.main);findViews();mRedrawHandler.sleep(dormant);private RefreshHandler mRedrawHandler = new RefreshHandler();class RefreshHandler extends

38、 Handler Overridepublic void handleMessage(Message msg) if(isCancel)run();elsepublic void sleep(long delayMillis) this.removeMessages(0);/ 移除信息队列中最顶部的信息(从顶部取出信息)sendMessageDelayed(obtainMessage(0), delayMillis);/ 获得顶部信息并延时发送;public void run() if (cv.PROCESS_VALUE 0 & cv.much != 0) cv.PROCESS_VALUE-;

39、pb.setProgress(cv.PROCESS_VALUE);show_RemainTime.setText(String.valueOf(cv.PROCESS_VALUE);mRedrawHandler.sleep(dormant); else if (cv.PROCESS_VALUE = 0 & cv.much != 0) cv.setEnabled(false);dialogForFail().show(); else if (cv.PROCESS_VALUE != 0 & cv.much = 0) cv.setEnabled(false);dialogForSucceed().sh

40、ow();private void findViews() pb = (ProgressBar) findViewById(R.id.pb);show_RemainTime = (TextView) findViewById(R.id.show_remainTime);cv = (CtrlView) findViewById(R.id.cv);pb.setMax(cv.GAMETIME);pb.incrementProgressBy(-1);pb.setProgress(cv.PROCESS_VALUE);Overridepublic boolean onCreateOptionsMenu(M

41、enu menu) / TODO Auto-generated method stubmenu.add(0, START_ID, 0, R.string.newgame);menu.add(0, REARRARY_ID, 0, R.string.rearrage);menu.add(0, END_ID, 0, R.string.exit);return super.onCreateOptionsMenu(menu);Overridepublic boolean onOptionsItemSelected(MenuItem item) switch (item.getItemId() case

42、START_ID:newPlay();break;case REARRARY_ID:cv.rearrange();cv.PROCESS_VALUE = cv.PROCESS_VALUE - 5;pb.setProgress(cv.PROCESS_VALUE);break;case END_ID:isCancel=false;finish();break;default:break;return super.onOptionsItemSelected(item);Overrideprotected void onStop() isCancel=false;pb = null;cv = null;super.onStop();Overrideprotected void onDestroy()isCancel=false;super.onDestroy();Overrideprotected void

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论