贪吃蛇游戏安卓源代码

贪吃蛇游戏安卓源代码


2024年2月8日发(作者:)

附 录

iew类

package d_;

import ist;

import ;

import ;

import ;

import ask;

import d_snake.R;

import d_;

import d_;

import d_;

import d_;

import d_irection;

import d_;

import ssLint;

import t;

import ;

import Factory;

import ;

import ;

import ;

import ;

import r;

import yMetrics;

import y;

import Event;

import ;

import nager;

import Manager;

import ;

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(e msg) {

moveSnake();

invalidate();

}

};

public SnakeView(Context context) {

super(context);

t = context;

listx =new int[100];

listy =new int[100];

// 获得屏幕的高和宽

DisplayMetrics metrics = new DisplayMetrics();

WindowManager windowManager = (WindowManager) context

.getSystemService(_SERVICE);

aultDisplay().getMetrics(metrics);

screenHeight = Pixels;

screenWidth = ixels;

eachHeight = screenHeight / 32;

eachWidth = screenWidth / 20;

// 初始化图片

headBitmap = Resource(getResources(),

);

bodyBitmap = Resource(getResources(),

);

foodBitmap = Resource(getResources(),

);

stoneBitmap = Resource(getResources(),

);

ake();

od();

one();

gameRun();

}

@Override

protected void onDraw(Canvas canvas) {

(canvas);

Paint paint = new Paint();// 定义画笔

or();// 设置画笔颜色

iAlias(true);// 去除锯齿

le();// 设置空心实心

tSize(40);

drawLines(canvas, paint);

drawStone(canvas, paint);

if(isCollide()){

xt("Game Over!", screenWidth/4, screenHeight/3, paint);

xt("得分", screenWidth/4, screenHeight/2, paint);

();

}else{

ake(canvas, paint);

}

boolean flag = IsRectCollision(d().getPointX(), snake

.getHead().getPointY(), eachWidth, eachHeight,

ntX(), ntY(), eachWidth, eachHeight);

if (flag) {

food = null;

yList().add(new Body());

od();

one();

} else {

od(canvas, paint);

one(canvas, paint);

}

}

//方向控制

@Override

public boolean onTouchEvent(MotionEvent event) {

int x = (int) ();

int y = (int) ();

SnakeDirection nowDir = keDirection();

int m = -screenHeight * x + screenHeight * screenWidth - screenWidth * y;

int n = screenHeight * x - screenWidth * y;

if ((m > 0 && n > 0) && (nowDir != )) {

keDirection();

} else if ((m > 0 && n < 0) && (nowDir != )) {

keDirection();

} else if ((m < 0 && n > 0) && (nowDir != )) {

keDirection();

} else if ((m < 0 && n < 0) && (nowDir != )) {

keDirection();

}

return hEvent(event);

}

public void gameRun() {

leAtFixedRate(new TimerTask() {

public void run() {

Message().sendToTarget();

}

}, 100, 400);

}

/*

* 画网格线

*/

public void drawLines(Canvas canvas, Paint paint) {

int startX = 0, startY = 0;

for (int i = 0; i < 100; i++) {

ne(0, startY, screenWidth, startY, paint);

startY = startY + eachHeight;

}

for (int i = 0; i < 100; i++) {

ne(startX, 0, startX, screenHeight, paint);

startX = startX + eachWidth;

}

ne(0, 0, screenWidth, screenHeight, paint);

ne(0, screenHeight, screenWidth, 0, paint);

}

// 初始化蛇

public void initSnake() {

List bodies = new ArrayList();

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 = new Body(eachWidth * 1, eachHeight * 2, bodyBitmap);

Body body4 = new Body(eachWidth * 0, eachHeight * 2, bodyBitmap);

(body1);

(body2);

(body3);

(body4);

snake = new Snake(head, bodies, );

}

// 画蛇

