Advertisement
kenadams53

Kens Game

Aug 20th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.08 KB | None | 0 0
  1. ''' Python code by ken Adams
  2. The structure of Ken’s game: There are five rooms. Going clockwise, we start in the lounge and enter the kitchen,
  3. dining hall, and bathroom and then back to the lounge. There is a circular shaped veranda that has doors to the kitchen
  4. (direction SE) and the lounge (direction SW). The player has the option to move to any adjacent room. The game starts
  5. in the veranda, there is nobody there. There is one character in each of the other rooms, two are friends and two are enemies.
  6. The player has a sports bag containing three items. Each friend has an item. When the player visits a room containing a friend
  7. he may swap an item in the bag for the one that the friend has. Each enemy character has a weakness item and if fought with
  8. this item will be defeated. When the player visits a room with an enemy character in it, the player may fight the enemy.
  9. The player is asked what item to use for the fight and that item must be in the bag, and be the weakness of the enemy,
  10. otherwise the player loses. If the player has the item and it is the weakness of the enemy character the player wins victory
  11. so two victories wins the game. The player has two lives, defeat cost a life so two defeats loses the game.
  12. The first time there is a meeting the player can decide not to fight an enemy (so can pass by and pick up a weapon by
  13. swapping with a friend). The second time the player must fight. The moves from room to room are limited (say to 12) so
  14. the player can also lose if too many moves have already been played. One item in the bag is a weakness of one of the enemies.
  15.  
  16. See pastebin for code:
  17. Kens Game main: https://pastebin.com/NHu4SGA0
  18. Room object: https://pastebin.com/Mn1HecX6
  19. Character object: https://pastebin.com/m6WJQNn5
  20. Item object: https://pastebin.com/GAirfZ5B
  21.  
  22. Acknowledgement, much of this code was produced by: the Raspberry Pi Foundation: Object-oriented Programming in Python,
  23. a FutureLearn Course. The structure of this particular game was crafted by Ken Adams whilst taking the course.
  24. '''
  25.  
  26.  
  27. from room import Room
  28. from character import Enemy, Friend
  29. from item import Bag
  30. lives = 2
  31. victories = 0
  32. moves = 12
  33. #########################Sports Bag set up##################
  34. sports = Bag("sports")
  35. sports.contents = ["banana", "potato", "fish"]
  36. sports.set_description("\nYour sports bag is large and black with contents:")
  37. sports.describe()
  38. #############setting up the characters###################
  39. dave = Enemy("Dave", "A smelly zombie")
  40. dave.set_conversation("Brrlgrh... rgrhl... brains...")
  41. dave.set_weakness("cheese")
  42. lilif = Enemy("Lilif", "A giant frog")
  43. lilif.set_conversation("whop whop whop Ah!")
  44. lilif.set_weakness("banana")
  45. charles = Friend("Charles", "A friendly octapus")
  46. charles.set_conversation("let me hug you with my tenticles")
  47. charles.set_has_item("cheese")
  48. joy = Friend("joy", "A wise oul")
  49. joy.set_conversation("hoot I know so much")
  50. joy.set_has_item("hat")
  51. #dave.describe()#lilif.describe()#charles.describe()#joy.describe()
  52. #############################swap#########################
  53. def SwapItems(myItem, Friend): # finction to exchange an item in the bag with a friends item
  54. tempItem = Friend.has_item
  55. Friend.has_item = myItem
  56. sports.contents.append(tempItem)
  57. for itemNo in range(len(sports.contents)):
  58. if sports.contents[itemNo] == myItem:
  59. tempNo = itemNo
  60. del sports.contents[tempNo]
  61. ####code for testing:#print("Charles has " + charles.has_item)#SwapItems("banana",charles)
  62. #print("Charles has " + charles.has_item)#print(sports.contents)
  63. #######################setup rooms##############################
  64. veranda = Room("Veranda")
  65. veranda.set_description("The Veranda is outside with pot plants and a swinging chair")
  66. kitchen = Room("Kitchen")
  67. kitchen.set_description("The kitchen: is a dank and dirty room buzzing with flies.")
  68. kitchen.set_character(charles)
  69. dining = Room("Dining")
  70. dining.set_description(" The dining room is a bright room with lots of chairs and tables.")
  71. dining.set_character(dave)
  72. bathroom = Room("bathroom")
  73. bathroom.set_description("The bathroom is a large bath and a toilet,frosted windows")
  74. bathroom.set_character(joy)
  75. lounge = Room("Lounge")
  76. lounge.set_description("The lounge has confortable chairs and nice pictures")
  77. lounge.set_character(lilif)
  78. #veranda.describe()#kitchen.describe()#dining.describe()#bathroom.describe()#lounge.describe()
  79.  
  80. #linking the rooms
  81. veranda.link_room(kitchen, "south-east")
  82. veranda.link_room(lounge, "south-west")
  83.  
  84. kitchen.link_room(veranda, "north-west")
  85. kitchen.link_room(dining, "south")
  86. kitchen.link_room(lounge, "west")
  87.  
  88. dining.link_room(kitchen, "north")
  89. dining.link_room(bathroom, "west")
  90.  
  91. bathroom.link_room(lounge, "north")
  92. bathroom.link_room(dining, "east")
  93.  
  94. lounge.link_room(bathroom, "south")
  95. lounge.link_room(kitchen, "east")
  96. lounge.link_room(veranda, "north-east")
  97.  
  98. ####code for testing#print(lounge.linked_rooms['east'].name)#if 'east' in lounge.linked_rooms.keys():
  99. #print("yes")#veranda.get_details()#kitchen.get_details()#dining.get_details()#bathroom.get_details()
  100. #lounge.get_details()
  101.  
  102. ##############################while loop###############################
  103.  
  104. current_room = veranda
  105. direction = ""
  106.  
  107. #The wile loop has three sections controled by insinstance functions, The character in a room is either a Friend (if),
  108. #Enemy (elif) or nobody there 'None' (else)
  109.  
  110. while moves >0:
  111. print(" you have " + str(moves) +" moves left ")
  112. current_room.get_details()
  113. #######################################################Friend#########################
  114. if isinstance(current_room.character, Friend):
  115. current_room.character.describe()
  116. decide = input("Do you want to swap items with your friend, say Y or N ? " )
  117. if decide == "Y":
  118. swapit = input("what item will you swap? ")
  119. if swapit in sports.contents:
  120. SwapItems(swapit,current_room.character )
  121. print("Now you have ")
  122. print(sports.contents)
  123. else:
  124. print("sorry you don't have that item, so no swap.")
  125. else:
  126. print(" OK no swap ")
  127. direction=input("What directionn will you move next? ")
  128. current_room =current_room.move(direction)
  129. ###################################################################Enemy######################
  130. elif isinstance(current_room.character, Enemy):
  131. current_room.character.describe()
  132. if current_room.character.noFights == 0:
  133. decide= input("Do you want to fight Y/N? " )
  134. if decide == "Y":
  135. weapon = input("choose your weapon? ")
  136. if weapon in sports.contents and current_room.character.weakness == weapon:
  137. victories += 1
  138. if victories == 2:
  139. print("congratulations you won the game ")
  140. moves = 0
  141. else:
  142. current_room.character = None
  143. direction = input(" You have " + str(victories) + " victories what direction next ")
  144. current_room =current_room.move(direction)
  145. else:
  146. print("You don't have " + weapon + " so You loose ")
  147. lives -= 1
  148. if lives == 0:
  149. print("You loose the game ")
  150. moves = 0
  151. else:
  152. direction = input(" You have " + str(lives) + " lives left what direction next? ")
  153. current_room =current_room.move(direction)
  154.  
  155. else:
  156. current_room.character.noFights = 1
  157. direction=input("OK see you next time, what directionn will you move? ")
  158. current_room =current_room.move(direction)
  159.  
  160. else:
  161. weapon = input("you have to fight me now, choose your weapon ")
  162. if weapon in sports.contents and current_room.character.weakness == weapon:
  163. victories += 1
  164. if victories == 2:
  165. print("congratulations you won the game ")
  166. moves = 0
  167. else:
  168. current_room.character = None
  169. direction = input(" You have " + str(victories) + " victories what direction next ")
  170. current_room =current_room.move(direction)
  171. else:
  172. print("You loose ")
  173. lives -= 1
  174. if lives == 0:
  175. print("You loose the game ")
  176. moves = 0
  177. else:
  178. direction = input(" You have " + str(lives) + " lives left what direction next ")
  179. current_room =current_room.move(direction)
  180. ################################################################None########################
  181. else:
  182. direction=input("nobody is here so in what directionn will you move? ")
  183. current_room =current_room.move(direction)
  184. moves -= 1
  185. if moves == 0:
  186. print("Too many moves so you loose the game ")
  187.  
  188. print("bye from game ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement