Android学习4——猜拳小游戏

本文详细介绍了如何使用Android开发猜拳小游戏,包括页面布局、执行程序、结果显示及交互逻辑,并通过代码展示了如何实现玩家出拳、结果比较及反馈。

1、猜拳小游戏页面布置:activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Tishi1" />


    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checkedButton="@+id/shitou"
        android:orientation="horizontal" >


        <RadioButton
            android:id="@+id/shitou"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="石头" />


        <RadioButton
            android:id="@+id/jiandao"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="剪刀" />


        <RadioButton
            android:id="@+id/bu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="布" />
    </RadioGroup>


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="出拳" />

执行程序页面显示如下:




</LinearLayout>

返回结果的页面布局resultstyle.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
<TextView
android:id="@+id/res"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25px"
/>
</LinearLayout>

执行程序页面显示如下:



读取传递参数主要代码MainActivity:

package com.game.zzw;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;


public class MainActivity extends Activity {
/** Called when the activity is first created. */


private RadioButton rdoST, rdoJD, rdoB;
private Button btnSend;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置当前的布局视图
setContentView(R.layout.activity_main);


rdoST = (RadioButton) findViewById(R.id.shitou);
rdoJD = (RadioButton) findViewById(R.id.jiandao);
rdoB = (RadioButton) findViewById(R.id.bu);
btnSend = (Button) findViewById(R.id.button1);


btnSend.setOnClickListener(new ViewBtnOcl());
}


class ViewBtnOcl implements View.OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
String ren = "0";
if (rdoST.isChecked()) {
ren = "1";
} else if (rdoJD.isChecked()) {
ren = "2";
} else {
ren = "3";
}
Intent intent = new Intent(MainActivity.this,
ResultActivity.class);
intent.putExtra("ren", ren);
startActivity(intent);
break;
}
}
          }
}

结果比较返回值主要代码ResulActivity:

package com.game.zzw;


import java.util.Random;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;


public class ResultActivity extends Activity {


private TextView myTextView;


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.resultstyle);


myTextView = (TextView) findViewById(R.id.res);


Intent intent = getIntent();
int ren = Integer.parseInt(intent.getStringExtra("ren"));


// 业务逻辑判断
// 自动生成npc产生拳的代码(1~3)
int npc = new Random().nextInt(3) + 1;
// 处理请求数据并进行逻辑判断,将结果返回
String res = "";
if (ren == npc) {
res = "平局,再接再厉";
} else if (ren == 3 && npc == 1) {
res = "恭喜,您获胜了……";
} else if (ren == 1 && npc == 3) {
res = "虽败犹荣……再来一局";
} else if (ren < npc) {
res = "恭喜,您获胜了……";
} else {
res = "虽败犹荣……再来一局";
}


String msg = "您:" + change(ren) + "  vs  " + "电脑:" + change(npc)
+ "\n您" + res;


myTextView.setText(msg);


}


// 将数字转换成汉字
private String change(int r) {
switch (r) {
case 1:
return "石头";
case 2:
return "剪刀";
case 3:
return "布";
}
return null;
}


}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值