当玩家死亡时退出游戏结束
Quitting to Game Over when player dies
所以,我正在开发一款 python 基于文本的游戏,我可以与朋友分享。我已经完成了大部分游戏的工作,但是当用户选择某些命令时,我正在为游戏结束部分而苦苦挣扎。我没有为此使用 pygame,因为我找不到 64 位版本。下面是我正在看的。我要在 gameOver 函数中输入什么才能真正退出游戏,或者如果玩家想要,再试一次?
import time
import random
import sys
def searchAreas():
print("1. Area1")
print("2. Area2")
print("3. Area3")
print("4. Just give up")
def badResponse():
print("Sorry, I don't understand " + search_area)
def gameOver():
print("You decide to give up because life is too hard.")
print("Would you like to try again?")
player_choice = input("> ")
if player_choice == "Yes":
mainGame()
elif player_choice == "No":
print("# what goes here to quit the game?")
else:
badResponse()
def mainGame():
search_area_options = ["1","2","3"]
search_area = ""
while search_area not in search_area_options:
print("Where do you want to start looking?")
searchAreas()
search_area = str(input("> "))
if search_area == "1":
print("Text here")
elif search_area == "2":
print("Text here")
elif search_area == "3":
print("text here")
elif search_area == "4":
gameOver()
else:
badResponse()
mainGame()
当输入除四个选项之外的任何内容时,或者当进入 gameOver 功能时,我看到此错误:
Traceback (most recent call last):
File "./test.py", line 45, in <module>
mainGame()
File "./test.py", line 43, in mainGame
badResponse()
File "./test.py", line 14, in badResponse
print("Sorry, I don't understand " + search_area)
NameError: name 'search_area' is not defined
要退出脚本,您可以使用
import sys
sys.exit()
至于你的 badResponse 错误:你正试图在 bad-response 函数中使用变量 search_area,但该变量是在另一个函数中定义的,这意味着它无法访问它。您要么必须将 search_area 作为参数传递给 badResponse,要么将 search_area 设为全局变量(在顶部定义,在任何函数之外)。
在你的函数中 search_area 不存在。
def badResponse():
print("Sorry, I don't understand " + search_area)
您需要将 search_area 传递到您的函数中:
def badResponse(search_area):
print("Sorry, I don't understand " + search_area)
当你想调用函数时使用:
badResponce(search_area)
在设计游戏时,比起传统的 "backend" Python 编码,我们发现需要这种模式:从内部函数到 "jump" 再到外部函数。
所以在游戏中,从主循环调用的函数中,您会想要跳出主循环并转到代码设置下一个游戏阶段或显示游戏的地方,这很常见- 在屏幕上,并提供开始新游戏的机会。
Python 有 "sys.exit" 调用完全停止程序,因此,虽然您可以从检查游戏结束条件的代码中调用它,但这会退出您的程序完全,并且不给用户开始新匹配的选项。 (如果您的游戏是图形 UI 而不是控制台 "print and input" 游戏,那么本已糟糕的体验将变成灾难性的,因为游戏本身会突然关闭而没有任何痕迹)。
因此,虽然这可以通过这些函数设置的 "state variable" 来管理,并由主循环管理(在您的情况下,[=13= 中的 while
语句] 函数),这种设计很乏味且容易出错——它会是这样的:
def mainGame(...):
...
game_over = False
while not game_over:
if search_area not in search_area_options:
game_over = True
...
if search_area == "4":
game_over = True
所以,请注意,在这种设计中,如果某些东西将 "game_over" 标志更改为 True,
无论在哪里,在下一次迭代中,"while" 条件都会失败,并且
该程序自然会结束您的 mainGame
函数的执行 - 并且
如果没有外部函数处理 "play again?" 屏幕,程序结束。
没关系,对于像这样的简单游戏来说也许是正确的做法。
但在更复杂的设计中,您在主循环中的选择可能会变得更加复杂 - 您可以调用可以自行实现迷你游戏的函数,或者检查本身可能并不简单 - 而且,最重要的是,
退出这个主要功能可能不止 "game over" 个条件,例如 "winning" 条件将引导游戏进入下一阶段。
在这些情况下,您可能希望使用 Python 的异常机制,而不是在变量中记录游戏状态。
异常是在程序错误时自然发生的语言构造,它使程序能够停止,或者继续在函数中 运行 "above" 发生异常的地方 - 如果程序员只包含正确的 "try-except" 子句来捕获异常。
因此,可以进行一个复杂的游戏,它可以处理任意复杂的游戏,而且通过创建良好命名的异常并适当放置 try-except 子句,很容易始终知道执行将导向何处到 -
使用此策略的更复杂游戏的骨架可能是:
# start
class BaseGameException(BaseException): pass
class ProgramExit(BaseGameException): pass
class GameOver(BaseGameException): pass
class MiniGameOver(BaseGameException): pass
class NextStage(BaseGameException): pass
def minigame():
while True:
# code for game-within-game mini game
...
if some_condition_for_winning_main_game_stage:
raise NextStage
...
def main_game(stage):
# get data, scenarios, and variables as appropriate for the current stage
while True:
...
if some_condition_for_minigame:
minigame()
...
if condition_for_death:
raise GameOver
...
def query_play_again():
# print and get messag reponse on whether to play again
...
if player_decided_to_quit:
# This takes execution to outsude the "main_game_menu" function;
raise ProgramExit
def main_game_menu():
"""Handle game start/play again/leatherboard screens"""
stage = 1
while True:
# print welcome message, and prompt for game start
try:
main_game(stage)
except NextStage:
# print congratulations message, and prepare for next stage
stage += 1
except GameOver:
# player died - print proper messages, leatherboard
query_play_again()
stage = 1
# if the program returns here, just let the "while" restart the game
if __name__ == "__main__":
try:
main_game_menu()
except ProgramExit:
print("Goodbye!")
所以,我正在开发一款 python 基于文本的游戏,我可以与朋友分享。我已经完成了大部分游戏的工作,但是当用户选择某些命令时,我正在为游戏结束部分而苦苦挣扎。我没有为此使用 pygame,因为我找不到 64 位版本。下面是我正在看的。我要在 gameOver 函数中输入什么才能真正退出游戏,或者如果玩家想要,再试一次?
import time
import random
import sys
def searchAreas():
print("1. Area1")
print("2. Area2")
print("3. Area3")
print("4. Just give up")
def badResponse():
print("Sorry, I don't understand " + search_area)
def gameOver():
print("You decide to give up because life is too hard.")
print("Would you like to try again?")
player_choice = input("> ")
if player_choice == "Yes":
mainGame()
elif player_choice == "No":
print("# what goes here to quit the game?")
else:
badResponse()
def mainGame():
search_area_options = ["1","2","3"]
search_area = ""
while search_area not in search_area_options:
print("Where do you want to start looking?")
searchAreas()
search_area = str(input("> "))
if search_area == "1":
print("Text here")
elif search_area == "2":
print("Text here")
elif search_area == "3":
print("text here")
elif search_area == "4":
gameOver()
else:
badResponse()
mainGame()
当输入除四个选项之外的任何内容时,或者当进入 gameOver 功能时,我看到此错误:
Traceback (most recent call last):
File "./test.py", line 45, in <module>
mainGame()
File "./test.py", line 43, in mainGame
badResponse()
File "./test.py", line 14, in badResponse
print("Sorry, I don't understand " + search_area)
NameError: name 'search_area' is not defined
要退出脚本,您可以使用
import sys
sys.exit()
至于你的 badResponse 错误:你正试图在 bad-response 函数中使用变量 search_area,但该变量是在另一个函数中定义的,这意味着它无法访问它。您要么必须将 search_area 作为参数传递给 badResponse,要么将 search_area 设为全局变量(在顶部定义,在任何函数之外)。
在你的函数中 search_area 不存在。
def badResponse():
print("Sorry, I don't understand " + search_area)
您需要将 search_area 传递到您的函数中:
def badResponse(search_area):
print("Sorry, I don't understand " + search_area)
当你想调用函数时使用:
badResponce(search_area)
在设计游戏时,比起传统的 "backend" Python 编码,我们发现需要这种模式:从内部函数到 "jump" 再到外部函数。
所以在游戏中,从主循环调用的函数中,您会想要跳出主循环并转到代码设置下一个游戏阶段或显示游戏的地方,这很常见- 在屏幕上,并提供开始新游戏的机会。
Python 有 "sys.exit" 调用完全停止程序,因此,虽然您可以从检查游戏结束条件的代码中调用它,但这会退出您的程序完全,并且不给用户开始新匹配的选项。 (如果您的游戏是图形 UI 而不是控制台 "print and input" 游戏,那么本已糟糕的体验将变成灾难性的,因为游戏本身会突然关闭而没有任何痕迹)。
因此,虽然这可以通过这些函数设置的 "state variable" 来管理,并由主循环管理(在您的情况下,[=13= 中的 while
语句] 函数),这种设计很乏味且容易出错——它会是这样的:
def mainGame(...):
...
game_over = False
while not game_over:
if search_area not in search_area_options:
game_over = True
...
if search_area == "4":
game_over = True
所以,请注意,在这种设计中,如果某些东西将 "game_over" 标志更改为 True,
无论在哪里,在下一次迭代中,"while" 条件都会失败,并且
该程序自然会结束您的 mainGame
函数的执行 - 并且
如果没有外部函数处理 "play again?" 屏幕,程序结束。
没关系,对于像这样的简单游戏来说也许是正确的做法。
但在更复杂的设计中,您在主循环中的选择可能会变得更加复杂 - 您可以调用可以自行实现迷你游戏的函数,或者检查本身可能并不简单 - 而且,最重要的是, 退出这个主要功能可能不止 "game over" 个条件,例如 "winning" 条件将引导游戏进入下一阶段。
在这些情况下,您可能希望使用 Python 的异常机制,而不是在变量中记录游戏状态。 异常是在程序错误时自然发生的语言构造,它使程序能够停止,或者继续在函数中 运行 "above" 发生异常的地方 - 如果程序员只包含正确的 "try-except" 子句来捕获异常。
因此,可以进行一个复杂的游戏,它可以处理任意复杂的游戏,而且通过创建良好命名的异常并适当放置 try-except 子句,很容易始终知道执行将导向何处到 - 使用此策略的更复杂游戏的骨架可能是:
# start
class BaseGameException(BaseException): pass
class ProgramExit(BaseGameException): pass
class GameOver(BaseGameException): pass
class MiniGameOver(BaseGameException): pass
class NextStage(BaseGameException): pass
def minigame():
while True:
# code for game-within-game mini game
...
if some_condition_for_winning_main_game_stage:
raise NextStage
...
def main_game(stage):
# get data, scenarios, and variables as appropriate for the current stage
while True:
...
if some_condition_for_minigame:
minigame()
...
if condition_for_death:
raise GameOver
...
def query_play_again():
# print and get messag reponse on whether to play again
...
if player_decided_to_quit:
# This takes execution to outsude the "main_game_menu" function;
raise ProgramExit
def main_game_menu():
"""Handle game start/play again/leatherboard screens"""
stage = 1
while True:
# print welcome message, and prompt for game start
try:
main_game(stage)
except NextStage:
# print congratulations message, and prepare for next stage
stage += 1
except GameOver:
# player died - print proper messages, leatherboard
query_play_again()
stage = 1
# if the program returns here, just let the "while" restart the game
if __name__ == "__main__":
try:
main_game_menu()
except ProgramExit:
print("Goodbye!")