热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用Python策划游戏

使用Python策划游戏原文:https://www.gee

使用 Python 策划游戏

原文:https://www.geeksforgeeks.org/mastermind-game-using-python/

鉴于当代人对游戏及其高要求技术的了解,许多人渴望进一步发展和推进游戏。最终,每个人都要从头开始。摄魂师是一款由两个玩家玩的破密码老游戏。这个游戏可以追溯到 19 世纪,可以用纸和铅笔玩。

先决条件:
Python 中的随机数

游戏规则

两个玩家互相进行游戏;让我们假设玩家 1 和玩家 2。


  • 玩家 1 通过设置多位数先玩。

  • 玩家 2 现在尝试第一次猜数字。

  • 如果玩家 2 第一次尝试成功(尽管几率极小),他就赢得了游戏,并加冕为摄魂师!如果不是,那么玩家 1 通过显示玩家 2 得到的数字或数字来提示。

  • 游戏继续进行,直到玩家 2 最终能够完全猜出数字。

  • 现在玩家 2 开始设置数字,玩家 1 扮演猜数字的角色。

  • 如果玩家 1 能够在比玩家 2 更少的尝试次数内猜出数字,则玩家 1 赢得游戏并加冕为摄魂师。

  • 如果没有,那么玩家 2 赢得游戏。

  • 然而,真正的游戏已经证明了美学,因为数字是由彩色编码的按钮表示的。

例如:
输入:

Player 1, set the number: 5672
Player 2, guess the number: 1472

输出:

Not quite the number. You did get 2 digits correct.
X X 7 2
Enter your next choice of numbers:

我们将不使用任何Pygame库来帮助我们获得额外的图形,因此将只处理框架和概念。此外,我们将与计算机对战,也就是说,计算机将生成要猜测的数字。

以下是上述想法的实现。

import random
# the .randrange() function generates a
# random number within the specified range.
num = random.randrange(1000, 10000)  
n = int(input("Guess the 4 digit number:"))
# condition to test equality of the
# guess made. Program terminates if true.
if (n == num):  
    print("Great! You guessed the number in just 1 try! You're a Mastermind!")
else:
    # ctr variable initialized. It will keep count of 
    # the number of tries the Player takes to guess the number.
    ctr = 0  
    # while loop repeats as long as the 
    # Player fails to guess the number correctly.
    while (n != num):  
        # variable increments every time the loop
        # is executed, giving an idea of how many
        # guesses were made.
        ctr += 1  
        count = 0
        # explicit type conversion of an integer to
        # a string in order to ease extraction of digits
        n = str(n)  
        # explicit type conversion of a string to an integer
        num = str(num)  
        # correct[] list stores digits which are correct
        correct = ['X']*4  
        # for loop runs 4 times since the number has 4 digits.
        for i in range(0, 4): 
             # checking for equality of digits
            if (n[i] == num[i]):  
                # number of digits guessed correctly increments
                count += 1  
                # hence, the digit is stored in correct[].
                correct[i] = n[i]  
            else:
                continue
        # when not all the digits are guessed correctly.
        if (count <4) and (count != 0):  
            print("Not quite the number. But you did get ", count, " digit(s) correct!")
            print("Also these numbers in your input were correct.")
            for k in correct:
                print(k, end=' ')
            print('\n')
            print('\n')
            n = int(input("Enter your next choice of numbers: "))
        # when none of the digits are guessed correctly.
        elif (count == 0):  
            print("None of the numbers in your input match.")
            n = int(input("Enter your next choice of numbers: "))
    # condition for equality.
    if n == num:  
        print("You've become a Mastermind!")
        print("It took you only", ctr, "tries.")

让我们假设计算机设定的数字是 1564

输出:

Guess the 4 digit number: 1564
Great! You guessed the number in just 1 try! You're a Mastermind!

如果这个数字一次都猜不到。

输出:

Guess the 4 digit number: 2164
Not quite the number. But you did get 2 digit(s) correct!
Also these numbers in your input were correct.
X X 6 4
Enter your next choice of numbers: 3564
Not quite the number. But you did get 2 digit(s) correct!
Also these numbers in your input were correct.
X 5 6 4
Enter your next choice of numbers: 1564
You've become a Mastermind.
It took you only 3 tries.

您可以通过增加输入的位数或不透露输入中哪些数字被正确放置来增加游戏难度。
这已经在下面的代码中解释过了。

