首页 > 代码库 > 蜗牛—Android基础之简易猜拳游戏

蜗牛—Android基础之简易猜拳游戏

MainActivity.java

package org.example.guess;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

	private ImageButton r_imgBtn, p_imgBtn, s_imgBtn; // 石头、布、剪刀的按钮
	private ImageView imgView; // 游戏界面上面的按钮
	private TextView reslut_tv, coun_tv; // 结果及游戏次数的textView
	int count = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		r_imgBtn = (ImageButton) findViewById(R.id.btnRock);
		p_imgBtn = (ImageButton) findViewById(R.id.btnPaper);
		s_imgBtn = (ImageButton) findViewById(R.id.btnSci);

		imgView = (ImageView) findViewById(R.id.viewCmp);

		reslut_tv = (TextView) findViewById(R.id.textResult);
		coun_tv = (TextView) findViewById(R.id.textCount);

		MyOnClickListener myOnClickListener = new MyOnClickListener();

		r_imgBtn.setOnClickListener(myOnClickListener);
		p_imgBtn.setOnClickListener(myOnClickListener);
		s_imgBtn.setOnClickListener(myOnClickListener);
	}

	private class MyOnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int rand = (int) (Math.random() * 3 + 1); // 得到1~3的随机数
			count++;// 游戏次数++
			switch (rand) {
			/**
			 * 当rand=1时,即电脑出的是石头,然后再判断用户出的什么。
			 */
			case 1:
				imgView.setImageResource(R.drawable.rock);
				switch (v.getId()) {
				case R.id.btnRock:
					reslut_tv.setText(getString(R.string.result)
							+ getString(R.string.result_draw));
					coun_tv.setText("游戏次数:" + count);
					break;
				case R.id.btnPaper:
					reslut_tv.setText(getString(R.string.result)
							+ getString(R.string.result_lose));
					coun_tv.setText("游戏次数:" + count);
					break;
				case R.id.btnSci:
					reslut_tv.setText(getString(R.string.result)
							+ getString(R.string.result_win));
					coun_tv.setText("游戏次数:" + count);
					break;
				}
				break;
			case 2:
				imgView.setImageResource(R.drawable.paper);
				switch (v.getId()) {
				case R.id.btnRock:
					reslut_tv.setText(getString(R.string.result)
							+ getString(R.string.result_win));
					coun_tv.setText("游戏次数:" + count);
					break;
				case R.id.btnPaper:
					reslut_tv.setText(getString(R.string.result)
							+ getString(R.string.result_draw));
					coun_tv.setText("游戏次数:" + count);
					break;
				case R.id.btnSci:
					reslut_tv.setText(getString(R.string.result)
							+ getString(R.string.result_lose));
					coun_tv.setText("游戏次数:" + count);
					break;
				}
				break;
			case 3:
				imgView.setImageResource(R.drawable.scissors);
				switch (v.getId()) {
				case R.id.btnRock:
					reslut_tv.setText(getString(R.string.result)
							+ getString(R.string.result_lose));
					coun_tv.setText("游戏次数:" + count);
					break;
				case R.id.btnPaper:
					reslut_tv.setText(getString(R.string.result)
							+ getString(R.string.result_win));
					coun_tv.setText("游戏次数:" + count);
					break;
				case R.id.btnSci:
					reslut_tv.setText(getString(R.string.result)
							+ getString(R.string.result_draw));
					coun_tv.setText("游戏次数:" + count);
					break;
				}
				break;
			}
		}
	}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/welcome"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/fg_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src=http://www.mamicode.com/"@drawable/fg_title" />>
WelcomeActivity.java

package org.example.guess;

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

public class WelcomeActivity extends Activity {

	/*
	 * 游戏开始界面 、、、即等待1.5s后进入游戏界面
	 */
	@Override
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		super.setContentView(R.layout.welcome);
		Thread splashTread = new Thread() {
			@Override
			public void run() {
				try {
					sleep(1500);// 这里进行操作
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					finish();// 调用这个Activity 的finish方法后,在主界面按手机 上的放回键就会直接退出程序
					// 启动主应用
					Intent intent = new Intent();
					intent.setClass(WelcomeActivity.this, MainActivity.class);
					startActivity(intent);
				}
			}
		};
		splashTread.start();
	}
}

welcome.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"
    android:orientation="vertical" >
    <ImageView
        android:src=http://www.mamicode.com/"@drawable/welcome" >