当前位置:   article > 正文

Python之初级RPG小游戏_用字典构建rpg游戏

用字典构建rpg游戏

在国外网站上找到一个练习Python的小游戏感觉不错,自己实现了一下。

通过该练习你能学到:

  1. 元组
  2. 字典
  3. 简单定义函数和封装
  4. 条件控制语句

 

游戏说明

以下是3个房间和1个花园:

Hall 客厅 有一把钥匙,Kitchen 厨房 有一只怪物,Dinning Room 餐厅 有一瓶药水,Garden 花园

完成游戏条件:拿到钥匙和药水到达花园并躲避怪物。

游戏操作指令:
      go [direction]
      get [item]

[direction] 包含:east, west, south, north

[item] 包含:key, potion

 

游戏源码

  1. #! python3
  2. """
  3. author: laoxu
  4. """
  5. # 游戏说明
  6. def showInstructions():
  7. print('''
  8. RPG Game
  9. ========
  10. 完成游戏条件:拿到钥匙和药水到达花园并躲避怪物。
  11. 命令:
  12. go [direction]
  13. get [item]
  14. '''
  15. )
  16. # 打印当前房间和背包信息
  17. def showCurrentRoom(room, bag):
  18. print('You are in the %s' % room)
  19. print('Inventory: ', bag)
  20. rooms = {
  21. 'Hall': {
  22. 'south': 'Kitchen',
  23. 'east': 'Dinning Room',
  24. 'item': 'key'
  25. },
  26. 'Kitchen': {
  27. 'north': 'Hall',
  28. 'item': 'monster'
  29. },
  30. 'Dinning Room': {
  31. 'west': 'Hall',
  32. 'south': 'Garden',
  33. 'item': 'potion'
  34. },
  35. 'Garden': {
  36. 'north': 'Dinning Room'
  37. }
  38. }
  39. # 初始化房间
  40. currentRoom = 'Hall'
  41. # 初始化物品栏
  42. inventory = []
  43. # 打印游戏帮助
  44. showInstructions()
  45. print('You are in the %s' % currentRoom)
  46. print('Inventory: ', inventory)
  47. print('You see a key')
  48. while True:
  49. # 玩家进入厨房,游戏结束
  50. if 'item' in rooms[currentRoom] and 'monster' in rooms[currentRoom]['item']:
  51. print('你被怪物抓住了...游戏结束!')
  52. break
  53. # 玩家拿到钥匙和药水进入花园,游戏结束
  54. if currentRoom == 'Garden' and 'key' in inventory and 'potion' in inventory:
  55. print('你逃脱了房子!你赢了!')
  56. break
  57. # 接收操作步骤
  58. step = input()
  59. # 客厅->厨房
  60. if currentRoom == 'Hall' and step == 'go south':
  61. currentRoom = 'Kitchen'
  62. continue
  63. # 客厅->餐厅
  64. elif currentRoom == 'Hall' and step == 'go east':
  65. currentRoom = 'Dinning Room'
  66. # 厨房->客厅
  67. elif currentRoom == 'Kitchen' and step == 'go north':
  68. currentRoom = 'Hall'
  69. # 餐厅->客厅
  70. elif currentRoom == 'Dinning Room' and step == 'go west':
  71. currentRoom = 'Hall'
  72. # 餐厅->花园
  73. elif currentRoom == 'Dinning Room' and step == 'go south':
  74. currentRoom = 'Garden'
  75. # 花园->餐厅
  76. elif currentRoom == 'Garden' and step == 'go north':
  77. currentRoom = 'Dinning Room'
  78. # 拿到钥匙
  79. elif currentRoom == 'Hall' and 'key' not in inventory and step == 'get key':
  80. inventory.append('key')
  81. print('key got!')
  82. # 拿到药水
  83. elif currentRoom == 'Dinning Room' and 'potion' not in inventory and step == 'get potion':
  84. inventory.append('potion')
  85. print('potion got!')
  86. # 打印房间和物品栏
  87. showCurrentRoom(currentRoom, inventory)
  88. if currentRoom == 'Hall' and 'key' not in inventory:
  89. print('You see a key')
  90. if currentRoom == 'Dinning Room' and 'potion' not in inventory:
  91. print('You see a potion')
  92. continue

 

运行效果

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/664832
推荐阅读
相关标签
  

闽ICP备14008679号