Android 仿doodle jump小游戲

這個(gè)游戲的邏輯主要在上升和下降以及觸碰原理

圖片:
4eb3cc2601e30b38736028f1ed4ba31.jpg
20190523_094047.gif

沒有做開始游戲 暫停游戲 和結(jié)束游戲统屈,但是主要邏輯全部都做出來了

思路,那個(gè)類創(chuàng)建的數(shù)據(jù)那個(gè)類管理

鏈?zhǔn)侥MAndroid 的Handler做出來的

d代碼如下:

   //存入梯子數(shù)據(jù)
    public void saveInitData(LadderData ladderData) {


        if (mLadderData == null) {

            mLadderData = ladderData;
            //mLadderData.setIndex(mLadderData.getIndex() == 0 ? 0 : -1);

        } else {

            LadderData ladderData1 = new LadderData();

            ladderData1.setLadderX(ladderData.getLadderX());

            ladderData1.setLadderY(ladderData.getLadderY());

            ladderData1.setmLadderData(mLadderData);


            mLadderData = ladderData1;


        }

        initIndex();
    }

這塊邏輯是類似于一個(gè)個(gè)引用在一塊宪郊,Handler的代碼邏輯:

  Message next() {
        // Return here if the message loop has already quit and been disposed.
        // This can happen if the application tries to restart a looper after quit
        // which is not supported.
        final long ptr = mPtr;
        if (ptr == 0) {
            return null;
        }

        int pendingIdleHandlerCount = -1; // -1 only during first iteration
        int nextPollTimeoutMillis = 0;
        for (;;) {
            if (nextPollTimeoutMillis != 0) {
                Binder.flushPendingCommands();
            }

            nativePollOnce(ptr, nextPollTimeoutMillis);

            synchronized (this) {
                // Try to retrieve the next message.  Return if found.
                final long now = SystemClock.uptimeMillis();
                Message prevMsg = null;
                Message msg = mMessages;
                if (msg != null && msg.target == null) {
                    // Stalled by a barrier.  Find the next asynchronous message in the queue.
                    do {
              //-------------------------------------------------------------------------------------------------------------------------
            
                        prevMsg = msg;

                //這塊↓↓
                        msg = msg.next;
                    } while (msg != null && !msg.isAsynchronous());
                }
                if (msg != null) {
                    if (now < msg.when) {
                        // Next message is not ready.  Set a timeout to wake up when it is ready.
                        nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
                    } else {
                        // Got a message.
                        mBlocked = false;
                        if (prevMsg != null) {
                            prevMsg.next = msg.next;
                        } else {

                            mMessages = msg.next;
                        }
                        msg.next = null;
                        if (DEBUG) Log.v(TAG, "Returning message: " + msg);
                        msg.markInUse();
                        return msg;
                    }
                } else {
                    // No more messages.
                    nextPollTimeoutMillis = -1;
                }

                // Process the quit message now that all pending messages have been handled.
                if (mQuitting) {
                    dispose();
                    return null;
                }

                // If first time idle, then get the number of idlers to run.
                // Idle handles only run if the queue is empty or if the first message
                // in the queue (possibly a barrier) is due to be handled in the future.
                if (pendingIdleHandlerCount < 0
                        && (mMessages == null || now < mMessages.when)) {
                    pendingIdleHandlerCount = mIdleHandlers.size();
                }
                if (pendingIdleHandlerCount <= 0) {
                    // No idle handlers to run.  Loop and wait some more.
                    mBlocked = true;
                    continue;
                }

                if (mPendingIdleHandlers == null) {
                    mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];
                }
                mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);
            }

            // Run the idle handlers.
            // We only ever reach this code block during the first iteration.
            for (int i = 0; i < pendingIdleHandlerCount; i++) {
                final IdleHandler idler = mPendingIdleHandlers[i];
                mPendingIdleHandlers[i] = null; // release the reference to the handler

                boolean keep = false;
                try {
                    keep = idler.queueIdle();
                } catch (Throwable t) {
                    Log.wtf(TAG, "IdleHandler threw exception", t);
                }

                if (!keep) {
                    synchronized (this) {
                        mIdleHandlers.remove(idler);
                    }
                }
            }

            // Reset the idle handler count to 0 so we do not run them again.
            pendingIdleHandlerCount = 0;

            // While calling an idle handler, a new message could have been delivered
            // so go back and look again for a pending message without waiting.
            nextPollTimeoutMillis = 0;
        }
    }

