新手在这里。我想要做的是一个基于文本的游戏,有多个级别。每个级别都有一些选项可以确定玩家是否进入下一级别。 这是主要问题:如果玩家失去一个关卡,无论什么级别,程序都必须从头开始重启。
以下是游戏的一般格式:
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.
重新开始游戏取决于变量&#34;重启&#34;是真是假,但每个级别来回切换似乎不是一种可行或有效的方法。如果有人有任何建议/想法/更好的解决方案,请尽快回复。 谢谢!
答案 0 :(得分:0)
为了使变量restart
的值对while循环下的代码块是否执行产生影响,需要计算while(此处,restart)之后的表达式。当您的代码当前已写入时,如果用户在级别1成功,则重新启动将更改为False
,然后再更改为True
,而不会影响代码块的执行。如果用户在级别1失败,则重新启动仍为True
,并且代码块继续执行相同的操作。为了再次计算while表达式(并且重新启动的值对代码块的执行产生任何影响,您需要耗尽代码块,或者让分支包括continue
,哪一点代码块的执行停止并且while表达式被评估。
在这里查看有关while循环的一些基本信息: https://www.tutorialspoint.com/python/python_while_loop.htm
答案 1 :(得分:0)
我会使用类似堆栈的结构来控制当前级别,使用字典来存储问题,答案和级别进度。目前你的游戏循环并不是一个循环,而是一个逻辑链,这会导致问题。
例如,这就是我写的:
#! /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
存储到目前为止所采用的路径。