用Android studio编写一个贪吃蛇游戏
时间: 2024-04-21 12:08:03 浏览: 124
好的,下面是一个简单的贪吃蛇游戏的 Android Studio 实现:
1. 首先,我们需要创建一个新的 Android Studio 项目。在创建新项目时,请确保选择空活动(Empty Activity)模板。
2. 在 MainActivity.java 文件中,添加以下代码:
```
package com.example.snakegame;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置窗口为全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 隐藏标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new GameView(this));
}
}
```
3. 创建一个新的类 GameView.java,在其中添加以下代码:
```
package com.example.snakegame;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class GameView extends SurfaceView implements Runnable {
private Thread thread;
private boolean isPlaying;
private Canvas canvas;
private SurfaceHolder surfaceHolder;
private Paint paint;
private int screenWidth, screenHeight;
private List<Point> snakePoints;
private Point foodPoint;
private int direction = 0;
private int blockSize = 50;
private int score = 0;
private Random random;
public GameView(Context context) {
super(context);
surfaceHolder = getHolder();
paint = new Paint();
snakePoints = new ArrayList<>();
random = new Random();
initGame();
}
private void initGame() {
// 获取屏幕宽度和高度
screenWidth = getResources().getDisplayMetrics().widthPixels;
screenHeight = getResources().getDisplayMetrics().heightPixels;
// 初始化蛇的位置
for (int i = 0; i < 5; i++) {
snakePoints.add(new Point(screenWidth / 2 - i * blockSize, screenHeight / 2));
}
// 初始化食物的位置
int foodX = random.nextInt(screenWidth - 2 * blockSize) + blockSize;
int foodY = random.nextInt(screenHeight - 2 * blockSize) + blockSize;
foodPoint = new Point(foodX, foodY);
}
@Override
public void run() {
while (isPlaying) {
update();
draw();
sleep();
}
}
private void update() {
// 更新蛇的位置
Point head = snakePoints.get(0);
switch (direction) {
case 0: // 向上移动
snakePoints.add(0, new Point(head.x, head.y - blockSize));
break;
case 1: // 向右移动
snakePoints.add(0, new Point(head.x + blockSize, head.y));
break;
case 2: // 向下移动
snakePoints.add(0, new Point(head.x, head.y + blockSize));
break;
case 3: // 向左移动
snakePoints.add(0, new Point(head.x - blockSize, head.y));
break;
}
// 判断蛇是否吃到食物
if (snakePoints.get(0).equals(foodPoint)) {
// 重新生成食物的位置
int foodX = random.nextInt(screenWidth - 2 * blockSize) + blockSize;
int foodY = random.nextInt(screenHeight - 2 * blockSize) + blockSize;
foodPoint = new Point(foodX, foodY);
score += 10;
} else {
snakePoints.remove(snakePoints.size() - 1);
}
// 判断蛇是否撞到墙或自己
Point head2 = snakePoints.get(0);
if (head2.x < 0 || head2.x > screenWidth - blockSize || head2.y < 0 || head2.y > screenHeight - blockSize) {
isPlaying = false; // 游戏结束
}
for (int i = 1; i < snakePoints.size(); i++) {
if (head2.equals(snakePoints.get(i))) {
isPlaying = false; // 游戏结束
break;
}
}
}
private void draw() {
if (surfaceHolder.getSurface().isValid()) {
canvas = surfaceHolder.lockCanvas();
canvas.drawColor(Color.BLACK);
paint.setColor(Color.WHITE);
paint.setTextSize(50);
canvas.drawText("Score: " + score, 50, 50, paint);
// 绘制蛇
paint.setColor(Color.GREEN);
for (Point point : snakePoints) {
canvas.drawRect(point.x, point.y, point.x + blockSize, point.y + blockSize, paint);
}
// 绘制食物
paint.setColor(Color.RED);
canvas.drawRect(foodPoint.x, foodPoint.y, foodPoint.x + blockSize, foodPoint.y + blockSize, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
private void sleep() {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void pause() {
isPlaying = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void resume() {
isPlaying = true;
thread = new Thread(this);
thread.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (event.getX() < screenWidth / 2 && direction != 1) {
direction = 3; // 左
} else if (event.getX() >= screenWidth / 2 && direction != 3) {
direction = 1; // 右
}
break;
case MotionEvent.ACTION_UP:
if (event.getY() < screenHeight / 2 && direction != 2) {
direction = 0; // 上
} else if (event.getY() >= screenHeight / 2 && direction != 0) {
direction = 2; // 下
}
break;
}
return true;
}
}
```
4. 修改 AndroidManifest.xml 文件,添加以下权限:
```
<uses-permission android:name="android.permission.WAKE_LOCK" />
```
5. 运行应用程序,您应该能够看到一个简单的贪吃蛇游戏。您可以使用手指滑动来控制蛇的移动方向。
希望这个简单的贪吃蛇游戏能够帮助您入门 Android Studio 开发。
阅读全文