Android撲克牌猜點小游戲源碼分享。
該游戲是簡單的猜點游戲,
1點為正確的點數
點重新開始後進行洗牌
每次牌的順序不同
Java代碼:
package com.mrzhu.test0109_project;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class Main extends Activity {
private ImageView imav1;
private ImageView imav2;
private ImageView imav3;
private Button reset;//重新開始按鈕
private TextView content;//顯示結果
private int[] arr;//0到2的數組,用於洗牌
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//取得控件
imav1 = (ImageView) findViewById(R.id.imv1);
imav2 = (ImageView) findViewById(R.id.imv2);
imav3 = (ImageView) findViewById(R.id.imv3);
reset = (Button) findViewById(R.id.reset);
content = (TextView) findViewById(R.id.content);
//為重新開始按鈕設置監聽
reset.setOnClickListener(new resetOnClickListener());
}
//判斷點擊了哪個牌,響應相應事件
public void click(View v){
if(v.getId() == imav1.getId()){
imav1Click();
}else if(v.getId() == imav2.getId()){
imav2onClick();
}else if(v.getId() == imav3.getId()){
imav3onClick();
}
}
//單擊了第一張牌響應的事件
public void imav1Click() {
retu();
if(arr[0] == 0)
content.setText("恭喜你,你猜對了!");
else
content.setText("哦哦,猜錯了!");
imav2.setAlpha(100);
imav3.setAlpha(100);
imav2.setClickable(false);
imav3.setClickable(false);
}
//單擊了第二張牌響應的事件
public void imav2onClick() {
retu();
if(arr[1] == 0)
content.setText("恭喜你,你猜對了!");
else
content.setText("哦哦,猜錯了!");
imav1.setAlpha(100);
imav3.setAlpha(100);
imav1.setClickable(false);
imav3.setClickable(false);
}
//單擊了第三張牌響應的事件
public void imav3onClick() {
retu();
if(arr[2] == 0)
content.setText("恭喜你,你猜對了!");
else
content.setText("哦哦,猜錯了!");
imav1.setAlpha(100);
imav2.setAlpha(100);
imav1.setClickable(false);
imav2.setClickable(false);
}
//重新開始按鈕事件
public class resetOnClickListener implements OnClickListener{
public void onClick(View v) {
imav1.setImageResource(R.drawable.p04);
imav2.setImageResource(R.drawable.p04);
imav3.setImageResource(R.drawable.p04);
imav1.setAlpha(255);
imav2.setAlpha(255);
imav3.setAlpha(255);
content.setText("猜猜看");
imav1.setClickable(true);
imav2.setClickable(true);
imav3.setClickable(true);
}
}
//翻牌時調用的方法,生成不重復的隨機數加到數組中,設置控件顯示的圖片
private void retu(){
Random random = new Random();
arr = new int[3];
for(int i = 0; i < 3; ++i){
int index = random.nextInt(3);
if(arr[index] != 0)
i--;
else
arr[index] = i;
}
imav1.setImageResource(R.drawable.p01 + arr[0]);
imav2.setImageResource(R.drawable.p01 + arr[1]);
imav3.setImageResource(R.drawable.p01 + arr[2]);
}
}
main.xml代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/content"
android:textSize="20px"
android:text="猜猜看" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/p04"
android:id="@+id/imv1"
android:onClick="click"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/p04"
android:id="@+id/imv2"
android:onClick="click"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/p04"
android:id="@+id/imv3"
android:onClick="click"
/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重新開始"
android:id="@+id/reset"
/>
</LinearLayout>
資源下載地址:
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2013年資料/1月/20日/Android撲克牌猜點小游戲源碼