这是我制作的第一款基于文字的游戏。它很小,因为我刚学了Python,想要从小程序开始,然后编写更大的程序。从上次开始我就加了些东西。请给出你诚实的意见。
关于这个游戏,这里是我最初的问题。
import random
import time
import sys
def fight_enemy_3(enemy_name, min_enemy_damage, max_enemy_damage, min_player_damage, max_player_damage):
enemy_damage_dealt = random.randint(min_enemy_damage, max_enemy_damage)
player_damage_dealt = random.randint(min_player_damage, max_player_damage)
if enemy_damage_dealt > player_damage_dealt:
print("Uh-oh! You died!")
game_over()
elif enemy_damage_dealt < player_damage_dealt:
print("You killed the {enemy_name}".format(enemy_name=enemy_name))
cold()
else:
print("You walk away unscathed, but the {enemy_name} still lives.".format(enemy_name=enemy_name))
cold()
def fight_enemy(enemy_name, min_enemy_damage, max_enemy_damage, min_player_damage, max_player_damage):
enemy_damage_dealt = random.randint(min_enemy_damage, max_enemy_damage)
player_damage_dealt = random.randint(min_player_damage, max_player_damage)
if enemy_damage_dealt > player_damage_dealt:
print("Uh-oh! You died!")
game_over()
elif enemy_damage_dealt < player_damage_dealt:
print("You killed the {enemy_name}".format(enemy_name=enemy_name))
its_getting_late()
else:
print("You walk away unscathed, but the {enemy_name} still lives.".format(enemy_name=enemy_name))
its_getting_late()
def fight_enemy_2(enemy_name, min_enemy_damage, max_enemy_damage, min_player_damage, max_player_damage):
enemy_damage_dealt = random.randint(min_enemy_damage, max_enemy_damage)
player_damage_dealt = random.randint(min_player_damage, max_player_damage)
if enemy_damage_dealt > player_damage_dealt:
print("Uh-oh! You died!")
game_over()
elif enemy_damage_dealt < player_damage_dealt:
print("You killed the {enemy_name}".format(enemy_name=enemy_name))
game_end()
else:
print("You walk away unscathed, but the {enemy_name} still lives.".format(enemy_name=enemy_name))
game_end()
def intro():
name = input("Enter you name: ")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("Welcome to the Wildlife %s" %(name))
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
time.sleep(1)
desert()
def desert():
print("You are in the middle of a Desert in Africa")
time.sleep(2)
print("All you see is Sand. EVERYWHERE!!!")
time.sleep(2)
print("You start walking")
time.sleep(2)
print("You see a Snake")
ch1 = str(input("Do you wanna attck the Snake? [y/n]: "))
if ch1 in ['y', 'Y', 'YES', 'Yes', 'yes']:
time.sleep(2)
fight_enemy('Snake', 1, 7, 1, 7)
elif ch1 in ['n', 'N', 'No', 'NO', 'no']:
print("You slowly back away")
time.sleep(1)
print("You managed to get away from the Snake")
mountain_2()
def game_end():
print("Congratulations you finished lvl 1!!!")
ch4 = str(input("Do you wanna play lvl 2? [y/n] "))
if ch4 in ['y', 'Y', 'YES', 'Yes', 'yes']:
print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
level_2_intro()
elif ch4 in ['n', 'N', 'No', 'NO', 'no']:
print("Thank you for playing")
def its_getting_late():
time.sleep(1)
print("Its starting to get late")
ch2 = str(input("Do you wanna go to sleep? [y/n]: "))
if ch2 in ['y', 'Y', 'YES', 'Yes', 'yes']:
print("You were stabbed dead last Night")
elif ch2 in ['n', 'N', 'No', 'NO', 'no']:
print("You stay up")
time.sleep(1)
print("You see a person")
ch3 = str(input("Do you wanna attack the person? [y/n]: "))
if ch3 in ['y', 'Y', 'YES', 'Yes', 'yes']:
time.sleep(1)
fight_enemy_2('Enemy', 4, 8, 1, 7)
elif ch3 in ['n', 'N', 'No', 'NO', 'no']:
print("You try to run away")
print("But the Enemy is faster then you")
fight_enemy_2('Enemy', 4, 8, 1, 6)
def game_over():
print("You lost")
ch5 = str(input("Do you wanna play again? [y/n] "))
if ch5 in ['y', 'Y', 'YES', 'Yes', 'yes']:
print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
intro()
elif ch5 in ['n', 'N', 'No', 'NO', 'no']:
print("Thank you for playing")
def level_2_intro():
print("Welcome to level 2!!!")
print("You got a spear!!!")
mountain()
def mountain():
print("You start walking up a mountain")
time.sleep(2)
print("Oh no you see a a Tiger!!!")
ch6 = str(input("Do you wanna attack the Tiger with you spear? [y/n]: "))
if ch6 in ['y', 'Y', 'YES', 'Yes', 'yes']:
time.sleep(1)
fight_enemy_3('Tiger', 5, 10, 4, 8)
elif ch6 in ['n', 'N', 'No', 'NO', 'no']:
print("The Tiger killed you")
game_over()
def mountain_2():
print("And start walking up a mountain")
time.sleep(2)
print("Oh no you see a Tiger!!!")
ch6 = str(input("Do you wanna attack the Tiger? [y/n]: "))
if ch6 in ['y', 'Y', 'YES', 'Yes', 'yes']:
time.sleep(1)
fight_enemy_3('Tiger', 5, 10, 1, 7)
elif ch6 in ['n', 'N', 'No', 'NO', 'no']:
print("The Tiger killed you")
game_over()
def cold():
print("You are getting cold")
time.sleep(1)
ch7 = str(input("Do you wanna make a fire? [y/n]: "))
if ch7 in ['y', 'Y', 'YES', 'Yes', 'yes']:
print("You made a fire")
time.sleep(1)
sleep()
elif ch7 in ['n', 'N', 'No', 'NO', 'no']:
print("You died")
game_over()
def sleep():
ch8 = str(input("Do you wanna go to sleep? [y/n]: "))
if ch8 in ['y', 'Y', 'YES', 'Yes', 'yes']:
print("You go to sleep")
time.sleep(2)
you_slept()
elif ch8 in ['n', 'N', 'No', 'NO', 'no']:
print("You stay up")
def you_slept():
print("The next day you wake up")
print("You keep on walking")
print("You see a person")
ch9 = str(input("Do you wanna attck the person? [y/n]: "))
if ch9 in ['y', 'Y', 'YES', 'Yes', 'yes']:
print("You attack the person")
time.sleep(1)
fight_enemy_4('Enemy', 1, 7, 1, 7)
elif ch9 in ['n', 'N', 'No', 'NO', 'no']:
print("It turns out the person is nice and want to be friends")
time.sleep(1)
print("You have a friend now")
friends()
def fight_enemy_4(enemy_name, min_enemy_damage, max_enemy_damage, min_player_damage, max_player_damage):
enemy_damage_dealt = random.randint(min_enemy_damage, max_enemy_damage)
player_damage_dealt = random.randint(min_player_damage, max_player_damage)
if enemy_damage_dealt > player_damage_dealt:
print("Uh-oh! You died!")
game_over()
elif enemy_damage_dealt < player_damage_dealt:
print("You killed the {enemy_name}".format(enemy_name=enemy_name))
you_killed_him()
else:
print("You walk away unscathed, but the {enemy_name} still lives.".format(enemy_name=enemy_name))
you_killed_him()
def you_killed_him():
print("You keep walking")
def friends():
print("You and your friend combined powers")
intro()发布于 2015-08-12 00:04:47
现在看来,要创建“阶段”,就需要创建多个函数,每个函数包含多个if语句,每个函数决定下一步将运行哪个函数或"stage“。这不是一个很好的方法,它可以使代码很难阅读。
实际上,我已经创建了一个库,并得到了一些其他用户的帮助,这使得创建这样的冒险游戏更加容易。它被称为Cactus,您可以找到存储库这里以及下载链接。您还可以访问网站。
来自官方文件:
导入--将仙人掌导入到您的项目中非常容易。将
Cactus/cactus添加到PYTHONPATH后,您只需要做以下工作:导入创建cactus.Position实例的仙人掌--cactus.Position实例存储有关cactus.Flowchart位置的相关数据。作为参考,这里有一个cactus.Position属性的列表。
"name" -这个职位的名字。"desc_enter" -当玩家“进入”位置时要显示的描述。"desc_exit" -当玩家“退出”位置时要显示的描述。"choices" -表示流程图中其他位置的选项字典。键是用户输入的内容,值是流程图中其他位置的引用键。下面是一个简单的cactus.Position实例的结构: cactus.Position({“名称”:"start“、"desc_enter":”enter on enter.“、"desc_exit":"Description on exit.”、"choices":{ "choice1":"1“、"choice2":"2”} }),创建cactus.Flowchart实例流程图对于Cactus是必不可少的。流程图是你如何构造游戏玩家将遍历的“地图”。作为参考,这里有一个cactus.Flowchart属性的列表。
"data" -将实际流程图数据存储为字典。下面是一个简单的流程图实例的结构。流程图= cactus.Flowchart({“数据”:{ "position_name":cactus.Position( .)} })创建一个cactus.Game实例cactus.Game类存储与流程图或位置无关的游戏一般数据。cactus.Game类也是存储事件处理程序和全局命令的地方。作为参考,这里有一个cactus.Game属性的列表。
"name" -游戏的名字。"desc" -游戏的描述。"prompt" -用于输入命令的提示符。"invalid_input_msg" -当用户输入无效输入时要显示的消息。"flowchart" -已创建的流程图实例。"case_sensitive" -如果这是假的,那么所有的用户输入,和某些属性值转换为小写。如果这是真的,那么这一切都不会发生。"allow_help" -如果这是真,那么用户可以获得帮助。"about_text" -这个游戏是关于文本的,其中包括了需要使用的属性文本。"event_handlers" -事件处理程序及其相应函数的字典。"global_commands" -可以在任何地方执行的命令字典及其相应的函数.下面是一个简单的cactus.Game实例的结构。cactus.Game({“名称”:“游戏名称","desc":”游戏描述“,”提示“:">”,"invalid_input_msg":“无效输入”,“流程图”:流程图,"case_sensitive":False,"allow_help":True,"about_text":“在这里写你的游戏。”、"event_handlers":{ "position.Name Goes 1.enter.after":exit、"position.Name Goes Here 2.enter.after":exit }、"global_commands":{ " exit ":exit })一旦完成以上所有操作,您就可以开始游戏了。假设您已经创建了cactus.Game的一个实例,命名为GAME,并且所有的检查都被检查出来,那么您所需要做的就是下面的GAME.play_game()
与其每次重新创建fight_enemy函数以运行不同的函数,不如在函数签名function_to_run的末尾添加一个额外的参数。然后,您的fight_enemy函数将变成如下:
def fight_enemy(enemy_name, min_enemy_damage, max_enemy_damage, min_player_damage, max_player_damage, function_to_run):
enemy_damage_dealt = random.randint(min_enemy_damage, max_enemy_damage)
player_damage_dealt = random.randint(min_player_damage, max_player_damage)
if enemy_damage_dealt > player_damage_dealt:
print("Uh-oh! You died!")
game_over()
elif enemy_damage_dealt < player_damage_dealt:
print("You killed the {enemy_name}".format(enemy_name=enemy_name))
function_to_run()
else:
print("You walk away unscathed, but the {enemy_name} still lives.".format(enemy_name=enemy_name))
function_to_run()您还在许多地方使用input函数转换str调用的结果。这是不必要的。您可以这样调用input:
... = input( ... )我也会删除各种time.sleeps,这是有点烦人的时候,必须等待游戏,特别是当它是超过1-2秒。
https://codereview.stackexchange.com/questions/100644
复制相似问题