a363f102f2e7efffde3f9ec003210bb.png

根據(jù)這個(gè)原理,它官方這個(gè)就是一個(gè)套一個(gè)

這個(gè)是重力加速度,復(fù)習(xí)了一下初中知識(shí)啊啊啊啊。衩椒。。蚕礼。烟具。梢什。奠蹬。

    /**
     * 返回拋物位置
     *
     * @return
     */

    private double getParabolic(long time) {


        return mV * (time * 0.001) - (0.5 * g * ((time * 0.001) * (time * 0.001)));


    }

    /**
     * 返回重力坐標(biāo)
     *
     * @param time
     * @return
     */

    private double gravityM(long time) {


        return 0.5 * g * kg * ((time * 0.001) * (time * 0.001));
    }

主要控制階梯往上移在這塊:

 //開始彈跳

    private void startJumpUp() {


        final int[] x = {0};

        new Thread(new Runnable() {
            @Override
            public void run() {


                while (true) {

                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    x[0]++;

                    if (arrayListladder != null && arrayListladder.size() > 0) {

                        for (int i = 0; i < arrayListladder.size(); i++) {


                            arrayListladder.get(i).setLadderY(arrayListladder.get(i).getLadderY() + x[0]);


                        }

                    }

                    if (x[0] > 40) {
                        break;
                    }


                }

            }
        }).start();

    }

這塊是鏈?zhǔn)阶頡AO的一段,可能網(wǎng)絡(luò)上的大神一下就看明白了,哈哈哈

 //刪除最后一個(gè)

    private void deleteLast() {

        if (mLadderData == null) {

            return;
        } else {


            LadderData temp = mLadderData;
            LadderData temp2 = null;
            if (mLadderData.getmLadderData() == null) {
                mLadderData = null;
            }


            while (true) {

                if (temp == null) {

                    if (temp2 != null) {
                        temp2.setmLadderData(null);
                    } else {
                        mLadderData = null;
                    }

                    break;
                }

                if (temp != null && temp.getmLadderData() != null)
                    temp2 = temp;
                temp = temp.getmLadderData();


            }

            // showData();

            initIndex();


        }


    }

碰撞的檢測功能類

 public void collision(ArrayList<LadderTempData> arrayList, int mLocationY, int x, GravityThread gravityThread, Context mContext) {

        garr.clear();

        for (int i = 0; i < arrayList.size(); i++) {
            LadderTempData ladderTempData = arrayList.get(i);
            int ladderY = ladderTempData.getLadderY();

            if (ladderY > 0) {
                garr.add(ladderTempData);

            }

            if (arrayList.size() < 5) {

                Ladder.temp.addAll(arrayList);
                arrayList.clear();


                Ladder.getInstance().initData(mContext);
                return;
            }

            if (arrayList.get(i).getLadderY() > mContext.getResources().getDisplayMetrics().heightPixels) {

                LadderTempData remove = arrayList.remove(i);
                i--;

            }

        }


        for (int i = 0; i < garr.size(); i++) {


            LadderTempData ladderTempData = garr.get(i);

            int ladderY = ladderTempData.getLadderY();
            int ladderX = ladderTempData.getLadderX();

            Log.e("坐標(biāo) X", "x:" + x + " ladderX:" + ladderX + " stopladderX:" + (ladderX + 300));
            Log.e("坐標(biāo) Y", "t:" + (ladderY + 5) + " b:" + (ladderY - 5) + " mLocationY:" + mLocationY);

            if (mLocationY < (ladderY + 50) && mLocationY > (ladderY - 50) && x > (ladderX - 40) && x < (ladderX + 300)) {

                gravityThread.height = ladderY + 20;

                Log.e("坐標(biāo)", "--------------------------");
                break;

            } else {

                gravityThread.initX();
            }


            //ladderY 底部坐標(biāo)
            //ladderY -40 頂部坐標(biāo)
            //ladderX 左部坐標(biāo)
            // ladderX + 300右部坐標(biāo)


           /* int t = ladderY - 50;
            int b = ladderY;


            int l = ladderX;
            int r = ladderX + 300;

            int pY = mLocationY;
            int pX = x;

            Log.e("坐標(biāo)", "top: " + (ladderY - 50) + " b:" + b + " mLocationY:" + mLocationY);

            if (pY < b && pY > t && pX > l && pY < r) {
                gravityThread.height = ladderY + 20;

                break;
            } else {
                gravityThread.initX();
            }
*/
        }


    }

