貪吃蛇游戲安卓源代碼_第1頁(yè)
貪吃蛇游戲安卓源代碼_第2頁(yè)
貪吃蛇游戲安卓源代碼_第3頁(yè)
貪吃蛇游戲安卓源代碼_第4頁(yè)
貪吃蛇游戲安卓源代碼_第5頁(yè)
已閱讀5頁(yè),還剩8頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

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;/ 每個(gè)小格子的高度和寬度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();/ 定義畫(huà)筆paint.setColor(Color.GRAY);/ 設(shè)置畫(huà)筆顏色paint.setAntiAlias(true);/ 去除鋸齒paint.setStyle(Style.STROKE);/ 設(shè)置空心實(shí)心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);/* * 畫(huà)網(wǎng)格線 */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);/ 畫(huà)蛇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();/ 移動(dòng)蛇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);/ 在界面上畫(huà)出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;/ 在界面上畫(huà)出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);/* * 矩形碰撞檢測(cè) 參數(shù)為x,y,width,height * * param x1 * 第一個(gè)矩形的x * param y1 * 第一個(gè)矩形的y * param w1 * 第一個(gè)矩形的w * param h1 * 第一個(gè)矩形的h * param x2 * 第二個(gè)矩形的x * param y2 * 第二個(gè)矩形的y * param w2 * 第二個(gè)矩形的w * param h2 *

27、 第二個(gè)矩形的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;/檢測(cè)蛇頭是否與蛇身碰撞/檢測(cè)蛇頭與墻的碰撞/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); /設(shè)置全屏 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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論