• 智能AI问答!更聪明、更完整、原创作!
  • 一款基于GPT的超级AI机器人,可以回答问题、聊天、创作、写文案!

请帮我写一个安卓系统可以运行的贪吃蛇的

发布于 2023-09-01 19:25:44

当然可以帮你写一个安卓系统可以运行的贪吃蛇游戏。下面是一个简单的示例代码,你可以根据自己的需求进行修改和扩展:

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class SnakeGame extends Activity {
    private GameView gameView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gameView = new GameView(this);
        setContentView(gameView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        gameView.resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        gameView.pause();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return false;
    }

    class GameView extends SurfaceView implements Runnable {
        private Thread gameThread = null;
        private SurfaceHolder surfaceHolder;
        private volatile boolean playing;
        private Canvas canvas;
        private Paint paint;
        private float x, y;
        private int direction;

        public GameView(Context context) {
            super(context);
            surfaceHolder = getHolder();
            paint = new Paint();
            x = 100; // 蛇头初始位置 x 坐标
            y = 100; // 蛇头初始位置 y 坐标
            direction = 0; // 初始方向,0代表向右移动
        }

        @Override
        public void run() {
            while (playing) {
                update();
                draw();
                control();
            }
        }

        private void update() {
            // 根据当前方向更新蛇头的坐标
            if (direction == 0) {
                x += 10;
            } else if (direction == 1) {
                y += 10;
            } else if (direction == 2) {
                x -= 10;
            } else if (direction == 3) {
                y -= 10;
            }
        }

        private void draw() {
            if (surfaceHolder.getSurface().isValid()) {
                canvas = surfaceHolder.lockCanvas();
                canvas.drawColor(Color.BLACK);
                paint.setColor(Color.WHITE);
                canvas.drawRect(x, y, x+10, y+10, paint); // 绘制蛇头
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }

        private void control() {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public void resume() {
            playing = true;
            gameThread = new Thread(this);
            gameThread.start();
        }

        public void pause() {
            playing = false;
            try {
                gameThread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

以上是一个简单的贪吃蛇游戏的实现代码,你可以将其保存为一个名为"SnakeGame.java"的文件,并在你的安卓项目中引用这个文件。如果需要更复杂的功能,你可以根据自己的需求进行扩展和优化。祝你编写成功!

0 条评论

发布
问题

在线
客服