- DesktopView.java是地图文件,用来画出桌面的文件
- Food.java食物类文件
- Rock.java石头类文件
- Snake.java蛇类文件
- SnakeActivity.java主要Activity
- SnakeView.java画出蛇的文件
代码如下
package mars.com;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;
public class SnakeActivity extends Activity {
public static final int CELL = 20;
public static WindowManager windowManager;
public static int SCREEN_WIDTH;
public static int SCREEN_HEIGHT;
public static int NUMBER;
SnakeView snakeView;
Snake snake;
Food food;
Rock rock;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.initVars();
snake = new Snake();
food = new Food();
rock = new Rock();
snakeView = new SnakeView(getApplicationContext(), snake, food, rock);
snakeView.setFocusable(true);
setContentView(snakeView);
snake.start();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
finish();
System.exit(0);
break;
}
return super.onKeyDown(keyCode, event);
}
private void initVars() {
windowManager = SnakeActivity.this.getWindowManager();
SCREEN_HEIGHT = windowManager.getDefaultDisplay().getHeight();
SCREEN_WIDTH = windowManager.getDefaultDisplay().getWidth();
NUMBER = SCREEN_WIDTH / CELL;
}
}
Snake类文件
package mars.com;
import java.util.ArrayList;
import java.util.LinkedList;
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.graphics.Point;
public class Snake {
public static final int UP = 1;
public static final int DOWN = -1;
public static final int LEFT = -2;
public static final int RIGHT = 2;
public static int oldDirection = RIGHT;
public static int newDirection = RIGHT;
public static LinkedList snakeBody = new LinkedList();
private Point snakeTail;
private boolean LIFE = true;
public Snake() {
int x = SnakeActivity.NUMBER / 2;
int y = SnakeActivity.NUMBER / 2;
for (int i = 0; i < 3; i++) {
snakeBody.add(new Point(x--, y));
}
}
public int getSpeed() {
return -20 * snakeBody.size();
}
public void eatRock(Rock rock) {
ArrayList rocks = rock.getRocks();
for (int i = 0; i < 3; i++) {
if (snakeBody.getFirst().equals(rocks.get(i))) {
LIFE = false;
break;
}
}
}
public void eatWall() {
if (snakeBody.getFirst().x < 0 || snakeBody.getFirst().y < 0
|| snakeBody.getFirst().x == SnakeActivity.NUMBER
|| snakeBody.getFirst().y == SnakeActivity.NUMBER) {
LIFE = false;
}
}
public void eatFood(Food food, Rock rock) {
if (snakeBody.getFirst().equals(food.eatFood())) {
snakeBody.addLast(snakeTail);
food.createFood();
rock.createRocks();
}
}
public void eatHimself() {
for (int i = 1; i < snakeBody.size(); i++) {
if (snakeBody.get(i).equals(snakeBody.getFirst())) {
LIFE = false;
break;
}
}
}
public void move() {
snakeTail = snakeBody.getLast();
Point headPoint = snakeBody.getFirst();
Point point = new Point();
if (oldDirection == newDirection || oldDirection + newDirection == 0) {
if (oldDirection == UP) {
point.set(headPoint.x, headPoint.y - 1);
} else if (oldDirection == DOWN) {
point.set(headPoint.x, headPoint.y + 1);
} else if (oldDirection == LEFT) {
point.set(headPoint.x - 1, headPoint.y);
} else if (oldDirection == RIGHT) {
point.set(headPoint.x + 1, headPoint.y);
}
snakeBody.addFirst(point);
snakeBody.removeLast();
return;
}
switch (newDirection) {
case UP:
point.set(headPoint.x, headPoint.y - 1);
break;
case DOWN:
point.set(headPoint.x, headPoint.y + 1);
break;
case LEFT:
point.set(headPoint.x - 1, headPoint.y);
break;
case RIGHT:
point.set(headPoint.x + 1, headPoint.y);
break;
}
oldDirection = newDirection;
snakeBody.addFirst(point);
snakeBody.removeLast();
}
class Tsnake implements Runnable {
public void run() {
while (LIFE) {
move();
try {
Thread.sleep(500 + getSpeed());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void start() {
new Thread(new Tsnake()).start();
}
public void display(Canvas canvas, Context context) {
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Style.FILL);
for (int i = 0; i < snakeBody.size(); i++) {
int x = snakeBody.get(i).x * SnakeActivity.CELL;
int y = snakeBody.get(i).y * SnakeActivity.CELL;
canvas.drawRect(x, y, x + SnakeActivity.CELL, y
+ SnakeActivity.CELL, paint);
}
paint.setColor(Color.RED);
if (LIFE == false) {
Bitmap image = BitmapFactory.decodeStream(context.getResources()
.openRawResource(R.drawable.fail));
canvas.drawBitmap(image, 200, 200, paint);
}
}
}
SnakeView类文件
package mars.com;
import android.content.Context;
import android.graphics.Canvas;
public class SnakeView extends DesktopView {
private Snake snake;
private Food food;
private Rock rock;
private Context context;
public SnakeView(Context context, Snake snake, Food food, Rock rock) {
super(context);
this.snake = snake;
this.food = food;
this.rock = rock;
this.context = context;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
snake.display(canvas, context);
food.display(canvas);
rock.display(canvas);
snake.eatFood(food, rock);
snake.eatRock(rock);
snake.eatHimself();
snake.eatWall();
}
}
石头类文件
package mars.com;
import java.util.ArrayList;
import java.util.Random;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
public class Rock {
public static ArrayList rocks = new ArrayList();
public int OK = 0;
private Point p;
public Rock() {
init();
}
public void init() {
rocks = new ArrayList();
for (int i = 0; i < 3; i++) {
p = new Point();
p.x = new Random().nextInt(SnakeActivity.NUMBER);
p.y = new Random().nextInt(SnakeActivity.NUMBER);
if (rocks.contains(p) == true
|| Snake.snakeBody.contains(p) == true) {
i--;
} else {
rocks.add(p);
}
}
}
public void createRocks() {
init();
}
public ArrayList getRocks() {
return rocks;
}
public void display(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Style.FILL);
for (int i = 0; i < 3; i++) {
int x = rocks.get(i).x * SnakeActivity.CELL;
int y = rocks.get(i).y * SnakeActivity.CELL;
canvas.drawRect(x, y, x + SnakeActivity.CELL, y
+ SnakeActivity.CELL, paint);
}
}
}
Food类文件
package mars.com;
import java.util.Random;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
public class Food {
private Point p = new Point();
public Food() {
init();
}
public Point eatFood() {
return p;
}
public void init() {
while (true) {
p.x = new Random().nextInt(SnakeActivity.NUMBER);
p.y = new Random().nextInt(SnakeActivity.NUMBER);
if (Snake.snakeBody.contains(p) == false) {
break;
}
}
}
public void createFood() {
init();
}
public void display(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Style.FILL);
canvas.drawRect(p.x * SnakeActivity.CELL, p.y * SnakeActivity.CELL,
(p.x + 1) * SnakeActivity.CELL, (p.y + 1) * SnakeActivity.CELL,
paint);
}
}
桌面类文件
package mars.com;
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.view.MotionEvent;
import android.view.View;
public class DesktopView extends View {
private int width;
private int height;
public DesktopView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
canvas.setViewport(SnakeActivity.SCREEN_WIDTH,
SnakeActivity.SCREEN_WIDTH);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
for (int i = 0; i <= SnakeActivity.NUMBER; i++)
canvas.drawLine(i * SnakeActivity.CELL, 0, i * SnakeActivity.CELL,
SnakeActivity.CELL * SnakeActivity.NUMBER, paint);
for (int i = 0; i <= SnakeActivity.NUMBER; i++)
canvas.drawLine(0, i * SnakeActivity.CELL, SnakeActivity.CELL
* SnakeActivity.NUMBER, i * SnakeActivity.CELL, paint);
paint.setColor(Color.GRAY);
int x = 0;
int y = SnakeActivity.SCREEN_WIDTH;
width = SnakeActivity.SCREEN_WIDTH / 7;
height = (SnakeActivity.SCREEN_HEIGHT - SnakeActivity.SCREEN_WIDTH) / 5;
Bitmap image = BitmapFactory.decodeStream(getResources()
.openRawResource(R.drawable.left));
canvas.drawBitmap(image, x + width * 3, y + height * 2, paint);
image = BitmapFactory.decodeStream(getResources().openRawResource(
R.drawable.right));
canvas.drawBitmap(image, x + width * 5, y + height * 2, paint);
image = BitmapFactory.decodeStream(getResources().openRawResource(
R.drawable.up));
canvas.drawBitmap(image, x + width * 4, y + height * 1, paint);
image = BitmapFactory.decodeStream(getResources().openRawResource(
R.drawable.down));
canvas.drawBitmap(image, x + width * 4, y + height * 3, paint);
postInvalidate();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
Bitmap image = BitmapFactory.decodeStream(getResources()
.openRawResource(R.drawable.left));
int imageX = image.getWidth();
int imageY = image.getHeight();
if (x > 4 * width && x < 4 * width + imageX
&& y > height + SnakeActivity.SCREEN_WIDTH
&& y < imageY + height + SnakeActivity.SCREEN_WIDTH) {
System.out.println("***上");
Snake.newDirection = Snake.UP;
}
if (x > 4 * width && x < 4 * width + imageX
&& y > 3 * height + SnakeActivity.SCREEN_WIDTH
&& y < imageY + 3 * height + SnakeActivity.SCREEN_WIDTH) {
System.out.println("***下");
Snake.newDirection = Snake.DOWN;
}
if (x > 3 * width && x < 3 * width + imageX
&& y > 2 * height + SnakeActivity.SCREEN_WIDTH
&& y < imageY + 2 * height + SnakeActivity.SCREEN_WIDTH) {
System.out.println("***左");
Snake.newDirection = Snake.LEFT;
}
if (x > 5 * width && x < 5 * width + imageX
&& y > 2 * height + SnakeActivity.SCREEN_WIDTH
&& y < imageY + 2 * height + SnakeActivity.SCREEN_WIDTH) {
System.out.println("***右");
Snake.newDirection = Snake.RIGHT;
}
return super.onTouchEvent(event);
}
}
项目问题:Snake主Activity启动加载layout布局,然后对SnakeView进行初始化,项目启动,主界面显示 “press up to start game”,这个时候我如何操作都不能启动项目,因为这个游戏试运行在带手柄的游戏设备上的,手机是无法启动它的,使用模拟器可以进行适当的设置调出前后左右操作键。后期会修改这个项目使其能够通过滑动去操作游戏。