50字范文,内容丰富有趣,生活中的好帮手!
50字范文 > 安卓练习----安卓android实现一个石头剪刀布小游戏

安卓练习----安卓android实现一个石头剪刀布小游戏

时间:2020-09-26 05:29:03

相关推荐

安卓练习----安卓android实现一个石头剪刀布小游戏

此博客用安卓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(){@Overridepublic 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() {@Overridepublic void run() {while (holderImgFlag){//do somethingtry{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();//更新uimTextViewShowNum.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);}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);init();mImageViewGetScissors.setOnClickListener(new View.OnClickListener() {@Overridepublic 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();//做判断,并更新uimButtonStartGame.setEnabled(true);}});mImageViewGetRock.setOnClickListener(new View.OnClickListener() {@Overridepublic 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();//做判断,并更新uimButtonStartGame.setEnabled(true);}});mImageViewGetPaper.setOnClickListener(new View.OnClickListener() {@Overridepublic 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();//做判断,并更新uimButtonStartGame.setEnabled(true);}});mButtonStartGame.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//无伤大雅的更新uimTextViewResultShow.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("胜");}//更新uimTextViewResultShow.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="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#8DD0F1F1"><ImageViewandroid:id="@+id/logo"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/logo"/><RelativeLayoutandroid:layout_below="@id/logo"android:id="@+id/portrait"android:layout_width="match_parent"android:layout_height="100dp"android:paddingTop="10dp"><ImageViewandroid:layout_width="80dp"android:layout_height="80dp"android:layout_marginLeft="20dp"android:src="@drawable/boy"android:layout_alignParentLeft="true"/><ImageViewandroid:layout_width="80dp"android:layout_height="80dp"android:src="@drawable/vs"android:layout_centerHorizontal="true"/><ImageViewandroid:layout_width="80dp"android:layout_height="80dp"android:layout_marginRight="20dp"android:src="@drawable/robot"android:layout_alignParentRight="true"/></RelativeLayout><RelativeLayoutandroid:layout_below="@id/portrait"android:id="@+id/goals"android:layout_marginTop="10dp"android:layout_width="match_parent"android:layout_height="90dp"><TextViewandroid: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"/><TextViewandroid: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><RelativeLayoutandroid:id="@+id/holder"android:layout_marginTop="5dp"android:layout_below="@id/goals"android:layout_width="match_parent"android:layout_height="180dp"><RelativeLayoutandroid:layout_width="180dp"android:layout_height="180dp"android:layout_alignParentLeft="true"><ImageViewandroid:id="@+id/boy_holder"android:layout_width="120dp"android:layout_height="120dp"android:layout_centerInParent="true"android:src="@drawable/holder"/><TextViewandroid: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><RelativeLayoutandroid:layout_width="180dp"android:layout_height="180dp"android:layout_alignParentRight="true"><ImageViewandroid:id="@+id/robot_holder"android:layout_width="120dp"android:layout_height="120dp"android:layout_centerInParent="true"android:src="@drawable/holder"/><TextViewandroid: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><Buttonandroid: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"/><RelativeLayoutandroid:id="@+id/bottom"android:layout_width="match_parent"android:layout_height="120dp"android:paddingTop="20dp"android:layout_alignParentBottom="true"><ImageViewandroid:id="@+id/get_scissors"android:layout_width="100dp"android:layout_height="100dp"android:src="@drawable/scissors"android:layout_alignParentLeft="true"/><ImageViewandroid:id="@+id/get_rock"android:layout_width="100dp"android:layout_height="100dp"android:src="@drawable/rock"android:layout_centerHorizontal="true"/><ImageViewandroid: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;//默认是三@Overrideprotected 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() {@Overridepublic void onClick(View v) {num = 3;mTextView.setText("最终胜局:3");}});b5.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {num = 5;mTextView.setText("最终胜局:5");}});b7.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {num = 7;mTextView.setText("最终胜局:7");}});b9.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {num = 9;mTextView.setText("最终胜局:9");}});bStart.setOnClickListener(new View.OnClickListener() {@Overridepublic 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="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg"><ImageViewandroid:id="@+id/logo"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/logo"/><LinearLayoutandroid:layout_centerInParent="true"android:layout_width="300dp"android:layout_height="300dp"android:orientation="vertical"><TextViewandroid: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"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="80dp"android:orientation="horizontal"android:layout_margin="20dp"><Buttonandroid: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"/><TextViewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><Buttonandroid: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><LinearLayoutandroid:layout_width="match_parent"android:layout_height="80dp"android:orientation="horizontal"android:layout_margin="20dp"><Buttonandroid: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"/><TextViewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"/><Buttonandroid: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><Buttonandroid: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;}}

总结

如果上面的请戳此处没有链接,不要着急,那是因为我资源还没有通过审核,我会在资源通过审核的第一时间补充上链接。码字不易,关注不迷路

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。