public void drawSnake(Canvas canvas, Paint paint) {

tmap(headBitmap, d().getPointX(), snake

.getHead().getPointY(), paint);

for (int i = 0; i < yList().size(); i++) {

tmap(bodyBitmap, yList().get(i)

.getPointX(), yList().get(i).getPointY(), paint);

}

}

// 改变蛇身的位置

public void changSnakePosition(int pointX, int pointY) {

for (int i = yList().size() - 1; i > 0; i--) {

yList().get(i)

.setPointX(yList().get(i - 1).getPointX());

yList().get(i)

.setPointY(yList().get(i - 1).getPointY());

}

yList().get(0).setPointX(d().getPointX());

yList().get(0).setPointY(d().getPointY());

}

// 移动蛇

public void moveSnake() {

int nowPointX = d().getPointX();

int nowPointY = d().getPointY();

if (keDirection() == ) {

changSnakePosition(nowPointX, nowPointY);

if (nowPointX >= screenWidth - eachWidth) {

d().setPointX(0);

} else {

d().setPointX(nowPointX + eachWidth);

}

} else if (keDirection() == ) {

changSnakePosition(nowPointX, nowPointY);

if (nowPointY >= screenHeight - eachHeight) {

d().setPointY(0);

} else {

d().setPointY(nowPointY + eachHeight);

}

} else if (keDirection() == ) {

changSnakePosition(nowPointX, nowPointY);

if (nowPointX <= 0) {

d().setPointX(screenWidth - eachWidth);

} else {

d().setPointX(nowPointX - eachWidth);

}

} else if (keDirection() == ) {

changSnakePosition(nowPointX, nowPointY);

if (nowPointY <= 0) {

d().setPointY(screenHeight - eachHeight);

} else {

d().setPointY(nowPointY - eachHeight);

}

}

}

// 初始化food

public void initFood() {

int x = new Random().nextInt(19);

int y = new Random().nextInt(29);

food = new Food(eachWidth * x, eachHeight * y, foodBitmap);

}

// 在界面上画出Food

public void drawFood(Canvas canvas, Paint paint) {

if (food != null) {

tmap(foodBitmap, ntX(), ntY(),

paint);

}

}

// 初始化stone

public void initstone() {

int x = new Random().nextInt(17);

int y = new Random().nextInt(23);

stone = new Stone(eachWidth * x, eachHeight * y, stoneBitmap);

int i=0,j=0;

listx[i++]=x;

listy[j++]=y;

}

// 在界面上画出Stone

public void drawStone(Canvas canvas, Paint paint) {

if (true) {

tmap(stoneBitmap, ntX(), ntY(),

paint);

for(int k=0;k<100;k++){

//food = new Food(eachWidth * listx[k], eachHeight * listy[k], foodBitmap);

//tmap(stoneBitmap,listx[k], listy[k],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

* 第二个矩形的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 false;

} else if (y2 < y1 && y2 <= y1 - h2) {

return false;

} else {

return true;

}

}

//检测蛇头是否与蛇身碰撞

//检测蛇头与墙的碰撞

//

public boolean isCollide() {

boolean flag = false;

for (int i = 0; i < yList().size(); i++) {

flag = IsRectCollision(d().getPointX(),

d().getPointY(), eachWidth, eachHeight,

yList().get(i).getPointX(),

yList().get(i).getPointY(),

eachWidth,eachHeight);

for(int j=0;j<100;j++){

flag = IsRectCollision(d().getPointX(),

d().getPointY(), eachWidth, eachHeight,

listx[j],listy[j], eachWidth,eachHeight);

if(flag){

break;

}

}

if ((d().getPointX() < 1) ||

(d().getPointY() < 1) ||

(d().getPointX() > screenWidth - 1)||

(d().getPointY() > screenHeight - 1)

){

flag = true;

}

if(flag){

break;

}

}

return flag;

}

}

tivity类

package d_snake;

import d_snake.R;

import d_iew;

//Downloads By

import ;

import ty;

import ;

import em;

import ;

