Android貪吃蛇

0x0000 寫在前面

記得第一次做貪吃蛇,是在大一學習C++,在貼吧看到了源碼,心里一動,連抄帶蒙,用VS實現了出來。
晃眼間十年過去了,看到Google官方的demo,一時興起又寫了一遍。當時寫了一周的貪吃蛇,今天只寫了不到兩小時。那時候沒想到自己以后會當程序員吧。

0x0001 功能簡介

以下幾個功能點:

  • 蛇在屏幕移動
  • 蘋果隨機出現
  • 根據Touch位置改變蛇的移動方向
  • 游戲失敗條件

0x0002 程序源碼

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        ScreenUtils.init(getApplicationContext());
        setContentView(R.layout.activity_main);
    }
}

SnakeView.java

public class SnakeView extends View {

    private final int DIRECTION_L = 0x1001;
    private final int DIRECTION_T = 0x0011;
    private final int DIRECTION_R = 0x0110;
    private final int DIRECTION_B = 0x1100;

    private final int CANVAS_REFRESH_INTERVAL = 300;

    private final int APPLE_STROKE_WIDTH = 50;
    private final int SNAKE_STROKE_WIDTH = 50;

    private final int MOVE_DISTANCE = 50;

    private final Paint applePaint = new Paint();
    private final Paint snakePaint = new Paint();

    private final Point apple = new Point();
    private final List<Point> snakeBody = new ArrayList<>();

    private int currDirection = DIRECTION_R;
    private int lastDirection = currDirection;

    private int hCount;
    private int vCount;

    private Random random = new Random();

    private final Runnable refresh = new Runnable() {
        @Override
        public void run() {
            updateSnake();
            SnakeView.this.invalidate();
        }
    };

    public SnakeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SnakeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initBound();
        initPaint();
        initApple();
        initSnake();
    }

    private void reset() {
        initApple();
        initSnake();
        currDirection = DIRECTION_R;
        lastDirection = DIRECTION_R;
    }

    private void initBound() {
        int width = ScreenUtils.getScreenWidth(getContext());
        int height = ScreenUtils.getScreenHeight(getContext());
        hCount = width / MOVE_DISTANCE;
        vCount = height / MOVE_DISTANCE;
    }

    private void initPaint() {
        applePaint.setAntiAlias(true);
        applePaint.setColor(Color.RED);
        applePaint.setStyle(Paint.Style.FILL);
        applePaint.setStrokeWidth(APPLE_STROKE_WIDTH);
        snakePaint.setAntiAlias(true);
        snakePaint.setColor(Color.WHITE);
        snakePaint.setStyle(Paint.Style.FILL);
        snakePaint.setStrokeWidth(SNAKE_STROKE_WIDTH);
    }

    private void initSnake() {
        Point head = new Point(300, 500);
        Point body = new Point(250, 500);
        Point tail = new Point(200, 500);
        snakeBody.clear();
        snakeBody.add(head);
        snakeBody.add(body);
        snakeBody.add(tail);
    }

    private void initApple() {
        apple.set(900, 200);
    }

    private void updateSnake() {
        Point head = snakeBody.get(0);
        Point temp = new Point(head);
        switch (currDirection) {
            case DIRECTION_L:
                temp.x -= MOVE_DISTANCE;
                break;
            case DIRECTION_T:
                temp.y -= MOVE_DISTANCE;
                break;
            case DIRECTION_R:
                temp.x += MOVE_DISTANCE;
                break;
            case DIRECTION_B:
                temp.y += MOVE_DISTANCE;
                break;
            default:
                temp.x += MOVE_DISTANCE;
                break;
        }
        if (snakeBody.contains(temp)) {
            //蛇的頭部撞到了自己,GAME OVER!
            Toast.makeText(getContext(), "GAME OVER!", Toast.LENGTH_LONG).show();
            reset();
            return;
        }
        if (temp.x < MOVE_DISTANCE * 2 || temp.x > MOVE_DISTANCE * hCount
                || temp.y < MOVE_DISTANCE * 2 || temp.y > MOVE_DISTANCE * vCount) {
            //蛇的頭部撞到了墻,GAME OVER!
            Toast.makeText(getContext(), "GAME OVER!", Toast.LENGTH_LONG).show();
            reset();
            return;
        }

        snakeBody.add(0, temp);
        lastDirection = currDirection;

        if (snakeBody.contains(apple)) {
            updateApple();
        } else {
            snakeBody.remove(snakeBody.size() - 1);
        }
    }

    private void updateApple() {
        int x = (2 + random.nextInt(hCount)) * MOVE_DISTANCE;
        int y = (2 + random.nextInt(vCount)) * MOVE_DISTANCE;
        apple.set(x, y);
        if (snakeBody.contains(apple)) {
            updateApple();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (Point point : snakeBody) {
            canvas.drawPoint(point.x, point.y, snakePaint);
        }
        canvas.drawPoint(apple.x, apple.y, applePaint);
        this.postDelayed(refresh, CANVAS_REFRESH_INTERVAL);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() != MotionEvent.ACTION_DOWN) {
            return super.onTouchEvent(event);
        }
        float x = event.getX();
        float y = event.getY();
        int snakeX = snakeBody.get(0).x;
        int snakeY = snakeBody.get(0).y;
        if (Math.abs(x - snakeX) >= Math.abs(y - snakeY)) {
            if (x > snakeX) {
                currDirection = DIRECTION_R;
            } else {
                currDirection = DIRECTION_L;
            }
        } else {
            if (y > snakeY) {
                currDirection = DIRECTION_B;
            } else {
                currDirection = DIRECTION_T;
            }
        }
        if ((currDirection & lastDirection) == 0) {
            currDirection = lastDirection;
        }
        return super.onTouchEvent(event);
    }
}

ScreenUtils.java

public class ScreenUtils {

    private static int sScreenWidth;
    private static int sScreenHeight;

    public static void init(Context context) {
        Resources resources = context.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        sScreenWidth = dm.widthPixels;
        sScreenHeight = dm.heightPixels;
    }

    public static int getScreenWidth(Context context) {
        if (sScreenWidth <= 0) {
            init(context);
        }
        return sScreenWidth;
    }

    public static int getScreenHeight(Context context) {
        if (sScreenHeight <= 0) {
            init(context);
        }
        return sScreenHeight;
    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <custom.widget.SnakeView
        android:id="@+id/snake"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/black" />
</RelativeLayout>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 先上個效果圖吧 改個顏色,瞬間帥氣了有木有。 實現思路: 自定義一個View,在游戲開始的時候,開啟一個定時器,不...
    廢柴大媽閱讀 2,193評論 0 0
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,455評論 25 708
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,250評論 4 61
  • 前言 學習的時候遺漏了這個知識點,最近在做一個類微信、支付寶的 "+"號彈出浮窗的功能,發現了這個好用的東西~ 做...
    DeadRabbit閱讀 7,734評論 4 6
  • 清晨, 一條街道 兩排行道樹 三四個小販 五六聲吆喝 以及天邊略顯暗淡的 七八個星 都聚于一個小鎮 小鎮在我的夢里
    種花家吳嶺閱讀 214評論 0 0