Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android 小項目之---猜撲克牌遊戲 (附源碼)

Android 小項目之---猜撲克牌遊戲 (附源碼)

編輯:Android開發實例

 本實例參考《Google Android SDK開發范例大全》。

  針對上述書本,個人的一點點優化和修正的一處BUG,感興趣的不妨可以參考一下,

運行圖:

 

代碼如下:

 

package cn.terry;

import android.app.Activity; 
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class testGame extends Activity {
    private ImageView myImageView;
    private ImageView myImageView2;
    private ImageView myImageView3;
    private Button myButton;
     private int a=0;
    private int[] myImage={
            R.drawable.p01,
            R.drawable.p02,
            R.drawable.p03, 
    };
    
    private View.OnClickListener MyimageClistener;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        myImageView=(ImageView)findViewById(R.id.mImage01);
        myImageView2=(ImageView)findViewById(R.id.mImage02);
        myImageView3=(ImageView)findViewById(R.id.mImage03);
         
        myButton=(Button)findViewById(R.id.mButton);
        Randon();
        MyimageClistener=new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                
                // TODO Auto-generated method stub
                  if(a==0)
                  {
                        switch (v.getId()) {
                        case R.id.mImage01:
                            SetImage(0,myImageView);
                             
                            break;
                        case R.id.mImage02:
                            SetImage(1,myImageView2);
                             
                            break;
                        case R.id.mImage03:
                            SetImage(2,myImageView3);
                             
                            break;
                        default:
                            break;
                        }
                  }
            }
        };
        
        myImageView.setOnClickListener(MyimageClistener);
        myImageView2.setOnClickListener(MyimageClistener);
        myImageView3.setOnClickListener(MyimageClistener);
        
        myButton.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(a==1)
                {
                    myImageView.setImageResource(R.drawable.p04);
                    myImageView2.setImageResource(R.drawable.p04);
                    myImageView3.setImageResource(R.drawable.p04);
                    myImageView.setAlpha(255);
                    myImageView2.setAlpha(255);
                    myImageView3.setAlpha(255);
                    Randon();
                    a--;
                }
            }
        });
    }
    
    private void SetImage(int temp,ImageView ss)
    {
        myImageView.setImageResource(myImage[0]);
        myImageView2.setImageResource(myImage[1]);
        myImageView3.setImageResource(myImage[2]);
        myImageView.setAlpha(100);
        myImageView2.setAlpha(100);
        myImageView3.setAlpha(100);
        ss.setAlpha(255);
        if(myImage[temp]==R.drawable.p01)
        {
            Toast.makeText(testGame.this, "恭喜你猜對了", Toast.LENGTH_SHORT)
            .show();
        }
        else
        {
            Toast.makeText(testGame.this, "對不起你猜錯了", Toast.LENGTH_SHORT)
            .show();
        }
        a ;
    }
    
    
    
    private void Randon()
    {
        for(int i=0;i<myImage.length;i )
        {
            int temp=myImage[i];
            int s=(int)(Math.random()*2);
            myImage[i]=myImage[s];
            myImage[s]=temp;
        }
    }
    
    
    
    
    
    
}

 

修改BUG一處,當用戶選擇完後,不可以再選,需要重新開始遊戲。

優化如下:

1:提取公共事件。

2:封裝相同代碼,如SetImage這個方法。

 源碼下載:/Files/TerryBlog/testGame.rar

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved