美文网首页
Python随机数实现简单猜拳游戏

Python随机数实现简单猜拳游戏

作者: 牛小妞 | 来源:发表于2020-06-02 15:20 被阅读0次
规则:

1、玩家player输入剪刀/石头/布
2、电脑computer自动产生剪刀/石头/布
3、系统判断玩家赢还是电脑赢,产生结果player赢了还是computer赢了

首先引入随机数

要在所有代码的最上方引入哦~

#引入随机数
import random
第二步,生成两个变量player computer

player输入剪刀/石头/布,这里用0代表石头,1代表剪刀,2代表布
computer随机

#玩家
player = int(input('请输入:0--石头;1--剪刀;v2 - -布'))
#电脑
computer = random.randint(0,2)

注意:random.randint(0,2)括号中的数字是范围,在这里是随机出0到2,也就是0,1,2的意思

第三步,判断输赢

先判断玩家赢的情况

玩家 电脑
石头--0 剪刀--1
剪刀--1 布--2
布--2 石头--0
#玩家赢
if (player == 0 and computer == 1) or (player == 1 and computer == 2) or (player == 2 and computer == 0):
    print('玩家赢了')

再判断电脑赢的情况

玩家 电脑
石头--0 布--2
剪刀--1 石头--0
布--2 剪刀--1
#电脑赢
if(player == 0 and computer == 2) or (player == 1 and computer == 0) or (player ==2 and computer ==1):
    print('电脑赢啦')

再判断平局的情况

玩家 电脑
石头--0 石头--0
剪刀--1 剪刀--1
布--2 布--2
#平局
if(player == 0 and computer ==0) or (player ==1 and computer ==1) or (player ==2 and computer ==2):
    print('平局啦,再来一局吧!')

这样就实现了用随机数实现简单的猜拳游戏,最后放上整个代码方便复制使用。

#引入随机数
import random

#玩家
player = int(input('请输入:0--石头;1--剪刀;v2 - -布'))

#电脑
computer = random.randint(0,2)

#判断输赢
#玩家赢
if (player == 0 and computer == 1) or (player == 1 and computer == 2) or (player == 2 and computer == 0):
    print('玩家赢了')

#电脑赢
if(player == 0 and computer == 2) or (player == 1 and computer == 0) or (player ==2 and computer ==1):
    print('电脑赢啦')

#平局
if(player == 0 and computer ==0) or (player ==1 and computer ==1) or (player ==2 and computer ==2):
    print('平局啦,再来一局吧!')

相关文章

  • Python随机数实现简单猜拳游戏

    规则: 1、玩家player输入剪刀/石头/布2、电脑computer自动产生剪刀/石头/布3、系统判断玩家赢还是...

  • python随机数小游戏

    这个是转载深入理解随机数的 !/usr/bin/python -- coding:utf-8 -- 随机数字小游戏...

  • 【13】python--猜拳游戏

    '需求分析 参与游戏的角色1,玩家 手动出拳2.电脑 随机出拳(1固定出拳) 判断输赢1.玩家获胜2.平局3...

  • 适合新手练手的三个python简单小游戏

    学Python之前我们先来几个简单的小游戏练练手,这三个小游戏一个比一个复杂,建议新手慢慢来:1.猜拳: 2.数字...

  • 随机数搞不明白?不存在的!

    python随机数 Python定义了一组用于生成或操作随机数的函数。这种特殊类型的函数用于许多游戏、彩票或任何需...

  • Python猜数小游戏

    今天给大家带来一个Python猜数小游戏,游戏十分简单,系统自动生成一个随机数,由玩家猜数,系统提示偏大或偏小,直...

  • 猜拳游戏

    我妈突然就走不动了,她的双腿无力,站也站不起来,手掐上大腿一点反应也没有。 我望着对面的山,寻思着哪座山头草多些,...

  • 猜拳游戏

    我记得小时候,有一天爸爸说单位要出去旅游,每位员工可以带一个孩子。那时候出去旅游可是新鲜事物,极富吸引力,自然我和...

  • 001-python脚本:猜拳游戏

    import random player = int (input ("请输入您要出的拳 石头(1)/剪刀(2)/...

  • 用python制作猜拳小游戏

    首先我们先打开ubuntu的终端,然后新建一个文件,比如说caiquan.py,进入到vim编辑里面。因为你要和电...

网友评论

      本文标题:Python随机数实现简单猜拳游戏

      本文链接:https://www.haomeiwen.com/subject/ckpuzhtx.html