此博客用安卓android實現了一個簡單的剪刀石頭布小遊戲。能完成人機對抗。
說明
這是我老師佈置的一道課內作業,圖片資源由老師提供,本篇博客僅展示代碼部分,圖片資源由於太多且沒有那麼必要,我就不在此展示。如需要完整代碼以及圖片資源,請戳此處
界面展示
主頁,選擇勝利需要的局數,然後進入遊戲(醜莫怪)
主界面如下,當點擊開始遊戲後,箭頭所指的紅色矩形框會抖動,意思爲等待選擇出的內容。選擇好了之後如右下圖片所示,原來不停抖動的圖片會替換成你選擇的和電腦選擇的手勢,並判斷勝負,更新比分。
代碼部分
模型有:Player.java抽象類,HumanPlayer.java,RobotPlayer.java,Judge.java,RuleInfo.java以及兩個頁面:IndexActivity.java,activity_index.xml和MainActivity.java,activity_main.xml
MainActivity.java
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.game1.model.HumanPlayer;
import com.example.game1.model.Judge;
import com.example.game1.model.RobotPlayer;
import com.example.game1.model.RuleInfo;
public class MainActivity extends Activity{
public int totalGameNumber;//記錄總勝利局數,從前一個界面獲取
private TextView mTextViewResultShow,mTextViewBoy,mTextViewRobot,mTextViewShowNum;
private Button mButtonStartGame;
private ImageView mImageViewBoyHolder,mImageViewRobotHolder,mImageViewGetScissors,mImageViewGetRock,mImageViewGetPaper;
private RobotPlayer mRobotPlayer;//定義一個機器人
private HumanPlayer mHumanPlayer;//定義一個真人玩家
private RuleInfo mRuleInfo;//定義遊戲規則,主要是記錄已經開始的遊戲局數
private boolean holderImgFlag = false;//控制holder圖片動或是不動的標誌
private int i = 10;//padding 初始值
private int[] images = {R.drawable.ps0,R.drawable.ps1,R.drawable.ps2};
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
//圖片抖動效果
if (msg.what == 0x123){
mImageViewBoyHolder.setPadding(i++ %18,i++ %20,i++ %15,i++ %12);
mImageViewRobotHolder.setPadding(i++ %18,i++ %20,i++ %15,i++ %12);
}
}
};
//線程運行體
Runnable mRunnable = new Runnable() {
@Override
public void run() {
while (holderImgFlag){
//do something
try{
mHandler.sendEmptyMessage(0x123);
Thread.sleep(50);
}catch (Exception e){
e.printStackTrace();
}
}
}
};
private void init(){
mTextViewResultShow = findViewById(R.id.show_result_text);
mTextViewShowNum = findViewById(R.id.show_num_text);
mButtonStartGame = findViewById(R.id.button_game_start);
mImageViewBoyHolder = findViewById(R.id.boy_holder);
mImageViewRobotHolder = findViewById(R.id.robot_holder);
mImageViewGetScissors = findViewById(R.id.get_scissors);
mImageViewGetRock = findViewById(R.id.get_rock);
mImageViewGetPaper = findViewById(R.id.get_paper);
mTextViewBoy = findViewById(R.id.boy_text);
mTextViewRobot = findViewById(R.id.robot_text);
totalGameNumber = getIntent().getIntExtra("num",3);
newGame();
}
private void newGame(){
prohibitTorch();//禁止點擊
mRobotPlayer = new RobotPlayer("Robot");
mHumanPlayer = new HumanPlayer("boy");
mRuleInfo = new RuleInfo();
//更新ui
mTextViewShowNum.setText("任意方累計獲勝"+totalGameNumber+"場即獲得最終勝利");
mButtonStartGame.setText("開始遊戲");
mButtonStartGame.setEnabled(true);
}
//不允許點擊剪刀石頭布三個圖片
private void prohibitTorch(){
mImageViewGetScissors.setEnabled(false);
mImageViewGetRock.setEnabled(false);
mImageViewGetPaper.setEnabled(false);
}
//允許點擊剪刀石頭布三個圖片
private void unProhibitTorch(){
mImageViewGetScissors.setEnabled(true);
mImageViewGetRock.setEnabled(true);
mImageViewGetPaper.setEnabled(true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
mImageViewGetScissors.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
prohibitTorch();//上鎖
mButtonStartGame.setText("第"+(mRuleInfo.gameNumber)+"局遊戲結束");
//圖片停止動
holderImgFlag = false;
//將真人玩家的value設置成剪刀
mHumanPlayer.setValue(RuleInfo.SCISSORS);
//將boy佔位圖片設置成剪刀
mImageViewBoyHolder.setImageResource(images[mHumanPlayer.getValue()]);
//將機器人的佔位圖片設置爲應該的樣子
mImageViewRobotHolder.setImageResource(images[mRobotPlayer.getValue()]);
doJudge();//做判斷,並更新ui
mButtonStartGame.setEnabled(true);
}
});
mImageViewGetRock.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mButtonStartGame.setText("第"+(mRuleInfo.gameNumber)+"局遊戲結束");
prohibitTorch();//上鎖
//圖片停止動
holderImgFlag = false;
//將真人玩家的value設置成石頭
mHumanPlayer.setValue(RuleInfo.ROCK);
//將boy佔位圖片設置成石頭
mImageViewBoyHolder.setImageResource(images[mHumanPlayer.getValue()]);
//將機器人的佔位圖片設置爲應該的樣子
mImageViewRobotHolder.setImageResource(images[mRobotPlayer.getValue()]);
doJudge();//做判斷,並更新ui
mButtonStartGame.setEnabled(true);
}
});
mImageViewGetPaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mButtonStartGame.setText("第"+(mRuleInfo.gameNumber)+"局遊戲結束");
prohibitTorch();//上鎖
//圖片停止動
holderImgFlag = false;
//將真人玩家的value設置成布
mHumanPlayer.setValue(RuleInfo.PAPER);
//將boy佔位圖片設置成布
mImageViewBoyHolder.setImageResource(images[mHumanPlayer.getValue()]);
//將機器人的佔位圖片設置爲應該的樣子
mImageViewRobotHolder.setImageResource(images[mRobotPlayer.getValue()]);
doJudge();//做判斷,並更新ui
mButtonStartGame.setEnabled(true);
}
});
mButtonStartGame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//無傷大雅的更新ui
mTextViewResultShow.setText(getShowResult(mHumanPlayer.getGoals(),mRobotPlayer.getGoals()));
//佔位圖片開始動
holderImgFlag = true;
new Thread(mRunnable).start();
unProhibitTorch();//解鎖
mImageViewBoyHolder.setImageResource(R.drawable.holder);
//將機器人的佔位圖片設置爲應該的樣子
mImageViewRobotHolder.setImageResource(R.drawable.holder);
//遊戲局數加一
int num = mRuleInfo.addGameNumber();
mButtonStartGame.setText("第"+(num)+"局遊戲開始");
//機器玩家開始玩遊戲
mRobotPlayer.robotAutoPlay();
mButtonStartGame.setEnabled(false);
}
});
}
private String getShowResult(int a,int b){
return a+" : "+b;
}
private void doJudge(){
int a = Judge.judge(mHumanPlayer,mRobotPlayer);
if (a == 0){
//平手
mTextViewBoy.setText("平");
mTextViewRobot.setText("平");
}else if (a == -1){
//真人勝利
mTextViewBoy.setText("勝");
mTextViewRobot.setText("負");
}else {
mTextViewBoy.setText("負");
mTextViewRobot.setText("勝");
}
//更新ui
mTextViewResultShow.setText(getShowResult(mHumanPlayer.getGoals(),mRobotPlayer.getGoals()));
if (mRobotPlayer.goals == totalGameNumber){
Toast.makeText(MainActivity.this,"遊戲結束,機器人獲勝!",Toast.LENGTH_LONG).show();
newGame();
}
if (mHumanPlayer.goals == totalGameNumber) {
Toast.makeText(MainActivity.this,"遊戲結束,男孩獲勝!",Toast.LENGTH_LONG).show();
newGame();
}
}
public static Intent newIntent(Context context, int num){
Intent intent = new Intent(context,MainActivity.class);
intent.putExtra("num",num);
return intent;
}
}
activity_mian.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8DD0F1F1"
>
<ImageView
android:id="@+id/logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/logo"/>
<RelativeLayout
android:layout_below="@id/logo"
android:id="@+id/portrait"
android:layout_width="match_parent"
android:layout_height="100dp"
android:paddingTop="10dp">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="20dp"
android:src="@drawable/boy"
android:layout_alignParentLeft="true"
/>
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/vs"
android:layout_centerHorizontal="true"/>
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginRight="20dp"
android:src="@drawable/robot"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_below="@id/portrait"
android:id="@+id/goals"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="90dp">
<TextView
android:id="@+id/show_result_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="0 : 0"
android:textSize="50dp"/>
<TextView
android:id="@+id/show_num_text"
android:layout_below="@id/show_result_text"
android:paddingTop="5dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="累計獲勝X場即獲得最終勝利"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/holder"
android:layout_marginTop="5dp"
android:layout_below="@id/goals"
android:layout_width="match_parent"
android:layout_height="180dp">
<RelativeLayout
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_alignParentLeft="true">
<ImageView
android:id="@+id/boy_holder"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_centerInParent="true"
android:src="@drawable/holder"/>
<TextView
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="@+id/boy_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="結果"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_alignParentRight="true">
<ImageView
android:id="@+id/robot_holder"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_centerInParent="true"
android:src="@drawable/holder"/>
<TextView
android:id="@+id/robot_text"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="結果"/>
</RelativeLayout>
</RelativeLayout>
<Button
android:id="@+id/button_game_start"
android:layout_below="@id/holder"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="20dp"
android:text="開始遊戲"
android:textSize="25dp"
android:background="#C6EED7"/>
<RelativeLayout
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="120dp"
android:paddingTop="20dp"
android:layout_alignParentBottom="true">
<ImageView
android:id="@+id/get_scissors"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/scissors"
android:layout_alignParentLeft="true"
/>
<ImageView
android:id="@+id/get_rock"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/rock"
android:layout_centerHorizontal="true"/>
<ImageView
android:id="@+id/get_paper"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/paper"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</RelativeLayout>
IndexActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class IndexActivity extends Activity {
private Button b3,b5,b7,b9,bStart;
private TextView mTextView;
private int num = 3;//默認是三
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
b3 = findViewById(R.id.button3);
b5 = findViewById(R.id.button5);
b7 = findViewById(R.id.button7);
b9 = findViewById(R.id.button9);
bStart = findViewById(R.id.button_start);
mTextView = findViewById(R.id.num_text);
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num = 3;
mTextView.setText("最終勝局:3");
}
});
b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num = 5;
mTextView.setText("最終勝局:5");
}
});
b7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num = 7;
mTextView.setText("最終勝局:7");
}
});
b9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num = 9;
mTextView.setText("最終勝局:9");
}
});
bStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = MainActivity.newIntent(IndexActivity.this,num);
startActivity(intent);
}
});
}
}
activity_index.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
>
<ImageView
android:id="@+id/logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/logo"/>
<LinearLayout
android:layout_centerInParent="true"
android:layout_width="300dp"
android:layout_height="300dp"
android:orientation="vertical">
<TextView
android:id="@+id/num_text"
android:layout_width="match_parent"
android:layout_height="70dp"
android:textSize="40dp"
android:text="請選擇最終勝局"
android:gravity="center"
android:background="#9A382E1A"
android:textColor="#F1F1F1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:layout_margin="20dp">
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:text="3"
android:textSize="30dp"
android:background="#BA94DCF5"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:text="5"
android:textSize="30dp"
android:background="#BA94DCF5"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:layout_margin="20dp">
<Button
android:id="@+id/button7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:text="7"
android:textSize="30dp"
android:background="#BA94DCF5"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="@+id/button9"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:text="9"
android:textSize="30dp"
android:background="#BA94DCF5"/>
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/button_start"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_margin="40dp"
android:text="開始遊戲"
android:textSize="35dp"
android:background="#ECD41D"/>
</RelativeLayout>
Player.java
public class Player {
public String name;
public int goals;
public int value;
public Player() {
}
public Player(String name) {
this.name = name;
this.goals = 0;
this.value = 0;
}
public Player(String name, int goals, int value) {
this.name = name;
this.goals = goals;
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGoals() {
return goals;
}
public void setGoals(int goals) {
this.goals = goals;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
HumanPlayer.java
public class HumanPlayer extends Player {
public HumanPlayer() {
}
public HumanPlayer(String name) {
super(name);
}
public HumanPlayer(String name, int goals, int value) {
super(name, goals, value);
}
}
RobotPlayer.java
public class RobotPlayer extends Player {
public RobotPlayer() {
}
public RobotPlayer(String name) {
super(name);
}
public RobotPlayer(String name, int goals, int value) {
super(name, goals, value);
}
//機器人自動划拳的方法
public void robotAutoPlay(){
value = (int)(Math.random()*3);
}
}
Judge.java
public class Judge {
public static int judge(Player p1,Player p2) {
if(p1.value-p2.value==1||p1.value-p2.value==-2){
p1.goals++;
return -1;
}else if(p1.value-p2.value==-1||p1.value-p2.value==2){
p2.goals++;
return 1;
}else{
return 0;
}
}
}
RuleInfo.java
/**
* 記錄遊戲規則
* */
public class RuleInfo {
public final static int SCISSORS = 0;//剪刀
public final static int ROCK = 1;//石頭
public final static int PAPER = 2;//布
public int gameNumber; //已經開始的局數
public int addGameNumber(){
this.gameNumber++;
return gameNumber;
}
public RuleInfo() { }
public int getGameNumber() {
return gameNumber;
}
public void setGameNumber(int gameNumber) {
this.gameNumber = gameNumber;
}
}
總結
如果上面的請戳此處沒有鏈接,不要着急,那是因爲我資源還沒有通過審覈,我會在資源通過審覈的第一時間補充上鍊接。碼字不易,關注不迷路