最近微信上很火的小遊戲【壹秒】android版——開發分享,android

來源:互聯網
上載者:User

最近上很火的小遊戲【壹秒】android版——開發分享,android

    最近在朋友圈,朋友轉了一個html小遊戲【壹秒】,遊戲的規則是:使用者按住按鈕然後釋放,看誰能精準地保持一秒的時間。^_^剛好剛才在linux伺服器上偵錯工具的時候伺服器掛了,騰出點時間分享下前天自己做的這個小遊戲。

話不多說,直接:

   

呐,作為一個對android無比熱愛的小夥伴,騰出一個小小時間做一個小小的小遊戲,也不失為一種享受哈。整個遊戲比較簡單,主要分為UI設計與開發和時間計算邏輯。當然,這個小遊戲介面要做得漂亮點,所以我側重講下UI設計與開發。


UI設計與開發

    採用RelativeLayout相對布局來控制整個介面比較靈活。基本的布局相信對你們來說都很easy,都是簡單的排版。主要在於按住“按住”按鈕之後怎麼實現布局變長,這裡提一下。我的思路是顯示秒數布局一開始包含了全部所需要的空間,這個布局採用高度隨內容增長,然後寫一個高度10dp的view把高度撐起來,然後其他直接隱藏,後面根據邏輯直接顯示就好。

下面是我的xml布局代碼

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    android:background="#ffffff"    >    <!--   android:background="@drawable/bg"--><!--    <TextView        android:layout_width="match_parent"        android:layout_height="50dp"        android:text="one second game,ready GO!"        android:textSize="22sp"        android:gravity="center"        android:textColor="#ffffff"        android:background="#000000"/>-->    <LinearLayout        android:id="@+id/linearLayout"        android:layout_marginTop="30dp"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <TextView            android:id="@+id/tip_l"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="你的"            android:textSize="50sp"            android:gravity="right"            android:textColor="#666666"            android:textStyle="bold"/>        <TextView            android:id="@+id/tip_r"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1.3"            android:text="一秒鐘"            android:textSize="50sp"            android:textColor="#FF3333"            android:gravity="left"            android:textStyle="bold"            />    </LinearLayout>    <TextView        android:layout_marginTop="3dp"        android:id="@+id/tip_b"        android:paddingLeft="8dp"        android:layout_below="@+id/linearLayout"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="到底有多長?"        android:textSize="40sp"        android:textColor="#666666"        android:textStyle="bold"        android:gravity="center"/>    <TextView        android:layout_marginTop="15dp"        android:id="@+id/tip_e"        android:layout_below="@+id/tip_b"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="How long is one second?"        android:textSize="15sp"        android:textColor="#666666"        android:textStyle="bold"        android:gravity="center"/>    <LinearLayout        android:layout_below="@+id/tip_e"        android:id="@+id/show_linearlayout"        android:layout_marginLeft="50dp"        android:layout_marginRight="50dp"        android:layout_marginTop="3dp"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="@drawable/show"        android:orientation="vertical">        <TextView            android:layout_width="fill_parent"            android:layout_height="10dp"/>        <LinearLayout            android:layout_marginTop="10dp"            android:id="@+id/show_result"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:orientation="horizontal"            android:visibility="gone">            <TextView                android:id="@+id/show_result_l"                android:layout_width="0dp"                android:layout_weight="1.7"                android:layout_height="wrap_content"                android:gravity="right"                android:textColor="#CC9933"                android:textSize="40sp"/>            <TextView                android:layout_marginLeft="3dp"                android:id="@+id/show_result_r"                android:text="秒"                android:gravity="left"                android:layout_width="0dp"                android:layout_weight="1"                android:layout_height="wrap_content"                android:textColor="#ffffff"                android:textSize="25sp"/>        </LinearLayout>        <TextView            android:paddingLeft="30dp"            android:paddingRight="30dp"            android:layout_marginTop="5dp"            android:gravity="center"            android:id="@+id/show_remark"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:visibility="gone"            android:textColor="#ffffff"            android:textSize="15sp"            android:layout_marginBottom="20dp"/>     </LinearLayout>    <Button        android:layout_below="@+id/show_linearlayout"        android:layout_centerHorizontal="true"        android:background="@drawable/button"        android:layout_marginTop="20dp"        android:id="@+id/cul_bt"        android:layout_height="120dp"        android:layout_width="120dp"        android:layout_gravity="center_horizontal"        android:gravity="center"        android:text="按住"        android:textColor="#ffffff"        android:textSize="30sp"/></RelativeLayout>


在UI介面中,為了更加美化加入了特殊字型。我加的是“方體卡通”和“華文彩雲”字型。在android中顯示特殊字型的方法是:如果你的IDE是eclipse,那麼直接放在assets中(方法在此我不多介紹),我的IDE是android studio,如果你的IDE和我是一樣的,可以使用以下方法:

首先在app→src→main目錄下建立assets包,然後在自己項目的.iml檔案中添加

<option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets" />


之後在自己的代碼中進行引用

private Typeface fangtikatong,huawencaiyun;//方體卡通,華文彩雲
// 將字型檔儲存在assets目錄下,www.linuxidc.com建立Typeface對象fangtikatong = Typeface.createFromAsset(getAssets(),"fangtikatong.ttf");huawencaiyun = Typeface.createFromAsset(getAssets(),"huawencaiyun.TTF");result_r.setTypeface(fangtikatong);tip_l.setTypeface(huawencaiyun);


時間計算邏輯

    時間計算邏輯比較簡單,首先對按鈕實現觸摸監聽,設定兩個變數記錄手摁下那一刻和離開那一刻(同個觸摸過程)的系統時間,後者減去前者再進行單位換算就可以算出手觸碰的期間。

代碼如下:

click_cl.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                if(event.getAction() == MotionEvent.ACTION_DOWN){                    x1 = (long)System.currentTimeMillis();                    //show = new StringBuffer("第一次按下"+'\n'+x1+'\n');                }                if(event.getAction() == MotionEvent.ACTION_UP){                    x2 = (long)System.currentTimeMillis();                    if(result_l.getVisibility() == View.GONE ||                            result_l.getVisibility() == View.GONE ||                            remark.getVisibility() == View.GONE ){                        result.setVisibility(View.VISIBLE);                        result_l.setVisibility(View.VISIBLE);                        result_r.setVisibility(View.VISIBLE);                        remark.setVisibility(View.VISIBLE);                    }                    num = (x2-x1)*1.000/1000;                    result_l.setText(num+"");                    //小於或等於1的先乘以1000再除以200                    if(num <= 1){                        remark.setText(remarks[(int)((num*1000)/200)]);                    //大於1的先乘以1000再除以200                    }else{                        remark.setText(remarks[(int)((num*1000+200)/200)]);                    }                }                return true;            }        });
到此,整個小遊戲基本開發完成,是不是也挺簡單的呢?

熱愛android的你們,閒置時候也可以嘗試開發自己的小遊戲呢?


CSDN遊戲代碼下載:一秒遊戲源碼

github遊戲代碼下在:一秒遊戲源碼








著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.