Android--Android撲克牌猜點小遊戲

來源:互聯網
上載者:User

該遊戲是簡單的猜點遊戲,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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.