贪吃蛇游戏安卓源代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

贪吃蛇游戏安卓源代码

附录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;

import 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 android.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;

import 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 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();

invalidate();

}

};

public SnakeView(Context context) {

super(context);

this.context = context;

listx =new int[100];

listy =new int[100];

// 获得屏幕的高和宽

DisplayMetrics metrics = new DisplayMetrics();

WindowManager windowManager = (WindowManager) context

.getSystemService(Context.WINDOW_SERVICE);

windowManager.getDefaultDisplay().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.drawable.body);

foodBitmap = BitmapFactory.decodeResource(getResources(),

R.drawable.food);

stoneBitmap = BitmapFactory.decodeResource(getResources(),

R.drawable.stone);

this.initSnake();

this.initFood();

this.initstone();

gameRun();

}

@Override

相关文档
最新文档