📜  如何在 Android Studio 中构建石头剪刀布游戏?

📅  最后修改于: 2022-05-13 01:55:40.132000             🧑  作者: Mango

如何在 Android Studio 中构建石头剪刀布游戏?

剪刀石头布(也称为剪刀石头布)是一种手游,在两个人之间进行,其中每个玩家同时形成三种形状中的一种。比赛的获胜者将根据以下规则决定:

  • Rock vs Paper -> Paper 获胜。
  • Rock vs Scissor -> Rock 获胜。
  • 纸 vs 剪刀 -> 剪刀获胜。

在这个游戏中,将要求用户根据用户和计算机的选择做出选择,然后结果将与计算机和用户的选择一起显示。在本文中,我们将使用 KotlinXML在 Android Studio 中构建 Rock Paper Scissor Game。

分步实施

第 1 步:创建一个新项目

要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。请注意,选择Kotlin作为编程语言。

步骤 2:使用 activity_main.xml 文件



导航到app > res > layout > activity_main.xml并将以下代码添加到该文件中。下面是activity_main.xml文件的代码。

XML


  
    
  
    
  
    
  
    
  
    
  
    
  
    
  
        
  
        
  
        
  
    
  
  
    
  
        
  
        
  
    
  
  
    
  
        
  
        
  
    
  
    
  
        


Kotlin
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.view.*
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        restart_btn.setOnClickListener {
            clear_score()
        }
  
        // when player click on scissor icon.
        scissors_btn.setOnClickListener {
  
            // set the image of user move to scissors
            user_move_img.setImageDrawable(getResources().getDrawable(R.drawable.scissors));
  
            // choose a random number between 1 to 3.
            val computer_move = (1..4).random() // 4 is not included.
              
            // 1 denotes "Rock"
            // if value of computer move is 1 it means computer has chosen Rock
  
            // 2 denotes "Paper"
            // if value of computer move is 2 it means computer has chosen paper
  
            // 3 denotes "Scissors"
            // if value of computer move is 1 it means computer has chosen Scissors
  
            if (computer_move == 1) {
                // set the image of computer move to rock
                computer_move_img.setImageDrawable(getResources().getDrawable(R.drawable.rock));
  
                // rock beats scissors.
                winner_tv.text = "Computer has won"
  
                // increase the computer score
                val cscore: Int = computer_score.text.toString().toInt() + 1
                computer_score.text = cscore.toString()
            } else if (computer_move == 2) {
  
                // set the image of computer move to paper
                computer_move_img.setImageDrawable(getResources().getDrawable(R.drawable.paper));
  
                // scissors beats paper
                winner_tv.text = "Player has won"
  
                // increase the player score
                val pscore: Int = player_score.text.toString().toInt() + 1
                player_score.text = pscore.toString()
            } else {
  
                // set the image of computer move to scissors
                computer_move_img.setImageDrawable(getResources().getDrawable(R.drawable.scissors));
  
                // both user move and computer move are "scissors"
                winner_tv.text = "Draw"
            }
  
        }
  
        // when player clicks on  paper icon
        paper_btn.setOnClickListener {
  
            // set the image of player move to paper
            user_move_img.setImageDrawable(getResources().getDrawable(R.drawable.paper));
  
  
            val computer_move = (1..4).random()
              
             // 1 denotes "Rock"
            // if value of computer move is 1 it means computer has chosen Rock
  
            // 2 denotes "Paper"
            // if value of computer move is 2 it means computer has chosen paper
  
            // 3 denotes "Scissors"
            // if value of computer move is 1 it means computer has chosen Scissors
  
            if (computer_move == 1) {
  
                // set the image of computer move to rock
                computer_move_img.setImageDrawable(getResources().getDrawable(R.drawable.rock));
  
                // paper beats rock
                winner_tv.text = "Player has won"
  
                // increase count of player score
                val pscore: Int = player_score.text.toString().toInt() + 1
                player_score.text = pscore.toString()
            }
            else if (computer_move == 2) {
  
                // set the image of computer move to paper
                computer_move_img.setImageDrawable(getResources().getDrawable(R.drawable.paper));
  
                // both user move and computer move are "paper"
                winner_tv.text = "Draw"
            } else {
  
                // set the image of computer move to scissors
  
                computer_move_img.setImageDrawable(getResources().getDrawable(R.drawable.scissors));
                // scissors beats paper
                winner_tv.text = "Computer has won"
  
                // increase the computer score
                val cscore: Int = computer_score.text.toString().toInt() + 1
                computer_score.text = cscore.toString()
            }
  
        }
  
        // when player click on rock icon.
        rock_btn.setOnClickListener {
  
            // set the image of user move to rock
            user_move_img.setImageDrawable(getResources().getDrawable(R.drawable.rock));
  
  
            val computer_move = (1..4).random()
              
            // 1 denotes "Rock"
            // if value of computer move is 1 it means computer has chosen Rock
  
            // 2 denotes "Paper"
            // if value of computer move is 2 it means computer has chosen paper
  
            // 3 denotes "Scissors"
            // if value of computer move is 1 it means computer has chosen Scissors
  
            if (computer_move == 1) {
                // set the image of computer move to rock
                computer_move_img.setImageDrawable(getResources().getDrawable(R.drawable.rock));
  
                // both user  and computer moves are rock
                winner_tv.text = "Draw"
  
            } else if (computer_move == 2) {
  
                // set the image of computer move to paper
                computer_move_img.setImageDrawable(getResources().getDrawable(R.drawable.paper));
  
                // paper beats rock
                winner_tv.text = "Computer has won"
  
                // increase the computer score
                val cscore: Int = computer_score.text.toString().toInt() + 1
                computer_score.text = cscore.toString()
  
  
            } else {
                // set the image of computer move to scissors
                computer_move_img.setImageDrawable(getResources().getDrawable(R.drawable.scissors));
  
                // rock beats scissors
                winner_tv.text = "Player has won"
  
                // increase the count of plyer
                val pscore: Int = player_score.text.toString().toInt() + 1
                player_score.text = pscore.toString()
            }
        }
    }
  
    private fun clear_score() {
  
        // set the computer and player score to 0
        computer_score.text = "0"
        player_score.text = "0"
  
        winner_tv.text = ""
  
        // set the images of computer move and user move to "Question mark image".
        user_move_img.setImageDrawable(getResources().getDrawable(R.drawable.question_mark));
        computer_move_img.setImageDrawable(getResources().getDrawable(R.drawable.question_mark));
  
    }
  
}


