如何在android studio中用java在main activity写出贪吃蛇代码
时间: 2024-03-06 22:49:38 浏览: 67
好的,以下是一个简单的贪吃蛇游戏代码示例:
1. 首先,我们需要在 Android Studio 中创建一个新的项目并创建一个 `SnakeGame` 类。
```java
public class SnakeGame extends AppCompatActivity {
private SnakeView snakeView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
snakeView = new SnakeView(this);
setContentView(snakeView);
}
@Override
protected void onPause() {
super.onPause();
snakeView.pause();
}
@Override
protected void onResume() {
super.onResume();
snakeView.resume();
}
}
```
2. 接下来,我们需要创建一个名为 `SnakeView` 的自定义视图类,并实现游戏逻辑。
```java
public class SnakeView extends SurfaceView implements Runnable {
private Thread gameThread = null;
private volatile boolean playing;
private Canvas canvas;
private SurfaceHolder surfaceHolder;
private Paint paint;
private Snake snake;
private Food food;
public SnakeView(Context context) {
super(context);
surfaceHolder = getHolder();
paint = new Paint();
snake = new Snake();
food = new Food();
playing = true;
}
@Override
public void run() {
while (playing) {
update();
draw();
control();
}
}
private void update() {
snake.update();
if (snake.checkCollision(food)) {
snake.grow();
food.spawn();
}
}
private void draw() {
if (surfaceHolder.getSurface().isValid()) {
canvas = surfaceHolder.lockCanvas();
canvas.drawColor(Color.BLACK);
paint.setColor(Color.WHITE);
paint.setTextSize(30);
canvas.drawText("Score: " + snake.getLength(), 10, 40, paint);
snake.draw(canvas, paint);
food.draw(canvas, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
private void control() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void pause() {
playing = false;
try {
gameThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void resume() {
playing = true;
gameThread = new Thread(this);
gameThread.start();
}
}
```
3. 最后,我们需要创建 `Snake` 和 `Food` 类来表示贪吃蛇和食物。
```java
public class Snake {
private List<Point> body;
private int direction;
public Snake() {
body = new ArrayList<>();
body.add(new Point(5, 5));
body.add(new Point(6, 5));
body.add(new Point(7, 5));
direction = SnakeView.RIGHT;
}
public void update() {
for (int i = body.size() - 1; i > 0; i--) {
body.get(i).x = body.get(i - 1).x;
body.get(i).y = body.get(i - 1).y;
}
switch (direction) {
case SnakeView.UP:
body.get(0).y--;
break;
case SnakeView.DOWN:
body.get(0).y++;
break;
case SnakeView.LEFT:
body.get(0).x--;
break;
case SnakeView.RIGHT:
body.get(0).x++;
break;
}
}
public boolean checkCollision(Food food) {
if (body.get(0).equals(food.getPosition())) {
return true;
}
for (int i = 1; i < body.size(); i++) {
if (body.get(i).equals(body.get(0))) {
return true;
}
}
return false;
}
public void grow() {
body.add(new Point(body.get(body.size() - 1)));
}
public void draw(Canvas canvas, Paint paint) {
paint.setColor(Color.WHITE);
for (Point point : body) {
canvas.drawRect(point.x * SnakeView.BLOCK_SIZE, point.y * SnakeView.BLOCK_SIZE,
(point.x + 1) * SnakeView.BLOCK_SIZE, (point.y + 1) * SnakeView.BLOCK_SIZE, paint);
}
}
public int getLength() {
return body.size() - 3;
}
public void setDirection(int direction) {
this.direction = direction;
}
}
public class Food {
private Point position;
public Food() {
position = new Point();
spawn();
}
public void spawn() {
position.x = (int) (Math.random() * SnakeView.NUM_BLOCKS_WIDE);
position.y = (int) (Math.random() * SnakeView.NUM_BLOCKS_HIGH);
}
public Point getPosition() {
return position;
}
public void draw(Canvas canvas, Paint paint) {
paint.setColor(Color.RED);
canvas.drawRect(position.x * SnakeView.BLOCK_SIZE, position.y * SnakeView.BLOCK_SIZE,
(position.x + 1) * SnakeView.BLOCK_SIZE, (position.y + 1) * SnakeView.BLOCK_SIZE, paint);
}
}
```
完成后,我们只需在 `AndroidManifest.xml` 中将 `SnakeGame` 设置为启动活动即可。运行应用程序后,您应该能够玩贪吃蛇游戏。
阅读全文