Android:一个很简单的扑克游戏(从项目资源创建图像)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huangbiao86/article/details/6733755

string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">简单的扑克牌游戏</string>
    <string name="app_name">扑克牌</string>
    <string name="again">再来一次</string>
    <string name="good_luck">恭喜您,猜对了,你该买彩票了!</string>
    <string name="bad_luck">很遗憾,猜错了,有志者事竟成,再来一次!</string>
    <string name="guess">猜猜A是哪一个张?</string>
</resources>

Activity

public class CardsActivity extends Activity {
    /** Called when the activity is first created. */
	private ImageView imageView0=null;
    private ImageView imageView1=null;
    private ImageView imageView2=null;
    private ImageView imageView3=null;
    private ImageView imageView4=null;
    private TextView textView01=null;
    //得到所有的图片的id
    private static int image[]=new int[]
    		{R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,R.drawable.main,};
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageView0=(ImageView)findViewById(R.id.card1);
        imageView1=(ImageView)findViewById(R.id.card2);
        imageView2=(ImageView)findViewById(R.id.card3);
        imageView3=(ImageView)findViewById(R.id.card4);
        imageView4=(ImageView)findViewById(R.id.main);
        randomNum();
        
        imageView0.setOnClickListener(new GuessCardOnclicker());
        imageView1.setOnClickListener(new GuessCardOnclicker());
        imageView2.setOnClickListener(new GuessCardOnclicker());
        imageView3.setOnClickListener(new GuessCardOnclicker());
        imageView4.setOnClickListener(new GuessCardOnclicker());
    }
    
    class GuessCardOnclicker implements OnClickListener{
    	/**
    	 * 根据点击事件,得到id,再根据id得到点击 的imageView对象,再判断这个imageVieW的image是不是A
    	 */
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.card1:
				gameResult(R.id.card1);
				break;
			case R.id.card2:
				gameResult(R.id.card2);
				break;
			case R.id.card3:
				gameResult(R.id.card3);
				break;
			case R.id.card4:
				gameResult(R.id.card4);
				break;
			case R.id.main:
				gameResult(R.id.main);
				break;
			default:
				break;
			}
		}
    }
    
    private void gameResult(int cardId) {
    	//显示图片的所有随机值;
    	 imageView0.setImageDrawable(getResources().getDrawable(image[0]));
         imageView1.setImageDrawable(getResources().getDrawable(image[1]));
         imageView2.setImageDrawable(getResources().getDrawable(image[2]));
         imageView3.setImageDrawable(getResources().getDrawable(image[3]));
         imageView4.setImageDrawable(getResources().getDrawable(image[4]));
         
         ImageView imageView;
         
         //得到点击 的imageView、判断是不是猜 对了。
         if(((imageView0.getId() == cardId)&&(R.drawable.image1 == image[0]))||
         ((imageView1.getId() == cardId)&&(R.drawable.image1 == image[1]))||
         ((imageView2.getId() == cardId)&&(R.drawable.image1 == image[2]))||
         ((imageView3.getId() == cardId)&&(R.drawable.image1 == image[3]))||
         ((imageView4.getId() == cardId)&&(R.drawable.image1 == image[4]))){
        	 hintResult(true);
         }else{
        	 hintResult(false);
         }
         
    }
    
    public void hintResult(boolean result){
    	if(result){
    		Toast.makeText(CardsActivity.this, "恭喜你,猜对了!", 500).show();
    	}else{
    		Toast.makeText(CardsActivity.this, "对不起,猜错了!", 500).show();
    	}
    	imageView0.setEnabled(false);
    	imageView1.setEnabled(false);
    	imageView2.setEnabled(false);
    	imageView3.setEnabled(false);
    	imageView4.setEnabled(false);
    }
    
    /**
     * 随机生成一个数组对象,存储扑克。代表imageView里的随机图像
     */
    private void randomNum(){
        for (int i = 0; i < 5; i++)
        {
            int s=(int)Math.random()*3;
            int temp;
            temp=image[i];
            image[i]=image[s];
            image[s]=temp;
        }
    }
    /**
     * 点击空白外游戏重新来;由于imageView也设置了点击 事件,当点击 屏幕的时候,imageView优先
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	imageView0.setImageDrawable(getResources().getDrawable(R.drawable.hint));
    	imageView1.setImageDrawable(getResources().getDrawable(R.drawable.hint));
    	imageView2.setImageDrawable(getResources().getDrawable(R.drawable.hint));
    	imageView3.setImageDrawable(getResources().getDrawable(R.drawable.hint));
    	imageView4.setImageDrawable(getResources().getDrawable(R.drawable.hint));
    	 randomNum();
    	 imageView0.setEnabled(true);
     	imageView1.setEnabled(true);
     	imageView2.setEnabled(true);
     	imageView3.setEnabled(true);
     	imageView4.setEnabled(true);
    	return super.onTouchEvent(event);
    }
}


layout布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
	android:id="@+id/game_name"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="25dp"
    android:layout_centerHorizontal="true"
    android:textColor="#ED207A"
    android:text="@string/hello"
    />
 <TextView 
 	android:id="@+id/tv_hint"
 	android:layout_width="wrap_content"
 	android:layout_height="wrap_content"
 	android:textSize="15dp"
 	android:layout_below="@id/game_name"
 	android:textColor="#FFFFFF"
 	android:text="@string/guess"/>

<ImageView 
	android:id="@+id/card1"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content" 
	android:layout_below="@id/tv_hint"
	android:layout_marginTop="5dp"
	android:layout_centerHorizontal="true"
	android:src="@drawable/hint"/>
	
<ImageView 
	android:id="@+id/card2"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content" 
	android:layout_below="@id/card1"
	android:layout_marginTop="10dp"
	android:layout_alignParentLeft="true"
	android:layout_marginLeft="5dp"
	android:src="@drawable/hint"/>
	
<ImageView 
	android:id="@+id/main"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content" 
	android:layout_below="@id/card1"
	android:layout_marginTop="10dp"
	android:layout_centerHorizontal="true"
	android:src="@drawable/hint"/>
	
<ImageView 
	android:id="@+id/card3"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content" 
	android:layout_below="@id/card1"
	android:layout_marginTop="10dp"
	android:layout_alignParentRight="true"
	android:layout_marginRight="5dp"
	android:src="@drawable/hint"/>
	
<ImageView 
	android:id="@+id/card4"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content" 
	android:layout_below="@id/card3"
	android:layout_marginTop="10dp"
	android:layout_centerHorizontal="true"
	android:src="@drawable/hint"/>

</RelativeLayout>


所用到的资源:



运行效果如下:


  

展开阅读全文

没有更多推荐了,返回首页