import Manager;

public class MainActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

te(savedInstanceState);

requestWindowFeature(E_NO_TITLE);

//设置全屏

getWindow().setFlags(_FULLSCREEN,

_FULLSCREEN);

SnakeView view = new SnakeView(this);

setContentView(view);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(, menu);

return true;

}

}

package d_;

import ;

public class Food {

private int pointX;

private int pointY;

private Bitmap foodBitmap;

public Food() {

super();

}

public Food(int pointX, int pointY, Bitmap foodBitmap) {

super();

= pointX;

= pointY;

tmap = foodBitmap;

}

public int getPointX() {

return pointX;

}

public void setPointX(int pointX) {

= pointX;

}

public int getPointY() {

return pointY;

}

public void setPointY(int pointY) {

= pointY;

}

public Bitmap getFoodBitmap() {

return foodBitmap;

}

public void setFoodBitmap(Bitmap foodBitmap) {

tmap = foodBitmap;

}

}

package d_;

import ;

public class Body {

private int pointX;

private int pointY;

private Bitmap bodyBitMap;

public Body() {

super();

}

public Body(int pointX, int pointY, Bitmap bodyBitMap) {

super();

= pointX;

= pointY;

tMap = bodyBitMap;

}

public int getPointX() {

return pointX;

}

public void setPointX(int pointX) {

= pointX;

}

public int getPointY() {

return pointY;

}

public void setPointY(int pointY) {

= pointY;

}

public Bitmap getBodyBitMap() {

return bodyBitMap;

}

public void setBodyBitMap(Bitmap bodyBitMap) {

tMap = bodyBitMap;

}

}

package d_;

import ;

public class Head {

private int pointX;

private int pointY;

private Bitmap headBitMap;

public Head() {

super();

}

}

public Head(int pointX, int pointY, Bitmap headBitMap) {

super();

= pointX;

= pointY;

tMap = headBitMap;

}

public int getPointX() {

return pointX;

}

public void setPointX(int pointX) {

= pointX;

}

public int getPointY() {

return pointY;

}

public void setPointY(int pointY) {

= pointY;

}

public Bitmap getHeadBitMap() {

return headBitMap;

}

public void setHeadBitMap(Bitmap headBitMap) {

tMap = headBitMap;

}

package d_;

import ist;

import ;

import ;

public class Snake {

private Head head;

private Body body;

private List bodyList;

private SnakeDirection snakeDirection;

public Snake(){

}

public Snake(Head head,List bodyList,SnakeDirection snakeDirection){

super();

= head;

st = bodyList;

irection = snakeDirection;

}

}

public Head getHead() {

return head;

}

public void setHead(Head head) {

= head;

}

public Body getBody() {

return body;

}

public void setBody(Body body) {

= body;

}

public List getBodyList() {

return bodyList;

}

public void setBodyList(List bodyList) {

st = bodyList;

}

public SnakeDirection getSnakeDirection() {

return snakeDirection;

}

public void setSnakeDirection(SnakeDirection snakeDirection) {

irection = snakeDirection;

}

irection类

package d_;

public enum SnakeDirection {

UP,DOWN,LEFT,RIGHT;

}

package d_;

import ;

public class Stone {

private int pointX;

private int pointY;

private Bitmap stoneBitmap;

public Stone() {

super();

}

public Stone(int pointX, int pointY, Bitmap foodBitmap) {

super();

= pointX;

}

= pointY;

itmap = stoneBitmap;

}

public int getPointX() {

return pointX;

}

public void setPointX(int pointX) {

= pointX;

}

public int getPointY() {

return pointY;

}

public void setPointY(int pointY) {

= pointY;

}

public Bitmap getStoneBitmap() {

return stoneBitmap;

}

public void setFoodBitmap(Bitmap foodBitmap) {

itmap = stoneBitmap;

}


发布者:admin,转转请注明出处:http://www.yc00.com/news/1707394029a1502028.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信