灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:3871回复:0

Android--Android扑克牌猜点小游戏

楼主#
更多 发布于:2013-01-10 15:39
该游戏是简单的猜点游戏,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 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>

喜欢0 评分0
游客

返回顶部