public class body {
public int ax;//代表X周变量
public int ay;//代表Y轴变量
public int getAx() {
return ax;
}
public void setAx(int ax) {
this.ax = ax;
}
public int getAy() {
return ay;
}
public void setAy(int ay) {
this.ay = ay;
}
public body(int a, int b){//构造函数
this.ax = a;
this.ay = b;
}
}
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class GetStone {//获取图片
public static Bitmap createBitmap(Context context,int resId){
Bitmap map = BitmapFactory.decodeResource(context.getResources(),resId);
return map;
}
}
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
private rungame game;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//全屏显示
super.onCreate(savedInstanceState);
int width =getWindowManager().getDefaultDisplay().getWidth();//获得屏幕宽度
int height = getWindowManager().getDefaultDisplay().getHeight();//获得屏幕高度
game = new rungame(this,width,height);
setContentView(game);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
import java.util.ArrayList;
import java.util.Random;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;
import android.view.View;
import android.view.View.OnTouchListener;
public class rungame extends SurfaceView implements Callback,OnTouchListener{
private Context context;
private SurfaceHolder holder;
private int screenw;//传递屏幕宽度
private int screenh;//传递屏幕高度
private static int HH = 0;//获得砖块的高度,设置为全局变量
private static int WW = 0;//获得砖块的宽度,设置为全局变量
// private static int [][] bodystart = {{3,8},{3,7},{3,6},{3,5}};//贪吃蛇初始化出现的位置
private static ArrayList<body> snakeBodies = new ArrayList<body>();//LIST数组的蛇身位置
private static ArrayList<body> star = new ArrayList<body>();//LIST数组的星星位置
private static final int UP = 1;//移动方向向上
private static final int DOWN = 2;//移动方向向下
private static final int LEFT = 3;//移动方向向左
private static final int RIGHT = 4;//移动方向向右
private static int DirectionMove;//当前移动方向,并初始化为向下
private static int NextDirectionMove=DOWN;//当前移动方向,并初始化为向下
private static long StayTime = 600;//绘制停留时间,初始化为600毫秒
private static int Score = 0;//分数,默认为0
public rungame(Context context,int screenwidth,int screenheight) {
super(context);
this.context = context;
this.screenh = screenheight;
this.screenw = screenwidth;
holder = this.getHolder();
holder.addCallback(this);
setOnTouchListener(this);
getWH();//初始化,取得砖块的长宽(游戏以砖墙的长宽为基础)
setArrayList();//初始化蛇身的位置
setstar();//初始化星星的位置
}
private void draw(){//绘制初始化
Canvas canvas = holder.lockCanvas();
drawwall(canvas);//绘制墙壁
drawstar(canvas);//绘制星星
drawsnake(canvas);//绘制蛇身
holder.unlockCanvasAndPost(canvas);
}
public void drawstar(Canvas canvas){//随即绘制两个个星星
super.onDraw(canvas);
// Canvas canvas = holder.lockCanvas();
Bitmap mapstar =GetStone.createBitmap(context, R.drawable.star);
// canvas.drawBitmap(mapstar, star.get(0).ax*WW, star.get(0).ay*HH,null);
// canvas.drawBitmap(mapstar, star.get(1).ax*WW, star.get(1).ay*HH,null);
for(int i=0;i<star.size();i++ ){//画出星星
int px = star.get(i).ax;
int py = star.get(i).ay;
canvas.drawBitmap(mapstar, px*WW, py*HH, null);
}
// holder.unlockCanvasAndPost(canvas);
}
private ArrayList<body> setstar(){
int [][] yes1 = randompoint();
int [][] yes2 = randompoint();
Log.i("FFF", "star的坐标:"+yes1[0][0]+" "+yes1[0][1]);
Log.i("FFF", "star的坐标:"+yes2[0][0]+" "+yes2[0][1]);
body boss1 = new body(yes1[0][0], yes1[0][1]);
body boss2 = new body(yes2[0][0], yes2[0][1]);
star.add(boss1);
star.add(boss2);
Log.i("FFF", "star的长度:"+star.size());
return star;
}
public int[][] randompoint(){//在空白处随机产生一个数组,用于产生星星的位置
Random random = new Random();
int b,d;
int a = screenh/HH;
int c = screenw/WW;
ArrayList<body> bossBody = new ArrayList<body>();
do{
d = random.nextInt(c-2)+1;
b = random.nextInt(a-2)+1;
body booss = new body(d, b);
bossBody.add(booss);
}
while(snakeBodies.equals(bossBody));//最初的位置不能跟蛇身的位置重合
int [][] arraystar = {{d,b}};
Log.i("Width", "宽度格数"+c);
Log.i("Height", "高度格数"+a);
return arraystar;
}
public ArrayList<body> setArrayList(){//蛇身的位置
body bos1 = new body(1,8);
body bos2 = new body(1,7);
body bos3 = new body(1,6);
body bos4 = new body(1,5);
snakeBodies.add(bos1);
snakeBodies.add(bos2);
snakeBodies.add(bos3);
snakeBodies.add(bos4);
return snakeBodies;
}
public void drawsnake(Canvas canvas){//初始化一条蛇,由数组控制蛇的样子
int movex,movey;
super.onDraw(canvas);
DirectionMove=NextDirectionMove;//将变初始向设置为变更方向
switch (DirectionMove) {
case UP:
movex = 0;
movey = -1;
break;
case DOWN:
movex = 0;
movey = 1;
break;
case LEFT:
movex = -1;
movey = 0;
break;
default:
movex = 1;
movey = 0;
break;
}
Bitmap mapbody = GetStone.createBitmap(context, R.drawable.body);
body mms = new body(snakeBodies.get(0).ax, snakeBodies.get(0).ay);//获得蛇头的坐标
mms.ax+=movex;//蛇头下一步X轴的坐标
mms.ay+=movey;//蛇头下一步Y轴的坐标
Log.i("SHOWmovex", " "+movex);
Log.i("SHOWmovex", " "+movey);
Log.i("添加前snakeBodiesSIZE", " "+snakeBodies.size());
snakeBodies.add(0,mms);//将该蛇头下一步的坐标插入蛇头的位置,后面蛇身的位置向后移动
Log.i("添加后snakeBodiesSIZE", " "+snakeBodies.size());
if(!eatcheck()){
snakeBodies.remove(snakeBodies.size()-1);//删除蛇的最后
}
for(int i=0;i<snakeBodies.size();i++ ){//画出蛇身
int px = snakeBodies.get(i).ax;
int py = snakeBodies.get(i).ay;
canvas.drawBitmap(mapbody, px*WW, py*HH, null);
}
Log.i("EVEPOINT", "每次蛇头的坐标"+snakeBodies.get(0).ax+" "+snakeBodies.get(0).ay);
try {
Thread.sleep(StayTime);
} catch (Exception e) {
// TODO: handle exception
}
}
private void getWH(){
Bitmap mapcrack =GetStone.createBitmap(context, R.drawable.crack);
WW = mapcrack.getWidth();
HH = mapcrack.getHeight();
}
public void drawwall(Canvas canvas){//绘制墙壁
super.onDraw(canvas);
// Canvas canvas = holder.lockCanvas();
Bitmap mapcrack =GetStone.createBitmap(context, R.drawable.crack);
// WW = mapcrack.getWidth();
// HH = mapcrack.getHeight();
int ssh = screenh/HH;
int ssw = screenw/WW;
canvas.drawColor(Color.WHITE);
for(int i=0;i<=ssh*HH;i+=HH){//绘制墙壁左边
canvas.drawBitmap(mapcrack, 0, i,null);
}
for(int i=0;i<=ssh*HH;i+=HH){//绘制墙壁右边
canvas.drawBitmap(mapcrack, (ssw-1)*WW, i,null);//这里有一点问题
}
for(int i=WW;i<=ssw*WW-WW;i+=WW){//绘制墙壁上边
canvas.drawBitmap(mapcrack, i, 0,null);
}
for(int i=WW;i<=ssw*WW-WW;i+=WW){//绘制墙壁下边
canvas.drawBitmap(mapcrack, i, ssh*HH,null);
}
// holder.unlockCanvasAndPost(canvas);
}
public void drawScore(){//绘制分数
Canvas canvas = holder.lockCanvas();
// canvas.drawColor(Color.WHITE);//注释掉后会直接绘制到游戏界面上
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setTextSize(30);
canvas.drawText("总分数:"+Score, screenw/3,screenh/2, paint);//确定绘制分数位置
holder.unlockCanvasAndPost(canvas);
}
class moveThread implements Runnable{//创建线程
@Override
public void run() {
while(runinto()){
draw();
}
drawScore();
}
}
public boolean runinto(){//碰撞检测
if(snakeBodies.get(0).ax == 0 || snakeBodies.get(0).ax == (screenw/WW-1)
|| snakeBodies.get(0).ay == 0 || snakeBodies.get(0).ay == (screenh/HH-1)){//蛇头装墙壁上
Log.i("RRR", "false1");
return false;
}else if(runbody(snakeBodies.get(0))){//蛇头撞蛇身上
Log.i("RRR", "false2");
return false;
}else {
Log.i("RRR", "true");
return true;
}
}
public boolean runbody(body oop){//蛇头位置相对于蛇身位置的判断
ArrayList<body> fuu = new ArrayList<body>();
// fuu = snakeBodies;//直接赋值有严重错误,操作fuu相当于直接操作snakeBodies
for(int i=0;i<snakeBodies.size();i++){
body puk = new body(snakeBodies.get(i).ax, snakeBodies.get(i).ay);
fuu.add(puk);
}
fuu.remove(0);//获得除蛇头以外的躯干坐标
/* for(int i=0;i<fuu.size();i++){
if(fuu.get(i).equals(oop)){
return true;
}
}*/
/* if(fuu.contains(oop)){
return true;
}
return false;
*/
// return fuu.contains(oop);
int nx = snakeBodies.get(0).ax;
int ny = snakeBodies.get(0).ay;
for(int i=0;i<fuu.size();i++){
if(fuu.get(i).ax==nx&&fuu.get(i).ay==ny){
return true;
}
}
return false;
}
public boolean eatcheck(){//吃到星星检测
/* if(snakeBodies.get(0).equals(star.get(0))){
star.remove(0);
int [][] aa = randompoint();
body ppo = new body(aa[0][0], aa[0][1]);
star.add(ppo);
Score++;
Log.i("EEE","吃到星星"+1);
return true;
}else if (snakeBodies.get(0).equals(star.get(1))) {
star.remove(1);
int [][] aa = randompoint();
body ppo = new body(aa[0][0], aa[0][1]);
star.add(ppo);
Score++;
Log.i("EEE","吃到星星"+2);
return true;
}else {
Log.i("EEE","没有吃到星星");
return false;
}*/
/* if(star.contains(snakeBodies.get(0))){
int amp = star.indexOf(snakeBodies.get(0));//找出与蛇头位置一样的星星序号
Log.i("NUM", "星星序号"+amp);
Log.i("STAR", "星星坐标"+star.get(amp).ax+" "+star.get(amp).ay);
star.remove(amp);
int [][] aa = randompoint();
body ppo = new body(aa[0][0], aa[0][1]);
star.add(ppo);
Score++;
Log.i("EEE","吃到星星"+amp);
return true;
}else {
Log.i("EEE","没有吃到星星");
return false;
}*/
/* for(int i=0;i<star.size();i++){
if(snakeBodies.get(0).equals(star.get(i))){
Log.i("NUM", "星星序号"+i);
Log.i("STAR", "星星坐标"+star.get(i).ax+" "+star.get(i).ay);
star.remove(i);
int [][] aa = randompoint();
body ppo = new body(aa[0][0], aa[0][1]);
star.add(ppo);
Score++;
Log.i("EEE","吃到星星"+i);
return true;
}
}
return false;*/
int nx = snakeBodies.get(0).ax;
int ny = snakeBodies.get(0).ay;
for(int i=0;i<star.size();i++){
if(star.get(i).ax==nx&&star.get(i).ay==ny){
Log.i("NUM", "星星序号"+i);
Log.i("STAR", "星星坐标"+star.get(i).ax+" "+star.get(i).ay);
star.remove(i);
int [][] aa = randompoint();
body ppo = new body(aa[0][0], aa[0][1]);
star.add(ppo);
Score++;
Log.i("EEE","吃到星星"+i);
StayTime *= 0.9;
return true;
}
}
return false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("BBB","初始方向"+DirectionMove);//初始方向
if(DirectionMove == UP||DirectionMove == DOWN){
if(event.getX()>snakeBodies.get(0).ax*WW){
NextDirectionMove = RIGHT;
}else {
NextDirectionMove = LEFT;
}
}else if(DirectionMove == RIGHT||DirectionMove == LEFT){
if (event.getY()>snakeBodies.get(0).ay*HH) {
NextDirectionMove = DOWN;
}else {
NextDirectionMove = UP;
}
}
Log.i("CCC","变更方向"+NextDirectionMove);//变更方向
return false;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
new Thread(new moveThread()).start();//启动线程
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
地址:http://pan.baidu.com/share/link?shareid=2604684104&uk=1259703658