python石头剪刀布游戏_在python3中构建剪刀石头布游戏

本文介绍了如何使用Python3构建经典的石头剪刀布游戏。通过翻译自Medium的文章,详细讲解了游戏的实现过程。
摘要由CSDN通过智能技术生成

python石头剪刀布游戏

In this article, I will be showing you step by step how to make a full python rock-paper-scissor game easily. I won’t be using any hard ways so even beginners can understand how it works.

在本文中,我将逐步向您展示如何轻松制作完整的python石头剪刀布游戏。 我不会使用任何困难的方法,因此即使是初学者也可以理解它的工作原理。

下面是程序的外观: (Here’s how the program will look at end:)

To check out the full code in advance:

要提前查看完整代码,请执行以下操作:

Here the link the the code

这里的链接代码

Image for post

让我们开始吧! (Lets get Started!)

So let’s import all the things that we will need. And that will be:

因此,让我们导入我们需要的所有东西。 那将是:

步骤1:导入所有必要的文件 (Step 1: Import all necessary files)

  • Randint from random module

    来自随机模块的Randint
  • Sleep from time module

    从时间模块Hibernate
Image for post

You will know later what each of this will be used for later on.

稍后您将知道这些功能将用于以后的工作。

步骤2:建立游戏变数 (Step 2: Create game variables)

So first, we must give the player a choice of what to choose from Paper, Rock or Scissor.

因此,首先,我们必须让玩家选择“纸”,“石头”或“剪刀”。

For that let’s make a list called choices to store that for easier access to game variables.

为此,让我们创建一个名为choices的列表以存储该列表,以便更轻松地访问游戏变量。

Also two more variables which will hold the value of who will win the game

还有两个变量将保留谁将赢得比赛的价值

这三个变量是最重要的,将作为游戏的结果持有者 (These three variables are the most important and will act as the result holder of the game)

Image for post

步骤3:设定输入/控制 (Step 3: Set Your inputs/controls)

在本教程中,我们将用户称为p1,将对手称为p2。 (Let’s call user as p1 and opponent as p2 through out the tutorial.)

So the user(p1) will be able to select what to choose from list choices and the opponent(p2)’s choice will be random. I.e why we had imported randint from random module earlier.

因此,用户(p1)将能够从列表选项中选择要选择的内容,而对手(p2)的选择将是随机的。 即为什么我们早些时候从random模块中导入了randint。

因此,让我们将所有这些放到一个循环中,这样,除非其中一位玩家获胜,否则游戏不会结束。 这是为了防止平局比赛。 (So let’s put all of this inside a loop, so unless one of the players win, the game won’t end. This is to prevent a draw match.)

稍后再详细介绍 (I’ll show you that in detail later)

Image for post

注意:p1choice和p2choice是数字,而p1choice_name和p2choice_name是从我们之前做出的“选择”列表的索引中选取的字符串。 记得? (Note: p1choice & p2choice are numbers and p1choice_name & p2choice_name are strings of choices taken from index of the “choices’ list we made earlier. Remember?)

Image for post

Remember the ‘sleep' function from 'time' we imported at first? For those who don’t know, it will pause the game for 1 sec. Since Sleep takes argument in seconds.

还记得我们最初导入的“时间”中的“睡眠”功能吗? 对于不认识的人,它将暂停游戏1秒钟。 由于睡眠在几秒钟内就引起争论。

步骤4:设定游戏条件 (Step 4: Set game conditions)

棘手的部分到了。 首先让我解释一下它将如何工作,然后再开始。 (Here comes the tricky part. First let me explain how it’ll work and then start it.)

仔细阅读以了解整个概念。 (Read it carefully to understand the whole concept.)

So the list choices looks like this right:

因此,列表选项如下所示:

选择= [“纸张”,“剪刀”,“摇滚”] (choices = [“Paper”,”Scissor”,”Rock”])

Notice how it’s going left to right?

请注意它是如何从左向右移动的吗?

Paper beats Scissor which beats Rock.

纸击败剪刀,而击败剪刀。

So if p1 chose paper(1) and p2 chose scissor(2), p2 who has higher number wins.

因此,如果p1选择paper(1),而p2选择剪刀(2),则数字较高的p2获胜。

让我们做第一件事。 (Let’s do that first thing.)

Image for post

如果他们两个都选择相同怎么办? 这就是循环的正确选择吗? (Also what if both of them chooses the same? Then that’s what the loop is for right?)

Image for post

但是这些仅在假设两者的差为1的情况下才有效 (But these will work only suppose both of them has a difference of 1)

Example, p1 chose 1 and p2 chose 2

例如,p1选择1,p2选择2

此处:abs(1-2)= abs(-1)= 1 (Here: abs(1–2) = abs(-1) = 1)

But what if p1 chose 3 and p2 chose 1

但是如果p1选择3且p2选择1怎么办

此处:abs(3-1)= abs(2)= 2 (Here: abs(3–1) = abs(2) = 2)

For that, let’s go back to choices

为此,让我们回到选择

即选择= [“纸张”,“剪刀”,“ Rock”] (I.e choices = [“Paper”,”Scissor”,”Rock”])

Now Rock is 3 and paper is 1 but paper beats Rock, so in this case who has lower index wins!

现在Rock的得分为3,纸张的得分为1,但Paper击败了Rock,因此在这种情况下,谁的指数更低!

让我们将其放入我们的代码中 (Lets put that into our code)

Image for post

步骤5:结束! (Step 5: The end!)

Image for post

It’s simple no need to explain. Just see who won from the p1win or p2win variable and print the text.

很简单,无需解释。 只要看看谁从p1win或p2win变量中获胜,然后打印文本即可。

最后的想法 (Final thoughts)

如果您对现在的游戏不满意,如果我是您,那我就不会满意, (If you Aren’t satisfied with the game as it is now, which if I was you I wouldn’t be,)

Then I am thinking of updating it and then add more features like

然后我正在考虑对其进行更新,然后添加更多功能,例如

  • Multiplayer maybe?

    多人游戏?
  • Or Score counter? That’ll be good

    还是分数计数器? 那会很好
  • Or maybe make survival mode, or add money system, or add graphics.

    或者,可以选择生存模式,或者增加金钱系统,或者增加图形。

基本上您想到的任何东西,或其他类似这样的简单项目构想, (Basically anything that comes to your mind, or any other simple projects ideas like this,)

You can surprise me on:

您可以使我感到惊讶:

Instagram @shubham_tw914

Instagram@ shubham_tw914

Facebook Page @Python_Web_Hub

Facebook页面@Python_Web_Hub

Pinterest: @shubham_tw914

Pinterest:@ shubham_tw914

也, (Also,)

If you want me to help you with your python programming, check out my online service here.

如果您想让我帮助您进行python编程, 请在此处查看我的在线服务

更多类似这样的文章: (More articles like this:)

Medium Shubham Tiwary

中型Shubham Tiwary

更多类似的项目: (More projects like this:)

GitHub: @shubhamtiwary914

GitHub:@ shubhamtiwary914

翻译自: https://medium.com/@shubhamtiwary914/building-a-rock-paper-scissors-game-in-python3-176467c8f976

python石头剪刀布游戏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值