Android初学者综合应用——猜拳游戏

我是一个技术爱好者,不光是移动开发,还是画简单的插画,都与专业人士不相上下。这次我专门利用业余时间,借鉴参考书上的代码,自己编写了电脑猜拳游戏,以飨Android移动开发初学者。

猜拳游戏的界面如下图所示:

以XML语言的方式写法如下,其中我使用了TableLayout和LinearLayout,源文件位于WebView\app\src\main\res\layout\finger_guessing_game_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TableRow android:orientation="horizontal"
        android:textAlignment="center"
        android:layout_gravity="center"
        android:gravity="center">
        <TextView
            android:id="@+id/title"
            android:paddingTop="12sp"
            android:paddingBottom="12sp"
            android:textSize="32sp"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="和电脑猜拳"/>
    </TableRow>
    <TableRow android:gravity="center"
        android:paddingLeft="32sp"
        android:paddingRight="32sp">
        <TextView
            android:id="@+id/computerActionText"
            android:text="电脑出拳:"
            android:layout_weight="1"
            />
        <TextView
            android:id="@+id/playerActionText"
            android:text="玩家出拳:"
            android:layout_weight="1"/>
    </TableRow>
    <TableRow android:gravity="center"
        android:paddingLeft="32sp"
        android:paddingRight="32sp">
        <TextView
            android:id="@+id/whatComputerActedText"
            android:textSize="28sp"
            android:text="布"
            android:layout_weight="1"
            />
        <LinearLayout
            android:layout_weight="1"
            android:orientation="vertical">
            <Button
                android:id="@+id/scissorButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="剪刀"/>
            <Button
                android:id="@+id/stoneButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="石头"/>
            <Button
                android:id="@+id/clothButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="布" />
        </LinearLayout>
    </TableRow>
    <TextView
        android:id="@+id/resultText"
        android:text="判定输赢:"
        android:textAlignment="center" />
</TableLayout>

各个按钮的具体实现,需要使用Java代码,源文件位于WebView\app\src\main\java\com\demo\android\webview\fggActivity.java,与主入口MainActivity.java同一文件路径。具体实现如下:

package com.demo.android.webview;

import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by androiduser on 2017/8/20.
 */

public class fggActivity extends Activity {
    private TextView title, computerActionText, playerActionText,
            whatComputerActedText, resultText;
    private Button scissorButton, stoneButton, clothButton;
    private String[] fingers = new String[] {"剪刀", "石头", "布"};
    private String player_win = "恭喜,你赢了!";
    private String player_draw = "双方平手!";
    private String player_lose = "很可惜,你输了!";
    int iComPlay;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        AssetManager assets = getAssets();
        Typeface typeface = Typeface.createFromAsset(assets, "fonts/FZXKJW.TTF");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.finger_guessing_game_layout);
        title = (TextView)findViewById(R.id.title);
        title.setTypeface(typeface);
        computerActionText = (TextView)findViewById(R.id.computerActionText);
        playerActionText = (TextView)findViewById(R.id.playerActionText);
        whatComputerActedText = (TextView)findViewById(R.id.whatComputerActedText);
        resultText = (TextView)findViewById(R.id.resultText);
        scissorButton = (Button)findViewById(R.id.scissorButton);
        stoneButton = (Button)findViewById(R.id.stoneButton);
        clothButton = (Button)findViewById(R.id.clothButton);
        iComPlay = (int)(Math.random()*3 + 1);

        scissorButton.setOnClickListener(btnOnClick);
        stoneButton.setOnClickListener(btnOnClick);
        clothButton.setOnClickListener(btnOnClick);
    }

    private Button.OnClickListener btnOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Button b = (Button)view;
            String currentText = b.getText().toString();
            iComPlay = (int)(Math.random() * 3);
            whatComputerActedText.setText(fingers[iComPlay]);
            //1 - 剪刀, 2 - 石头, 3 - 布
            switch (iComPlay) {
                case 0: //剪刀
                    if (currentText.equals("剪刀"))
                        resultText.setText(player_draw);
                    if (currentText.equals("石头"))
                        resultText.setText(player_win);
                    if (currentText.equals("布"))
                        resultText.setText(player_lose);
                    break;
                case 1: //石头
                    if (currentText.equals("剪刀"))
                        resultText.setText(player_lose);
                    if (currentText.equals("石头"))
                        resultText.setText(player_draw);
                    if (currentText.equals("布"))
                        resultText.setText(player_win);
                    break;
                case 2: //布
                    if (currentText.equals("剪刀"))
                        resultText.setText(player_win);
                    if (currentText.equals("石头"))
                        resultText.setText(player_lose);
                    if (currentText.equals("布"))
                        resultText.setText(player_draw);
                    break;
            }
        }
    };
}

如果不在MainActivity,java中写上述代码,记得在MainActivity.java中编写如下代码,以显示这个界面:

	    Intent intent = new Intent();
            intent.setClass(MainActivity.this, fggActivity.class);
            startActivity(intent);

最后,还需在AndroidManifest.xml中声明如下,否则程序会报错,此代码位于application节点,与MainActivity同级:

        <activity android:name=".fggActivity"/>




猜你喜欢

转载自blog.csdn.net/weixin_38307752/article/details/77427932
今日推荐