用Android studio写一个贪吃蛇游戏代码
时间: 2023-10-05 15:07:36 浏览: 129
贪吃蛇android代码
很好,下面是一个简单的贪吃蛇游戏的 Android Studio 代码示例:
```java
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.util.ArrayList;
import java.util.Random;
public class SnakeGameView extends SurfaceView implements SurfaceHolder.Callback {
private static final int MAX_SNAKE_LENGTH = 200;
private static final int SNAKE_BLOCK_SIZE = 20;
private static final int GAME_REFRESH_RATE = 100;
private static final int MAX_GAME_SPEED = 40;
private static final int MIN_GAME_SPEED = 5;
private SurfaceHolder holder;
private Context context;
private SnakeThread snakeThread;
private Paint paint;
private ArrayList<SnakeBlock> snakeBlocks;
private SnakeDirection snakeDirection;
private int gameSpeed;
private boolean gameOver;
private SnakeBlock foodBlock;
public SnakeGameView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
holder = getHolder();
holder.addCallback(this);
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
snakeBlocks = new ArrayList<>();
snakeDirection = SnakeDirection.RIGHT;
gameSpeed = MAX_GAME_SPEED;
gameOver = false;
// Add the initial snake blocks
int x = 10 * SNAKE_BLOCK_SIZE;
int y = 10 * SNAKE_BLOCK_SIZE;
for (int i = 0; i < 5; i++) {
snakeBlocks.add(new SnakeBlock(x + i * SNAKE_BLOCK_SIZE, y));
}
// Generate the first food block
generateFoodBlock();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
snakeThread = new SnakeThread();
snakeThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
snakeThread.stopThread();
}
private void update() {
// Move the snake head in the current direction
int headX = snakeBlocks.get(0).getX();
int headY = snakeBlocks.get(0).getY();
switch (snakeDirection) {
case UP:
headY -= SNAKE_BLOCK_SIZE;
break;
case DOWN:
headY += SNAKE_BLOCK_SIZE;
break;
case LEFT:
headX -= SNAKE_BLOCK_SIZE;
break;
case RIGHT:
headX += SNAKE_BLOCK_SIZE;
break;
}
// Check if the snake has collided with the walls or itself
if (headX < 0 || headX >= getWidth() || headY < 0 || headY >= getHeight()) {
gameOver = true;
return;
}
for (int i = 1; i < snakeBlocks.size(); i++) {
if (snakeBlocks.get(i).getX() == headX && snakeBlocks.get(i).getY() == headY) {
gameOver = true;
return;
}
}
// Check if the snake has eaten the food block
if (headX == foodBlock.getX() && headY == foodBlock.getY()) {
snakeBlocks.add(0, foodBlock);
generateFoodBlock();
// Increase the game speed (up to a maximum)
gameSpeed = Math.max(MIN_GAME_SPEED, gameSpeed - 1);
} else {
// Move the snake blocks forward
for (int i = snakeBlocks.size() - 1; i > 0; i--) {
snakeBlocks.get(i).setX(snakeBlocks.get(i - 1).getX());
snakeBlocks.get(i).setY(snakeBlocks.get(i - 1).getY());
}
}
// Move the snake head
snakeBlocks.get(0).setX(headX);
snakeBlocks.get(0).setY(headY);
}
private void generateFoodBlock() {
Random random = new Random();
int x = random.nextInt(getWidth() / SNAKE_BLOCK_SIZE) * SNAKE_BLOCK_SIZE;
int y = random.nextInt(getHeight() / SNAKE_BLOCK_SIZE) * SNAKE_BLOCK_SIZE;
foodBlock = new SnakeBlock(x, y);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
// Draw the snake blocks
for (SnakeBlock block : snakeBlocks) {
canvas.drawRect(block.getX(), block.getY(),
block.getX() + SNAKE_BLOCK_SIZE, block.getY() + SNAKE_BLOCK_SIZE, paint);
}
// Draw the food block
canvas.drawRect(foodBlock.getX(), foodBlock.getY(),
foodBlock.getX() + SNAKE_BLOCK_SIZE, foodBlock.getY() + SNAKE_BLOCK_SIZE, paint);
}
public void setSnakeDirection(SnakeDirection direction) {
if (!gameOver && direction != snakeDirection.opposite()) {
snakeDirection = direction;
}
}
private class SnakeThread extends Thread {
private boolean running;
public SnakeThread() {
running = true;
}
public void stopThread() {
running = false;
}
@Override
public void run() {
while (running) {
long startTime = System.currentTimeMillis();
update();
Canvas canvas = holder.lockCanvas();
if (canvas != null) {
draw(canvas);
holder.unlockCanvasAndPost(canvas);
}
long timeElapsed = System.currentTimeMillis() - startTime;
try {
Thread.sleep(Math.max(0, GAME_REFRESH_RATE - gameSpeed * timeElapsed));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
enum SnakeDirection {
UP, DOWN, LEFT, RIGHT;
public SnakeDirection opposite() {
switch (this) {
case UP:
return DOWN;
case DOWN:
return UP;
case LEFT:
return RIGHT;
case RIGHT:
return LEFT;
}
return null;
}
}
class SnakeBlock {
private int x;
private int y;
public SnakeBlock(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
```
这个示例代码实现了一个简单的贪吃蛇游戏,可以在 Android Studio 中创建一个新的项目,将上述代码复制到 `MainActivity.java` 文件中,并在 `activity_main.xml` 文件中添加一个 `SnakeGameView` 的自定义视图即可开始游戏。
阅读全文