import random
#the .randrange() function generates
# a random number within the specified range.
num = random.randrange(1000,10000) 
n = int(input("Guess the 4 digit number:"))
# condition to test equality of the 
# guess made. Program terminates if true.
if(n == num):             
     print("Great! You guessed the number in just 1 try! You're a Mastermind!")
else:
     # ctr variable initialized. It will keep count of 
     # the number of tries the Player takes to guess the number.
     ctr = 0    
     # while loop repeats as long as the Player
     # fails to guess the number correctly.
     while(n!=num):
          # variable increments every time the loop 
          # is executed, giving an idea of how many 
          # guesses were made.
          ctr += 1             
          count = 0
          # explicit type conversion of an integer to 
          # a string in order to ease extraction of digits
          n = str(n) 
          # explicit type conversion of a string to an integer                                 
          num = str(num)
          # correct[] list stores digits which are correct 
          correct=[]        
          # for loop runs 4 times since the number has 4 digits.     
          for i in range(0,4): 
              # checking for equality of digits
              if(n[i] == num[i]): 
                  # number of digits guessed correctly increments
                  count += 1    
                  # hence, the digit is stored in correct[].
                  correct.append(n[i])     
              else:
                  continue
          # when not all the digits are guessed correctly.
          if (count <4) and (count != 0):     
              print("Not quite the number. But you did get ",count," digit(s) correct!")
              print("Also these numbers in your input were correct.")
              for k in correct:
                  print(k, end=' ')
              print('\n')
              print('\n')
              n = int(input("Enter your next choice of numbers: "))
          # when none of the digits are guessed correctly.
          elif(count == 0):         
              print("None of the numbers in your input match.")
              n=int(input("Enter your next choice of numbers: ")) 
     if n==num:                
         print("You've become a Mastermind!")
         print("It took you only",ctr,"tries.")

假设计算机设定的数字是 54876。

输出:

Guess the 5 digit number: 38476
Not quite the number. But you did get 2 digit(s) correct!
Enter your next choice of numbers: 41876
Not quite the number. But you did get 4 digit(s) correct!
Enter the next choice of numbers: 54876
Great you've become a Mastermind!
It took you only 3 tries!

修改这段代码的整个范围是巨大的。这里的想法是了解这个概念是什么。像这种依赖类似基本代码的游戏还有很多。

通过利用这段代码,进一步开发它,同时结合 Pygame 的库,将使它更像真正的交易,更不用说涉及更多了!


推荐阅读
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • 尽管使用TensorFlow和PyTorch等成熟框架可以显著降低实现递归神经网络(RNN)的门槛,但对于初学者来说,理解其底层原理至关重要。本文将引导您使用NumPy从头构建一个用于自然语言处理(NLP)的RNN模型。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • Python自动化处理:从Word文档提取内容并生成带水印的PDF
    本文介绍如何利用Python实现从特定网站下载Word文档,去除水印并添加自定义水印,最终将文档转换为PDF格式。该方法适用于批量处理和自动化需求。 ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
  • 从 .NET 转 Java 的自学之路:IO 流基础篇
    本文详细介绍了 Java 中的 IO 流,包括字节流和字符流的基本概念及其操作方式。探讨了如何处理不同类型的文件数据,并结合编码机制确保字符数据的正确读写。同时,文中还涵盖了装饰设计模式的应用,以及多种常见的 IO 操作实例。 ... [详细]
  • 毕业设计:基于机器学习与深度学习的垃圾邮件(短信)分类算法实现
    本文详细介绍了如何使用机器学习和深度学习技术对垃圾邮件和短信进行分类。内容涵盖从数据集介绍、预处理、特征提取到模型训练与评估的完整流程,并提供了具体的代码示例和实验结果。 ... [详细]
  • #点球小游戏fromrandomimportchoiceimporttimescore[0,0]direction[left,center,right]defkick() ... [详细]
  • 本文详细介绍 Go+ 编程语言中的上下文处理机制,涵盖其基本概念、关键方法及应用场景。Go+ 是一门结合了 Go 的高效工程开发特性和 Python 数据科学功能的编程语言。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 本文介绍了在Windows环境下使用pydoc工具的方法,并详细解释了如何通过命令行和浏览器查看Python内置函数的文档。此外,还提供了关于raw_input和open函数的具体用法和功能说明。 ... [详细]
  • 深入解析JMeter中的JSON提取器及其应用
    本文详细介绍了如何在JMeter中使用JSON提取器来获取和处理API响应中的数据。特别是在需要将一个接口返回的数据作为下一个接口的输入时,JSON提取器是一个非常有用的工具。 ... [详细]
author-avatar
手机用户2502870457
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有