我对Python的编码和一般的编程都是全新的。我拿起了“学习Python”的书,慢慢地读完了这本书。我决定尝试通过编写一个简单的基于文字的游戏来实现我所学到的东西,这个游戏可以在Mac终端上运行。
我希望听到任何关于代码段的反馈,或者对整个代码的反馈!请记住,任何被认为是“先进”的建议在我学习的这一点上都将失去。
可能有很多不相关的代码。这个练习主要是为了练习我学到的东西。
###___def___ practice in game format
import time
def skip():
print
# Created to create adequate space after endgame()
def resskip():
print() * 10
#Used to repeat inputs if wrong input is selected
#Also used reset "inpot" value for choiceloop2() for sceneselect when
#an incorrect input is selected
def choiceerror2(option, frame):
inpot = option
if inpot == '1':
return choiceloop2(inpot, frame)
elif inpot == '2':
return choiceloop2(inpot, frame)
else:
print('Invalid option! Please try again!')
inpot = raw_input('What do you choose?: ')
return choiceerror2(inpot, frame)
#has the same functionality as choiceerror2(), scenes with 3 choices
def choiceerror3(option, frame):
inpot = option
if inpot == '1':
return choiceloop3(inpot, frame)
elif inpot == '2':
return choiceloop3(inpot, frame)
elif inpot == '3':
return choiceloop3(inpot, frame)
else:
print('Invalid option! Please try again!')
inpot = raw_input('What do you choose?: ')
return choiceerror3(inpot, frame)
#used primarily to assign a "scene" value for sceneselect
def choiceloop2(option, frame):
inpot = option
if inpot == '1':
return sceneselect(frame, '1')
else:
return sceneselect(frame, '2')
#used for the same thing as choiceloop3(), scenes with 3 choices
def choiceloop3(option, frame):
inpot = option
if inpot == '1':
return sceneselect(frame, '1')
elif inpot == '2':
return sceneselect(frame, '2')
else:
return sceneselect(frame, '3')
#selects scenes based on the "frame" value assigned at the end of each
#scene, as well as the scene value assigned during choiceloop
def sceneselect(frame, scene):
if frame == '1':
if scene == '1':
scenesewer()
else:
scenefield()
elif frame == '2sewer':
if scene == '1':
scenefield()
if scene == '3':
scenehall()
elif frame == '2field':
scenesewer()
#End of game prompt. Also has invalid input loop prompting for re-input
def endgame():
print('YOU HAVE DIED....\nWould you like to try again?: ')
time.sleep(2)
inpot = raw_input('Y/N ... ')
if inpot == 'Y':
resskip()
main()
elif inpot == 'N':
skip()
print('Thank you for playing!')
exit()
else:
print('Invalid input!')
endgame()
#Win Game prompt.
def wingame():
print('Thank you for playing!')
time.sleep(5)
exit()
#Beginning of game
def opening():
skip()
skip()
print("Welcome to the beginning of an adventure...")
time.sleep(5)
skip()
print('There will be many paths to choose from...')
time.sleep(5)
skip()
print('Will you be able to make it to the end?')
time.sleep(5)
skip()
print('You wake up in a dungeon, you are injured and do not have any weapons.\nYou hear loud growling, and shortly after, some scratches at a door behind you.\nYou become frightened.\nYou see there is a window with a few broken bars that you can squeeze through to make an escape.\nLikewise, there is a sewer you can try to escape through!')
time.sleep(2)
skip()
print('1 = Sewer, 2 = Window')
inpot = raw_input('What do you choose?: ')
skip()
if inpot == '1':
choiceloop2(inpot, '1')
elif inpot == '2':
choiceloop2(inpot, '1')
else:
choiceerror2(inpot, '1')
#Field "scene"
def scenefield():
print('You find yourself in a large field outside of the dungeon.\nYou see an apple tree. Hoping to find food, you begin to approach it\nAs you approach the tree, two wolves show themselve!\nYou turn to run back to where you came from. As you make your way, you spot two other escape routes.\n One is a large hole in the side of the dungeon leading down.\nThe other is a river crossing.\n....')
skip()
time.sleep(15)
print('1 - Big Hole, 2- River Crossing')
inpot = raw_input('Which do you choose?: ')
if inpot == '1':
skip()
print('You jump through the hole wihout hesitation. You slide back down into the dungeon.\n...')
time.sleep(3)
choiceloop2(inpot, '2field')
elif inpot == '2':
skip()
time.sleep(3)
print('You jump into the river in an attempt to cross.\nThe river current is much stronger than you anticipated.\nYou knew this was a bad idea before you choose it.\nYou drown in the current.\n...')
time.sleep(8)
endgame()
else:
choiceerror2(inpot, '2field')
#Sewer "scene"
def scenesewer():
print('You tread through the dark sewer.\nAs you make your way through the darkness, you can hear the growling behind you grow faint.\nYou realise that you have lost your sense of direction!\nYou come to an intersection that divides into three paths... ')
skip()
time.sleep(2)
print('1 - Left, 2 - Center, 3 - Right')
inpot = raw_input('Which way do you choose?: ')
if inpot == '1':
skip()
print('You take the path on the left.\nYou find yourself walking on a slight incline.\nYou find it hard to get a solid footing, but you make it up.\nYou find a door, and you open it. As you open the door, the sunlight blinds you.\nAs you step through, you find yourself in a field...')
time.sleep(3)
choiceloop3(inpot, '2sewer')
elif inpot == '2':
skip()
print('You take the center path.\nYou feel elated that you can finally see a light at the end of the path.\nYou start to move quickly in an attempt to get out of the sewer as soon as possible.\nYou suddenly fall, and severly injure your leg.\nYou do no survive!\n.....')
time.sleep(10)
skip()
endgame()
elif inpot == '3':
skip()
print('You take the path on the right. You make your way through the sewer until you come to a hatch.\nYou open the hatch and jump into the space above.\nYou find yourself in a long hall...')
time.sleep(3)
choiceloop3(inpot, '2sewer')
else:
choiceerror3(inpot, '2sewer')
#End of game
def scenehall():
skip()
print('It is late and I am tired of writing this code. I only want to sleep now.\nYOU WIN BY DEFAULY!')
time.sleep(5)
# def main() made for no reason but practice
def main():
opening()
main()发布于 2016-01-03 22:18:59
这有点让人恼火,但我讨厌看到大量的if语句。将它们替换为字典查找:
actions = { 'house': scene_house,
'garden': scene_garden,
'street': scene_street
}
def unknown_action():
print("Invalid action! Please try again!")
return raw_input("What do you want to do?")
current_scene = raw_input("What do you want to do?")
while current_scene != 'dead':
current_scene = actions.get(current_scene, unknown_action)()发布于 2016-01-03 09:45:47
您使用的是Python2.x,所以print是一个语句,而不是一个函数,删除打印出的所有括号,或者在代码的第一行中写入
from __future__ import print_function在reskip * 10中,您认为,它需要10倍的空元组元素(例如,空元组);结果是打印的空元组。你需要一个循环来得到你想要的结果。
不要使用不透明的递归。打开呼叫,选择调用,错误调用,选择调用.你的代码就像一条蛇在咬她自己的尾巴。一个程序应该看起来更像一棵树,遍历分支到叶,并将分支返回到其他叶:opening调用第一个scene,scene调用choiceloop,choiceloop返回一个有效的选择,scene返回这个选择,这样opening就可以调用下一个场景。然后,您也不必让frame通过所有这些函数。
每个choiceloops看起来都是一样的,您可以编写一个通用函数:
def choice(options):
print ', '.join('%d - %s' % (i, option) for i, option in enumerate(options, 1))
while True:
try:
answer = int(raw_input('Which way do you choose?: '))
if 1 <= answer <= len(options):
return options[answer - 1]
except ValueError:
pass
print('Invalid option! Please try again!')你的游戏看起来是:
def scene_house():
print "nice room"
answer = choice(["street", "garden"])
if answer == "street":
print "you go through the front door"
return answer
def scene_garden():
print "nice trees"
answer = choice(["house"])
return answer
def scene_street():
print "car comes"
return "dead"
def main():
current_scene = "house"
while current_scene != "dead":
if current_scene == "house":
current_scene = scene_house()
if current_scene == "garden":
current_scene = scene_garden()
if current_scene == "street":
current_scene = scene_street()
print "you are dead"
if __name__ == '__main__':
main()https://codereview.stackexchange.com/questions/115701
复制相似问题