【发布时间】:2019-09-27 14:21:42
【问题描述】:
所以,我正在开发一个基于 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
【问题讨论】:
-
为什么不直接退出(0)?
标签: python python-3.x macos adventure