繁体   English   中英

Python游戏机制:重启程序

[英]Python game mechanics: restarting program

新手在这里。 我想要做的是一个具有多个级别的基于文本的游戏。 每个级别都有一些选项可以确定玩家是否进入下一级别。 这是主要问题:如果玩家失去一个等级,无论什么等级,程序都必须从头开始重启。

这是游戏的一般格式:

restart = True
while restart:
    print "Level 1"
    x = input("Question:...>ans1< >ans2< >ans3<")  #assume ans1 is always correct
    if x != ans1:
        print "GAME OVER"
    else:
        print "Continue"
        restart = False
    restart = True
    print "Level 2"
    x = input("Question:...>ans1< >ans2< >ans3<") 
    if x != ans1:
        print "GAME OVER"
    else:
        print "Continue"
        restart = False  #etc.

重新启动游戏取决于变量“restart”是否为真,但是每个级别来回切换似乎不是一种可行或有效的方法。 如果有人有任何建议/想法/更好的解决方案,请尽快回复。 谢谢!

为了使变量restart的值对while循环下的代码块是否执行产生影响,需要计算while(here,restart)之后的表达式。 当您的代码当前已写入时,如果用户在级别1成功,则重新启动将更改为False ,然后再返回True ,而不会影响代码块的执行。 如果用户在级别1失败,则重新启动保持为True ,并且代码块继续执行相同的操作。 为了再次计算while表达式(并且重新启动的值对代码块的执行产生任何影响,您需要耗尽代码块,或者让分支包括continue ,此时执行代码块停止并计算while表达式。

看看这里有关while循环的一些基本信息: https//www.tutorialspoint.com/python/python_while_loop.htm

我会使用像堆栈这样的结构来控制当前级别,使用字典来存储问题,答案和级别进展。 目前你的游戏循环不是作为一个循环运行,而是一个逻辑链,这会导致问题。

例如,这就是我写的:

#! /usr/bin/env python3

from enum import Enum

class Commands( Enum ):
   Traverse = "go to"
   Restart = "restart"
   QuitGame = "quit"
   Backtrack = "backtrack"

def main( **kwargs ):
   debug = kwargs.get( "debug", False )

   # Answer option value:
   # Value 0: operation
   # Value 1: parameter

   # Operations:
   # "go to": traverse to level <parameter>
   # "restart": restart the game
   # "quit": quit the game
   # "backtrack": backtrack <parameter> levels

   beginning_level = 0
   level_options = { beginning_level: ( "question 0", { "go to 1": ( Commands.Traverse, 1 ), "go to 2": ( Commands.Traverse, 2 ), "restart": ( Commands.Restart, 0 ), "quit": ( Commands.QuitGame, 0 ), } ), 
                 1: ( "question 1", { "go to 2": ( Commands.Traverse, 2 ), "back 1": ( Commands.Backtrack, 1 ), "restart": ( Commands.Restart, 0 ), "quit": ( Commands.QuitGame, 0 ), } ), 
                 2: ( "question 2", { "back 1": ( Commands.Backtrack, 1 ), "restart": ( Commands.Restart, 0 ), "quit": ( Commands.QuitGame, 0 ), } ),
               } # clearly more could go here.

   try:
      level_stack = []
      quit = False

      while not quit:
         if not level_stack:
            level_stack.append( beginning_level )

         current_level = level_stack[ -1 ]
         level_question, options = level_options[ current_level ]

         print( "Level {:d}:".format( current_level ) )
         answer = input( "{}: {}> ".format( level_question, ", ".join( sorted( options ) ) ) )

         answer = answer.strip() # Be forgiving of leading and trailing whitespace.
         if answer in options:
            command, parameter = options[ answer ]

            if command == Commands.Traverse:
               level_stack.append( parameter )
               action_message = "Traversing to level {:d}.".format( parameter )
            elif command == Commands.Backtrack:
               for i in range( parameter ):
                  if level_stack:
                     level_stack.pop()
                  else:
                     break

               action_message = "Back-tracking {:d} step(s).".format( parameter )
            elif command == Commands.Restart:
               level_stack = []
               action_message = "Restarting game."
            elif command == Commands.QuitGame:
               quit = True
               action_message = "Quitting game."
            else:
               raise Exception( "Unsupported command: {}".format( command ) )

            if debug:
               print( action_message )
         else:
            print( "Invalid response: please try again." )
   except Exception as e:
      result = 1
      print( "Error {}".format( str( e ) ) )
   else:
      result = 0

   return result

if __name__ == "__main__":
   import sys
   sys.exit( main() )

level_options存储游戏进程选项, level_stack存储迄今为止所采用的路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM