android中SurfaceView的应用,一个贪吃蛇的小程序

开始时

这里写图片描述

吃到东西后

这里写图片描述

说明:1.一直都只有一个东西,吃到东西就增加一格。
2.吃到自己,撞到墙,游戏就结束了。
3.不能直线后退,吃到东西一次加100分,东西被吃掉后会随机增加一个东西。

MainActivity 类

public class MainActivity extends AppCompatActivity {

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

SnakeSurfaceView 类

public class SnakeSurfaceView extends SurfaceView implements Runnable, SurfaceHolder.Callback {

    Paint paint = new Paint();
    Snake snake;
    //食物
    Point food;

    int score = 0;


    int x, y;

    public SnakeSurfaceView(Context context) {
        super(context);
        getHolder().addCallback(this);
    }

    public SnakeSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        getHolder().addCallback(this);
    }


    //19 15

    public void myDraw() {
        Canvas canvas = getHolder().lockCanvas();
        //绘制背景
        canvas.drawColor(Color.WHITE);
        //绘制线条
        paint.setColor(Color.GRAY);
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < 15; j++) {
                //横线
                canvas.drawLine(0, i * x, getWidth(), i * x, paint);
                canvas.drawLine(j * x, 0, j * x, x * 19, paint);
            }
        }
        //绘制食物
        paint.setColor(Color.RED);
        canvas.drawRect(food.x * x + 5, food.y * x + 5, (food.x + 1) * x - 5, (food.y + 1) * x - 5, paint);
        snake.myDraw(canvas);
        paint.setTextAlign(Paint.Align.LEFT);
        paint.setTextSize(24);
        canvas.drawText("分数:" + score, 0, getHeight() - 50, paint);
        getHolder().unlockCanvasAndPost(canvas);
    }


    @Override
    public void run() {
        //不断循环  贴图
        //需要动
        while (!snake.isOver()) {
            myDraw();
            if (snake.eat(food)) {
                createFood();
                //给一个分数
                score += 100;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //结束
        over();
    }

    private void over() {
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(48);
        Canvas canvas = getHolder().lockCanvas();
        canvas.drawText("游戏结束", getWidth() / 2, getHeight() / 2, paint);
        getHolder().unlockCanvasAndPost(canvas);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setColor(Color.GRAY);
        x = getWidth() / 15;
        y = getHeight() / 20;
        snake = new Snake(x, 15, 19);
        //创建第一个食物
        createFood();
        //自己开始跑
        new Thread(this).start();
        snake.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }


    float oldx;
    float oldy;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //手势
        //左滑,右滑,
        //上下
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                oldx = x;
                oldy = y;
                break;
            case MotionEvent.ACTION_MOVE:
                //判断 到底是上下还是左右
                if (Math.abs(oldx - x) >= Math.abs(oldy - y)) {// zuoyou
                    //25个像素
                    if (oldx - x > 10) //←
                    {
                        snake.setDriection(Snake.LEFT);
                    } else if (oldx - x < -10) {
                        snake.setDriection(Snake.RIGHT);
                    }
                } else {
                    //上下
                    if (oldy - y > 10) {
                        snake.setDriection(Snake.UP);

                    } else if (oldy - y < -10) {
                        snake.setDriection(Snake.DOWN);
                    }
                }
                oldx = x;
                oldy = y;
                break;
            case MotionEvent.ACTION_UP:
                if (snake.isOver()) {
                    //重新开始
//                    new Thread(this).start();
                }
                break;
        }
        return true;
    }


    //初始化食物
    public void createFood() {
        //食物不能刷在蛇身上
        Point point = new Point();
        do {
            point.x = (int) (Math.random() * 15);
            point.y = (int) (Math.random() * 19);
        } while (snake.isConnPoint(point));
        food = point;
    }
}

Snake 类

public class Snake extends Thread {

    //10个等级
    int guanqia = 9;

    public static final int LEFT = 11;
    public static final int RIGHT = 12;
    public static final int UP = 23;
    public static final int DOWN = 24;

    public int direction = UP;


    @Override
    public void run() {
        super.run();
        while (!isOver()) {
            try {
                Thread.sleep(1050 - guanqia * 100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            move();

        }
    }

    public boolean isOver() {
        Point point = mList.get(0);
        if (point.x < 0 || point.x == x || point.y < 0 || point.y == y)
            return true;
        //撞到自己
        for (int i = 1; i < mList.size(); i++) {
            if (point.x == mList.get(i).x && point.y == mList.get(i).y)
                return true;
        }
        return false;
    }

    private final int y;
    private final int x;
    private int width;

    Paint paint = new Paint();

    //知道每个蛇的大小
    public Snake(int width, int x, int y) {
        this.width = width;
        this.x = x;
        this.y = y;
        paint.setColor(Color.GREEN);

        //3个点
        mList.add(new Point(x / 2, y / 2));
        mList.add(new Point(x / 2, y / 2 + 1));
        mList.add(new Point(x / 2, y / 2 + 2));
    }

    //蛇

    List<Point> mList = new ArrayList<>();


    //能绘制自己
    public void myDraw(Canvas canvas) {
        for (Point point : mList) {
            canvas.drawRect(point.x * width, point.y * width, (point.x + 1) * width, (point.y + 1) * width, paint);
        }
    }


    //移动
    public void move() {
        if (tempDriection / 10 != direction / 10)
            direction = tempDriection;
        Point p = mList.get(mList.size() - 1);
        Point point = new Point(p.x, p.y);
        switch (direction) {
            case LEFT:
                point.x = mList.get(0).x - 1;
                point.y = mList.get(0).y;
                break;
            case RIGHT:
                point.x = mList.get(0).x + 1;
                point.y = mList.get(0).y;
                break;
            case UP:
                point.x = mList.get(0).x;
                point.y = mList.get(0).y - 1;
                break;
            case DOWN:
                point.x = mList.get(0).x;
                point.y = mList.get(0).y + 1;
                break;
        }

        if (food == null)
            mList.remove(mList.get(mList.size() - 1));
        else
            food = null;
        mList.add(0, point);
    }

    //缓冲
    public void setDriection(int driection) {
        this.tempDriection = driection;
    }

    int tempDriection = direction;


    //判断是否在蛇身上
    public boolean isConnPoint(Point point) {
        for (Point point1 : mList) {
            if (point.equals(point1.x, point1.y))
                return true;
        }
        return false;
    }

    public boolean eat(Point point) {
        Point head = mList.get(0);
        if (head.equals(point.x, point.y)) {
            food = point;
            return true;
        }
        return false;
    }

    Point food = null;

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值