Android基础综合案例(八)猜拳小游戏
2014-04-04 22:25
405 查看
猜拳小游戏
一、要求
阶段1:进行猜拳小游戏的布局
阶段2:编写电脑类和玩家类
阶段3:编写猜拳游戏类,判定输赢
阶段4:页面跳转传值
二、具体实现代码
1、activity_main.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bj"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请选择你要出的拳:"
android:textColor="#FF0000"
android:textSize="30sp" />
<RadioGroup
android:id="@+id/radiogroupe"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/Stone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="石头"
android:textSize="20sp" />
<RadioButton
android:id="@+id/Scissors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="剪刀"
android:textSize="20sp" />
<RadioButton
android:id="@+id/Cloth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="布"
android:textSize="20sp" />
</RadioGroup>
<Button
android:id="@+id/quchuan"
android:layout_width="179dp"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:text="出拳"
android:textSize="20sp" />
</LinearLayout>
2、result.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/bj">
<TextView
android:id="@+id/result"
android:textSize="30sp"
android:textColor="#FF0000"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
3、MainActivity.java
package com.example.guess;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
public class MainActivity extends Activity {
private RadioGroup radioGroup;
private Button button;
private OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.quchuan:
// 获得所选择的拳
int id = radioGroup.getCheckedRadioButtonId();
int quan = 0;
// 根据不同的选择 给一个整型值赋值 石头1 剪刀2 布3
switch (id) {
case R.id.Stone:
quan = 1;
break;
case R.id.Scissors:
quan = 2;
break;
case R.id.Cloth:
quan = 3;
break;
default:
break;
}
// 启动结果页面 并传值
Intent intent = new Intent();
intent.setClass(MainActivity.this, ResultActivity.class);
intent.putExtra("quan", quan);
startActivity(intent);
break;
default:
break;
}
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = (RadioGroup) findViewById(R.id.radiogroupe);
button = (Button) findViewById(R.id.quchuan);
button.setOnClickListener(listener);
}
}
4、ResultActivity.java
package com.example.guess;
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 textview;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
textview=(TextView)findViewById(R.id.result);
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
int ren=bundle.getInt("quan");
//电脑出的拳
Random random=new Random();
int cpu=random.nextInt(3-1)+1;
String result=compare(ren ,cpu);
textview.setText(result);
}
private String compare(int ren ,int cpu){
StringBuffer show=new StringBuffer("结果:\n"+"人:"+xianshi(ren)+" VS "+"电脑:"+xianshi(cpu)+"\n");
if (ren==cpu) {
show.append("哈哈,平局!");
}else if(ren==1 && cpu==3){
show.append("电脑胜,你输啦!");
}else if(ren==3 && cpu==1){
show.append("人胜,真厉害!");
}else if(ren<cpu){
show.append("人胜,真厉害!");
}else if(ren>cpu){
show.append("电脑胜,你输啦!");
}
return show.toString();
}
private String xianshi(int id){
switch (id) {
case 1:
return "石头";
case 2:
return "剪刀";
case 3:
return "布";
default:
break;
}
return null;
}
}
5、Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">猜拳游戏</string>
<string name="action_settings">Settings</string>
</resources>
6、AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.guess"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.guess.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ResultActivity"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
三、运行效果图
1、猜拳效果图
2、显示结果图
一、要求
阶段1:进行猜拳小游戏的布局
阶段2:编写电脑类和玩家类
阶段3:编写猜拳游戏类,判定输赢
阶段4:页面跳转传值
二、具体实现代码
1、activity_main.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bj"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请选择你要出的拳:"
android:textColor="#FF0000"
android:textSize="30sp" />
<RadioGroup
android:id="@+id/radiogroupe"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/Stone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="石头"
android:textSize="20sp" />
<RadioButton
android:id="@+id/Scissors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="剪刀"
android:textSize="20sp" />
<RadioButton
android:id="@+id/Cloth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="布"
android:textSize="20sp" />
</RadioGroup>
<Button
android:id="@+id/quchuan"
android:layout_width="179dp"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:text="出拳"
android:textSize="20sp" />
</LinearLayout>
2、result.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/bj">
<TextView
android:id="@+id/result"
android:textSize="30sp"
android:textColor="#FF0000"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
3、MainActivity.java
package com.example.guess;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
public class MainActivity extends Activity {
private RadioGroup radioGroup;
private Button button;
private OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.quchuan:
// 获得所选择的拳
int id = radioGroup.getCheckedRadioButtonId();
int quan = 0;
// 根据不同的选择 给一个整型值赋值 石头1 剪刀2 布3
switch (id) {
case R.id.Stone:
quan = 1;
break;
case R.id.Scissors:
quan = 2;
break;
case R.id.Cloth:
quan = 3;
break;
default:
break;
}
// 启动结果页面 并传值
Intent intent = new Intent();
intent.setClass(MainActivity.this, ResultActivity.class);
intent.putExtra("quan", quan);
startActivity(intent);
break;
default:
break;
}
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = (RadioGroup) findViewById(R.id.radiogroupe);
button = (Button) findViewById(R.id.quchuan);
button.setOnClickListener(listener);
}
}
4、ResultActivity.java
package com.example.guess;
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 textview;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
textview=(TextView)findViewById(R.id.result);
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
int ren=bundle.getInt("quan");
//电脑出的拳
Random random=new Random();
int cpu=random.nextInt(3-1)+1;
String result=compare(ren ,cpu);
textview.setText(result);
}
private String compare(int ren ,int cpu){
StringBuffer show=new StringBuffer("结果:\n"+"人:"+xianshi(ren)+" VS "+"电脑:"+xianshi(cpu)+"\n");
if (ren==cpu) {
show.append("哈哈,平局!");
}else if(ren==1 && cpu==3){
show.append("电脑胜,你输啦!");
}else if(ren==3 && cpu==1){
show.append("人胜,真厉害!");
}else if(ren<cpu){
show.append("人胜,真厉害!");
}else if(ren>cpu){
show.append("电脑胜,你输啦!");
}
return show.toString();
}
private String xianshi(int id){
switch (id) {
case 1:
return "石头";
case 2:
return "剪刀";
case 3:
return "布";
default:
break;
}
return null;
}
}
5、Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">猜拳游戏</string>
<string name="action_settings">Settings</string>
</resources>
6、AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.guess"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.guess.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ResultActivity"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
三、运行效果图
1、猜拳效果图
2、显示结果图
相关文章推荐
- Android基础综合案例(七)《简易计算器UI界面设计》
- Android基础综合案例(九)Android UI设计
- Android基础综合案例(六)
- Android基础综合案例(五)
- Android基础综合案例(九)Android UI设计2
- Android基础综合案例(三)
- Android基础综合案例(一)
- Android基础综合案例(四)
- Android基础综合案例(二)
- Android基础开发小案例之短信发送器
- Android和H5互调案例基础详解
- Android案例(1)——美女拼图小游戏
- IOS基础UI之(六)综合小案例-应用管理
- Android PopupWindow,GridView,BaseAdapter以及图片合成综合案例
- android基础:handler与messag案例(计时器)
- Android基础教程(六)之----多选项CheckBox的综合应用
- android基础:动画案例(图片翻转)
- android开发------案例二(猜拳游戏)
- Android-NDK入门案例及基础问题解决(一)
- 分享我的第一个Android小游戏——猜拳游戏