您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > 贪吃蛇游戏安卓源代码
附录1.SnakeView类packagecom.example.android_snake.view;importjava.util.ArrayList;importjava.util.List;importjava.util.Random;importjava.util.Timer;importjava.util.TimerTask;importcom.example.android_snake.R;importcom.example.android_snake.food.Food;importcom.example.android_snake.snake.Body;importcom.example.android_snake.snake.Head;importcom.example.android_snake.snake.Snake;importcom.example.android_snake.snake.SnakeDirection;importcom.example.android_snake.stone.Stone;importandroid.annotation.SuppressLint;importandroid.content.Context;importandroid.graphics.Bitmap;importandroid.graphics.BitmapFactory;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;importandroid.graphics.Paint.Style;importandroid.os.Handler;importandroid.util.DisplayMetrics;importandroid.view.Display;importandroid.view.MotionEvent;importandroid.view.View;importandroid.view.ViewManager;importandroid.view.WindowManager;importandroid.widget.Toast;publicclassSnakeViewextendsView{privateContextcontext;privateBitmapheadBitmap;privateBitmapbodyBitmap;privateBitmapfoodBitmap;privateBitmapstoneBitmap;//屏幕的高度和宽度privateintscreenHeight;privateintscreenWidth;//每个小格子的高度和宽度privateinteachHeight;privateinteachWidth;privateSnakesnake;privateFoodfood;privateStonestone;privateint[]listx;privateint[]listy;privateTimertimer=newTimer();Handlerhandler=newHandler(){publicvoidhandleMessage(android.os.Messagemsg){moveSnake();invalidate();}};publicSnakeView(Contextcontext){super(context);this.context=context;listx=newint[100];listy=newint[100];//获得屏幕的高和宽DisplayMetricsmetrics=newDisplayMetrics();WindowManagerwindowManager=(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();}@OverrideprotectedvoidonDraw(Canvascanvas){super.onDraw(canvas);Paintpaint=newPaint();//定义画笔paint.setColor(Color.GRAY);//设置画笔颜色paint.setAntiAlias(true);//去除锯齿paint.setStyle(Style.STROKE);//设置空心实心paint.setTextSize(40);drawLines(canvas,paint);drawStone(canvas,paint);if(isCollide()){canvas.drawText(GameOver!,screenWidth/4,screenHeight/3,paint);canvas.drawText(得分,screenWidth/4,screenHeight/2,paint);timer.cancel();}else{this.drawSnake(canvas,paint);}booleanflag=IsRectCollision(snake.getHead().getPointX(),snake.getHead().getPointY(),eachWidth,eachHeight,food.getPointX(),food.getPointY(),eachWidth,eachHeight);if(flag){food=null;snake.getBodyList().add(newBody());this.initFood();this.initstone();}else{this.drawFood(canvas,paint);this.drawStone(canvas,paint);}}//方向控制@OverridepublicbooleanonTouchEvent(MotionEventevent){intx=(int)event.getX();inty=(int)event.getY();SnakeDirectionnowDir=snake.getSnakeDirection();intm=-screenHeight*x+screenHeight*screenWidth-screenWidth*y;intn=screenHeight*x-screenWidth*y;if((m0&&n0)&&(nowDir!=SnakeDirection.DOWN)){snake.setSnakeDirection(SnakeDirection.UP);}elseif((m0&&n0)&&(nowDir!=SnakeDirection.RIGHT)){snake.setSnakeDirection(SnakeDirection.LEFT);}elseif((m0&&n0)&&(nowDir!=SnakeDirection.LEFT)){snake.setSnakeDirection(SnakeDirection.RIGHT);}elseif((m0&&n0)&&(nowDir!=SnakeDirection.UP)){snake.setSnakeDirection(SnakeDirection.DOWN);}returnsuper.onTouchEvent(event);}publicvoidgameRun(){timer.scheduleAtFixedRate(newTimerTask(){publicvoidrun(){handler.obtainMessage().sendToTarget();}},100,400);}/**画网格线*/publicvoiddrawLines(Canvascanvas,Paintpaint){intstartX=0,startY=0;for(inti=0;i100;i++){canvas.drawLine(0,startY,screenWidth,startY,paint);startY=startY+eachHeight;}for(inti=0;i100;i++){canvas.drawLine(startX,0,startX,screenHeight,paint);startX=startX+eachWidth;}canvas.drawLine(0,0,screenWidth,screenHeight,paint);canvas.drawLine(0,screenHeight,screenWidth,0,paint);}//初始化蛇publicvoidinitSnake(){ListBodybodies=newArrayListBody();Headhead=newHead(eachWidth*4,eachHeight*2,headBitmap);Bodybody1=newBody(eachWidth*3,eachHeight*2,bodyBitmap);Bodybody2=newBody(eachWidth*2,eachHeight*2,bodyBitmap);Bodybody3=newBody(eachWidth*1,eachHeight*2,bodyBitmap);Bodybody4=newBody(eachWidth*0,eachHeight*2,bodyBitmap);bodies.add(body1);bodies.add(body2);bodies.add(body3);bodies.add(body4);snake=newSnake(head,bodies,SnakeDirection.RIGHT);}//画蛇publicvoiddrawSnake(Canvascanvas,Paintpaint){canvas.drawBitmap(headBitmap,snake.getHead().getPointX(),snake.getHead().getPointY(),paint);for(inti=0;isnake.getBodyList().size();i++){canvas.drawBitmap(bodyBitmap,snake.getBodyList().get(i).getPointX(),snake.getBodyList().get(i).getPointY(),paint);}}//改变蛇身的位置publicvoidchangSnakePosition(intpointX,intpointY){for(inti=snake.getBodyList().si
本文标题:贪吃蛇游戏安卓源代码
链接地址:https://www.777doc.com/doc-5118356 .html