写了这么多代码后,我们的 UI 看起来是这样的:

步骤 3:使用 MainActivity.kt 文件

转到MainActivity.kt文件并参考以下代码。下面是MainActivity.kt文件的代码。代码中添加了注释以更详细地理解代码。

科特林

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.view.*
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        restart_btn.setOnClickListener {
            clear_score()
        }
  
        // when player click on scissor icon.
        scissors_btn.setOnClickListener {
  
            // set the image of user move to scissors
            user_move_img.setImageDrawable(getResources().getDrawable(R.drawable.scissors));
  
            // choose a random number between 1 to 3.
            val computer_move = (1..4).random() // 4 is not included.
              
            // 1 denotes "Rock"
            // if value of computer move is 1 it means computer has chosen Rock
  
            // 2 denotes "Paper"
            // if value of computer move is 2 it means computer has chosen paper
  
            // 3 denotes "Scissors"
            // if value of computer move is 1 it means computer has chosen Scissors
  
            if (computer_move == 1) {
                // set the image of computer move to rock
                computer_move_img.setImageDrawable(getResources().getDrawable(R.drawable.rock));
  
                // rock beats scissors.
                winner_tv.text = "Computer has won"
  
                // increase the computer score
                val cscore: Int = computer_score.text.toString().toInt() + 1
                computer_score.text = cscore.toString()
            } else if (computer_move == 2) {
  
                // set the image of computer move to paper
                computer_move_img.setImageDrawable(getResources().getDrawable(R.drawable.paper));
  
                // scissors beats paper
                winner_tv.text = "Player has won"
  
                // increase the player score
                val pscore: Int = player_score.text.toString().toInt() + 1
                player_score.text = pscore.toString()
            } else {
  
                // set the image of computer move to scissors
                computer_move_img.setImageDrawable(getResources().getDrawable(R.drawable.scissors));
  
                // both user move and computer move are "scissors"
                winner_tv.text = "Draw"
            }
  
        }
  
        // when player clicks on  paper icon
        paper_btn.setOnClickListener {
  
            // set the image of player move to paper
            user_move_img.setImageDrawable(getResources().getDrawable(R.drawable.paper));
  
  
            val computer_move = (1..4).random()
              
             // 1 denotes "Rock"
            // if value of computer move is 1 it means computer has chosen Rock
  
            // 2 denotes "Paper"
            // if value of computer move is 2 it means computer has chosen paper
  
            // 3 denotes "Scissors"
            // if value of computer move is 1 it means computer has chosen Scissors
  
            if (computer_move == 1) {
  
                // set the image of computer move to rock
                computer_move_img.setImageDrawable(getResources().getDrawable(R.drawable.rock));
  
                // paper beats rock
                winner_tv.text = "Player has won"
  
                // increase count of player score
                val pscore: Int = player_score.text.toString().toInt() + 1
                player_score.text = pscore.toString()
            }
            else if (computer_move == 2) {
  
                // set the image of computer move to paper
                computer_move_img.setImageDrawable(getResources().getDrawable(R.drawable.paper));
  
                // both user move and computer move are "paper"
                winner_tv.text = "Draw"
            } else {
  
                // set the image of computer move to scissors
  
                computer_move_img.setImageDrawable(getResources().getDrawable(R.drawable.scissors));
                // scissors beats paper
                winner_tv.text = "Computer has won"
  
                // increase the computer score
                val cscore: Int = computer_score.text.toString().toInt() + 1
                computer_score.text = cscore.toString()
            }
  
        }
  
        // when player click on rock icon.
        rock_btn.setOnClickListener {
  
            // set the image of user move to rock
            user_move_img.setImageDrawable(getResources().getDrawable(R.drawable.rock));
  
  
            val computer_move = (1..4).random()
              
            // 1 denotes "Rock"
            // if value of computer move is 1 it means computer has chosen Rock
  
            // 2 denotes "Paper"
            // if value of computer move is 2 it means computer has chosen paper
  
            // 3 denotes "Scissors"
            // if value of computer move is 1 it means computer has chosen Scissors
  
            if (computer_move == 1) {
                // set the image of computer move to rock
                computer_move_img.setImageDrawable(getResources().getDrawable(R.drawable.rock));
  
                // both user  and computer moves are rock
                winner_tv.text = "Draw"
  
            } else if (computer_move == 2) {
  
                // set the image of computer move to paper
                computer_move_img.setImageDrawable(getResources().getDrawable(R.drawable.paper));
  
                // paper beats rock
                winner_tv.text = "Computer has won"
  
                // increase the computer score
                val cscore: Int = computer_score.text.toString().toInt() + 1
                computer_score.text = cscore.toString()
  
  
            } else {
                // set the image of computer move to scissors
                computer_move_img.setImageDrawable(getResources().getDrawable(R.drawable.scissors));
  
                // rock beats scissors
                winner_tv.text = "Player has won"
  
                // increase the count of plyer
                val pscore: Int = player_score.text.toString().toInt() + 1
                player_score.text = pscore.toString()
            }
        }
    }
  
    private fun clear_score() {
  
        // set the computer and player score to 0
        computer_score.text = "0"
        player_score.text = "0"
  
        winner_tv.text = ""
  
        // set the images of computer move and user move to "Question mark image".
        user_move_img.setImageDrawable(getResources().getDrawable(R.drawable.question_mark));
        computer_move_img.setImageDrawable(getResources().getDrawable(R.drawable.question_mark));
  
    }
  
}

输出:现在运行您的应用程序并查看应用程序的输出。

从这里获取完整的项目。

想要一个更快节奏和更具竞争力的环境来学习 Android 的基础知识吗?
单击此处前往由我们的专家精心策划的指南,旨在让您立即做好行业准备!