版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、附 录1.SnakeView类package com.example.android_snake.view;import java.util.ArrayList;import java.util.List;import java.util.Random;import java.util.Timer;import java.util.TimerTask;import com.example.android_snake.R;import com.example.android_snake.food.Food;import com.example.android_snake.snake.Body;i
2、mport com.example.android_snake.snake.Head;import com.example.android_snake.snake.Snake;import com.example.android_snake.snake.SnakeDirection;import com.example.android_snake.stone.Stone;import android.annotation.SuppressLint;import android.content.Context;import android.graphics.Bitmap;import andro
3、id.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Paint.Style;import android.os.Handler;import android.util.DisplayMetrics;import android.view.Display;import android.view.MotionEvent;import android.view.View;i
4、mport android.view.ViewManager;import android.view.WindowManager;import android.widget.Toast;public class SnakeView extends View private Context context;private Bitmap headBitmap;private Bitmap bodyBitmap;private Bitmap foodBitmap;private Bitmap stoneBitmap;/ 屏幕的高度和宽度private int screenHeight;private
5、 int screenWidth;/ 每个小格子的高度和宽度private int eachHeight;private int eachWidth;private Snake snake;private Food food;private Stone stone;private int listx;private int listy;private Timer timer = new Timer();Handler handler = new Handler() public void handleMessage(android.os.Message msg) moveSnake();inv
6、alidate();public SnakeView(Context context) super(context);this.context = context;listx =new int100;listy =new int100;/ 获得屏幕的高和宽DisplayMetrics metrics = new DisplayMetrics();WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);windowManager.getDefaultDisplay
7、().getMetrics(metrics);screenHeight = metrics.heightPixels;screenWidth = metrics.widthPixels;eachHeight = screenHeight / 32;eachWidth = screenWidth / 20;/ 初始化图片headBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.head);bodyBitmap = BitmapFactory.decodeResource(getResources(),R.drawabl
8、e.body);foodBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.food);stoneBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.stone);this.initSnake();this.initFood();this.initstone();gameRun();Overrideprotected void onDraw(Canvas canvas) super.onDraw(canvas);Paint paint = ne
9、w Paint();/ 定义画笔paint.setColor(Color.GRAY);/ 设置画笔颜色paint.setAntiAlias(true);/ 去除锯齿paint.setStyle(Style.STROKE);/ 设置空心实心paint.setTextSize(40);drawLines(canvas, paint);drawStone(canvas, paint);if(isCollide()canvas.drawText("Game Over!", screenWidth/4, screenHeight/3, paint);canvas.drawText(&
10、quot;得分", screenWidth/4, screenHeight/2, paint);timer.cancel();elsethis.drawSnake(canvas, paint);boolean flag = IsRectCollision(snake.getHead().getPointX(), snake.getHead().getPointY(), eachWidth, eachHeight,food.getPointX(), food.getPointY(), eachWidth, eachHeight);if (flag) food = null;snake.
11、getBodyList().add(new Body();this.initFood();this.initstone(); else this.drawFood(canvas, paint);this.drawStone(canvas, paint);/方向控制Overridepublic boolean onTouchEvent(MotionEvent event) int x = (int) event.getX();int y = (int) event.getY();SnakeDirection nowDir = snake.getSnakeDirection();int m = -
12、screenHeight * x + screenHeight * screenWidth - screenWidth * y;int n = screenHeight * x - screenWidth * y;if (m > 0 && n > 0) && (nowDir != SnakeDirection.DOWN) snake.setSnakeDirection(SnakeDirection.UP); else if (m > 0 && n < 0) && (nowDir != SnakeDirect
13、ion.RIGHT) snake.setSnakeDirection(SnakeDirection.LEFT); else if (m < 0 && n > 0) && (nowDir != SnakeDirection.LEFT) snake.setSnakeDirection(SnakeDirection.RIGHT); else if (m < 0 && n < 0) && (nowDir != SnakeDirection.UP) snake.setSnakeDirection(SnakeDirec
14、tion.DOWN);return super.onTouchEvent(event);public void gameRun() timer.scheduleAtFixedRate(new TimerTask() public void run() handler.obtainMessage().sendToTarget();, 100, 400);/* * 画网格线 */public void drawLines(Canvas canvas, Paint paint) int startX = 0, startY = 0;for (int i = 0; i < 100; i+) ca
15、nvas.drawLine(0, startY, screenWidth, startY, paint);startY = startY + eachHeight;for (int i = 0; i < 100; i+) canvas.drawLine(startX, 0, startX, screenHeight, paint);startX = startX + eachWidth;canvas.drawLine(0, 0, screenWidth, screenHeight, paint);canvas.drawLine(0, screenHeight, screenWidth,
16、0, paint);/ 初始化蛇public void initSnake() List<Body> bodies = new ArrayList<Body>();Head head = new Head(eachWidth * 4, eachHeight * 2, headBitmap);Body body1 = new Body(eachWidth * 3, eachHeight * 2, bodyBitmap);Body body2 = new Body(eachWidth * 2, eachHeight * 2, bodyBitmap);Body body3 =
17、 new Body(eachWidth * 1, eachHeight * 2, bodyBitmap);Body body4 = new Body(eachWidth * 0, eachHeight * 2, bodyBitmap);bodies.add(body1);bodies.add(body2);bodies.add(body3);bodies.add(body4);snake = new Snake(head, bodies, SnakeDirection.RIGHT);/ 画蛇public void drawSnake(Canvas canvas, Paint paint) ca
18、nvas.drawBitmap(headBitmap, snake.getHead().getPointX(), snake.getHead().getPointY(), paint);for (int i = 0; i < snake.getBodyList().size(); i+) canvas.drawBitmap(bodyBitmap, snake.getBodyList().get(i).getPointX(), snake.getBodyList().get(i).getPointY(), paint);/ 改变蛇身的位置public void changSnakePosi
19、tion(int pointX, int pointY) for (int i = snake.getBodyList().size() - 1; i > 0; i-) snake.getBodyList().get(i).setPointX(snake.getBodyList().get(i - 1).getPointX();snake.getBodyList().get(i).setPointY(snake.getBodyList().get(i - 1).getPointY();snake.getBodyList().get(0).setPointX(snake.getHead()
20、.getPointX();snake.getBodyList().get(0).setPointY(snake.getHead().getPointY();/ 移动蛇public void moveSnake() int nowPointX = snake.getHead().getPointX();int nowPointY = snake.getHead().getPointY();if (snake.getSnakeDirection() = SnakeDirection.RIGHT) changSnakePosition(nowPointX, nowPointY);if (nowPoi
21、ntX >= screenWidth - eachWidth) snake.getHead().setPointX(0); else snake.getHead().setPointX(nowPointX + eachWidth); else if (snake.getSnakeDirection() = SnakeDirection.DOWN) changSnakePosition(nowPointX, nowPointY);if (nowPointY >= screenHeight - eachHeight) snake.getHead().setPointY(0); else
22、 snake.getHead().setPointY(nowPointY + eachHeight); else if (snake.getSnakeDirection() = SnakeDirection.LEFT) changSnakePosition(nowPointX, nowPointY);if (nowPointX <= 0) snake.getHead().setPointX(screenWidth - eachWidth); else snake.getHead().setPointX(nowPointX - eachWidth); else if (snake.getS
23、nakeDirection() = SnakeDirection.UP) changSnakePosition(nowPointX, nowPointY);if (nowPointY <= 0) snake.getHead().setPointY(screenHeight - eachHeight); else snake.getHead().setPointY(nowPointY - eachHeight);/ 初始化foodpublic void initFood() int x = new Random().nextInt(19);int y = new Random().next
24、Int(29);food = new Food(eachWidth * x, eachHeight * y, foodBitmap);/ 在界面上画出Foodpublic void drawFood(Canvas canvas, Paint paint) if (food != null) canvas.drawBitmap(foodBitmap, food.getPointX(), food.getPointY(),paint);/ 初始化stonepublic void initstone() int x = new Random().nextInt(17);int y = new Ran
25、dom().nextInt(23);stone = new Stone(eachWidth * x, eachHeight * y, stoneBitmap);int i=0,j=0;listxi+=x;listyj+=y;/ 在界面上画出Stonepublic void drawStone(Canvas canvas, Paint paint) if (true) canvas.drawBitmap(stoneBitmap, stone.getPointX(), stone.getPointY(),paint);for(int k=0;k<100;k+)/food = new Food
26、(eachWidth * listxk, eachHeight * listyk, foodBitmap);/canvas.drawBitmap(stoneBitmap,listxk, listyk,paint);/* * 矩形碰撞检测 参数为x,y,width,height * * param x1 * 第一个矩形的x * param y1 * 第一个矩形的y * param w1 * 第一个矩形的w * param h1 * 第一个矩形的h * param x2 * 第二个矩形的x * param y2 * 第二个矩形的y * param w2 * 第二个矩形的w * param h2 *
27、 第二个矩形的h * return 是否碰撞 */public boolean IsRectCollision(int x1, int y1, int w1, int h1, int x2,int y2, int w2, int h2) if (x2 > x1 && x2 >= x1 + w1) return false; else if (x2 < x1 && x2 <= x1 - w2) return false; else if (y2 > y1 && y2 >= y1 + h1) return fals
28、e; else if (y2 < y1 && y2 <= y1 - h2) return false; else return true;/检测蛇头是否与蛇身碰撞/检测蛇头与墙的碰撞/public boolean isCollide() boolean flag = false;for (int i = 0; i < snake.getBodyList().size(); i+) flag = IsRectCollision(snake.getHead().getPointX(),snake.getHead().getPointY(), eachWidth,
29、eachHeight,snake.getBodyList().get(i).getPointX(),snake.getBodyList().get(i).getPointY(), eachWidth,eachHeight); for(int j=0;j<100;j+) flag = IsRectCollision(snake.getHead().getPointX(), snake.getHead().getPointY(), eachWidth, eachHeight, listxj,listyj, eachWidth,eachHeight); if(flag) break; if (
30、snake.getHead().getPointX() < 1) | (snake.getHead().getPointY() < 1) | (snake.getHead().getPointX() > screenWidth - 1)| (snake.getHead().getPointY() > screenHeight - 1) ) flag = true; if(flag) break; return flag;2.MainActivity类package com.example.android_snake;import com.example.android_
31、snake.R;import com.example.android_snake.view.SnakeView;/Downloads By import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.MenuItem;import android.view.Window;import android.view.WindowManager;public class MainActivity extends Activity Override public voi
32、d onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); /设置全屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); SnakeView view = new SnakeView(this); setContentView(view); Ove
33、rride public boolean onCreateOptionsMenu(Menu menu) getMenuInflater().inflate(R.menu.main, menu); return true; 3.Food类package com.example.android_snake.food;import android.graphics.Bitmap;public class Food private int pointX;private int pointY;private Bitmap foodBitmap;public Food() super();public F
34、ood(int pointX, int pointY, Bitmap foodBitmap) super();this.pointX = pointX;this.pointY = pointY;this.foodBitmap = foodBitmap;public int getPointX() return pointX;public void setPointX(int pointX) this.pointX = pointX;public int getPointY() return pointY;public void setPointY(int pointY) this.pointY
35、 = pointY;public Bitmap getFoodBitmap() return foodBitmap;public void setFoodBitmap(Bitmap foodBitmap) this.foodBitmap = foodBitmap;4.Body类package com.example.android_snake.snake;import android.graphics.Bitmap;public class Body private int pointX;private int pointY;private Bitmap bodyBitMap;public B
36、ody() super();public Body(int pointX, int pointY, Bitmap bodyBitMap) super();this.pointX = pointX;this.pointY = pointY;this.bodyBitMap = bodyBitMap;public int getPointX() return pointX;public void setPointX(int pointX) this.pointX = pointX;public int getPointY() return pointY;public void setPointY(i
37、nt pointY) this.pointY = pointY;public Bitmap getBodyBitMap() return bodyBitMap;public void setBodyBitMap(Bitmap bodyBitMap) this.bodyBitMap = bodyBitMap;5.Head类package com.example.android_snake.snake;import android.graphics.Bitmap;public class Head private int pointX;private int pointY;private Bitm
38、ap headBitMap;public Head() super();public Head(int pointX, int pointY, Bitmap headBitMap) super();this.pointX = pointX;this.pointY = pointY;this.headBitMap = headBitMap;public int getPointX() return pointX;public void setPointX(int pointX) this.pointX = pointX;public int getPointY() return pointY;p
39、ublic void setPointY(int pointY) this.pointY = pointY;public Bitmap getHeadBitMap() return headBitMap;public void setHeadBitMap(Bitmap headBitMap) this.headBitMap = headBitMap;6.Snake类package com.example.android_snake.snake;import java.util.ArrayList;import java.util.List;import android.graphics.Bitmap;public class Snake private Head head;private Body body;private List<Body> bodyList;private SnakeDirection snakeDirection;public Snake()public Snake(Head head,List<Body>
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024至2030年智能型网络机柜项目投资价值分析报告
- 淘宝运营劳动合同范例
- 风管采购安装合同范例
- 建筑总包合同范例范例
- 催收尾款合同范例
- 2024年铝合金扁丝项目可行性研究报告
- 支架施工方案
- 门面中介合同范例
- 2024年照片盒项目可行性研究报告
- 2024年木帽架项目可行性研究报告
- 2024年南京信息职业技术学院高职单招(英语/数学/语文)笔试历年参考题库含答案解析
- 2024年汽配行业分析报告
- 2024年房地产经纪协理考试题库附参考答案(综合题)
- 中药在护理中的应用
- 电工基础技能实训指导书
- 脊柱外科临床指南
- 万千教育学前透视幼儿的户外学习
- 《抗菌药物知识培训》课件
- 2024年北京市安全员A证考试题库附答案
- 医疗专业人员的情绪管理培训
- 森林法培训课件
评论
0/150
提交评论