類的主要說明

類說明:
XHApplication : 主要的Context
Accelerometer: 方向重力控制類
ControlTheCollision : 碰撞控制類
Gravity:   重力控制類
GravityThread : 重力控制線程
Ladder:梯子數(shù)據(jù)制造者
GameMainView:繪畫

Demo:https://github.com/hanxinhao000/jumpgame

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末嗡午,一起剝皮案震驚了整個(gè)濱河市囤躁,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌荔睹,老刑警劉巖狸演,帶你破解...
    沈念sama閱讀 218,284評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異僻他,居然都是意外死亡宵距,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門吨拗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來满哪,“玉大人婿斥,你說我怎么就攤上這事∩谘迹” “怎么了民宿?”我有些...
    開封第一講書人閱讀 164,614評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長像鸡。 經(jīng)常有香客問我活鹰,道長,這世上最難降的妖魔是什么只估? 我笑而不...
    開封第一講書人閱讀 58,671評(píng)論 1 293
  • 正文 為了忘掉前任志群,我火速辦了婚禮,結(jié)果婚禮上蛔钙,老公的妹妹穿的比我還像新娘赖舟。我一直安慰自己,他們只是感情好夸楣,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評(píng)論 6 392
  • 文/花漫 我一把揭開白布宾抓。 她就那樣靜靜地躺著,像睡著了一般豫喧。 火紅的嫁衣襯著肌膚如雪石洗。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,562評(píng)論 1 305
  • 那天紧显,我揣著相機(jī)與錄音讲衫,去河邊找鬼。 笑死孵班,一個(gè)胖子當(dāng)著我的面吹牛涉兽,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播篙程,決...
    沈念sama閱讀 40,309評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼枷畏,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了虱饿?” 一聲冷哼從身側(cè)響起拥诡,我...
    開封第一講書人閱讀 39,223評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎氮发,沒想到半個(gè)月后渴肉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,668評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡爽冕,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評(píng)論 3 336
  • 正文 我和宋清朗相戀三年仇祭,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片颈畸。...
    茶點(diǎn)故事閱讀 39,981評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡乌奇,死狀恐怖嚣艇,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情华弓,我是刑警寧澤食零,帶...
    沈念sama閱讀 35,705評(píng)論 5 347
  • 正文 年R本政府宣布,位于F島的核電站寂屏,受9級(jí)特大地震影響贰谣,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜迁霎,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評(píng)論 3 330
  • 文/蒙蒙 一吱抚、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧考廉,春花似錦秘豹、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至涮坐,卻和暖如春凄贩,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背袱讹。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評(píng)論 1 270
  • 我被黑心中介騙來泰國打工疲扎, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人捷雕。 一個(gè)月前我還...
    沈念sama閱讀 48,146評(píng)論 3 370
  • 正文 我出身青樓椒丧,卻偏偏與公主長得像,于是被迫代替她去往敵國和親救巷。 傳聞我的和親對(duì)象是個(gè)殘疾皇子壶熏,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評(píng)論 2 355