所以我正在制作一个游戏,我对python作为一种编码语言非常陌生,我目前还没上课,我自学。我正在制作一个基于文本的游戏。我希望将HP作为一个因素来实现,并在游戏结束时提供重启游戏的选项。这是我到目前为止的代码。
print("Welcome to the journey to Camelot. This game is case sensitive, type the answers exactly as shown!")
print("\nYou wake up and decide today you are going to become a knight of the round table. You are a skinny average joe"
" but, that does not bother you. You hunker up, get dressed, and decide what to bring with you. Do you bring a"
" 'Bow' or a 'Sword?'")
SwordBow = input('>')
if SwordBow == "Bow":
print("\nYou pick up your trusty bow and get ready to leave. You walk outside and notice it is raining. "
"This makes you lose some of your determination for this trip. Do you 'Go Home' or 'Continue Onward'")
GoHomeContinueOnward = input('>')
if GoHomeContinueOnward == "Go Home":
print("\nYou go home and go to the sleep, game over.")
##Instance of a game over
elif GoHomeContinueOnward == "Continue Onward":
print("\nFiller")
elif SwordBow == "Sword":
print("\nYou pick up your sturdy sword and prepare to leave. You notice it is raining. This makes you realize your"
" sword could rust. Do you 'Stick it under your shirt' or 'Eh keep it sheathed'?")
StickitunderyourshirtEhkeepitsheathed = input('>')
if StickitunderyourshirtEhkeepitsheathed == "Stick it under your shirt":
print("\nFiller")
print("\nYou have lost 10HP")
##Here is one of the reasons I want to implement HP
elif StickitunderyourshirtEhkeepitsheathed == "Eh keep it sheathed":
print("\nFiller")
我希望你能够失去HP,如果你太低,可以获得一个游戏,如果 HP> 0: 打印(" Game Over") 我很抱歉用这些简单的问题来打扰你的时间,但正如我所说,我对python很新,这是我尝试编写的第一个完整的东西。最后,我希望它只是一个基于选择的流程图样式文本冒险。我还希望在游戏结束后重启该功能。提前感谢您的任何帮助,我只是要求所以我知道将来如何避免混乱等。
答案 0 :(得分:0)
欢迎来到网站:)
我注意到的第一件事是变量名的长度。 StickitunderyourshirtEhkeepitsheathed
真的很长很难读;将其重命名为简短但具有描述性的内容,可能是weapon_action
,并使用下划线来分隔单词。
要实现hp
,只需在名为hp
的脚本开头添加一个变量,并为其指定一个默认值;例如hp = 100
。然后,在print("\nYou have lost 10HP")
后写hp -= 10
或hp = hp - 10
(它们的含义相同),从hp
中减去10。
您应该查看一些有关如何编写函数的教程,因为这是您轻松重新运行游戏所需的内容。目前,您的脚本将从上到下执行,并且没有简单的方法可以返回到开头。如果你把所有东西放在一个函数中,例如def main():
,def game():
或您想要调用它的任何内容,只要您想重新运行它,就可以调用该函数。