# -*- coding: utf-8 -*- """ Created on Thu Apr 2 15:38:54 2020 @author: Shawn 2-0-1 | 3 | 5-6-4 """ import Maps as maps import Roles as roles import datetime as dt from threading import Timer class SimpleRPG(object): def __init__(self): ## load potions self.potions = [] self.potionsGenerator() ## load monsters self.monsters = [] self.monstersGenerator() ## load maps self.bases = [] self.mapGenerator() # generate the map ## load games self.savedgames = [] lg = self.loadGame() # to start a new game or load an existed game ## commands >> start to play if lg: self.commands() else: print("See you next time.") def commands(self): """ * The commands for the adventurer """ while True: instruction = input(">> 你想要...:\t") instruction = instruction.strip().lower() if instruction == "quit" or instruction == "exit": break elif instruction == "east": self.adventurer.east() elif instruction == "west": self.adventurer.west() elif instruction == "south": self.adventurer.south() elif instruction == "north": self.adventurer.north() elif instruction == "look east" or instruction == "lookeast": print(self.adventurer.location.lookEast) elif instruction == "look west" or instruction == "lookwest": print(self.adventurer.location.lookWest) elif instruction == "look south" or instruction == "looksouth": print(self.adventurer.location.lookSouth) elif instruction == "look north" or instruction == "looknorth": print(self.adventurer.location.lookNorth) elif instruction == "look around" or instruction == "lookaround" or instruction == "la": print(self.adventurer.location) if self.adventurer.location.npc is not None: print(self.adventurer.location.npc.description) elif instruction == "self" or instruction == "myself": print(self.adventurer) elif instruction == "potion": noMedicine = True for p in range(len(self.adventurer.potions)): if self.adventurer.potions[p] > 0: print(f"{self.potions[p].pid}) {self.potions[p].name} x {self.adventurer.potions[p]}") noMedicine = False if noMedicine: print("你身上沒有藥劑") elif instruction == "eat": medicine = int(input ("What do you want to eat?\t:: ")) if self.adventurer.potions[medicine] <= 0: print(f"你沒有這種藥劑: {self.potions[medicine].name}") else: if self.adventurer.maxBlood - self.adventurer.blood < self.potions[medicine].increment: self.adventurer.blood = self.adventurer.maxBlood else: self.adventurer.blood += self.potions[medicine].increment self.adventurer.potions[medicine] -= 1 print(f"你吃了{self.potions[medicine].name}, 血量{self.adventurer.blood}") elif instruction == "talk": if self.adventurer.location.npc is not None: print(f"{self.adventurer.location.npc.name}:{self.adventurer.location.npc.response}") else: print(f"這裡沒人") elif instruction == "monster": if self.adventurer.location.monster is None or self.adventurer.location.monster.blood <= 0: print("這裡沒怪可打") else: print(f"{self.adventurer.location.monster.description}") elif instruction == "attact" or instruction == "kill": if self.adventurer.location.monster is None: print("這裡沒怪可打") else: iswin = self.adventurer.attact(self.adventurer.location.monster) if iswin: spot = self.adventurer.location.baseid t = Timer(10.0, lambda: self.bases[spot].monster.resume()) t.start() print(f"{self.adventurer.location.monster.name}'s blood is {self.adventurer.location.monster.blood}.") elif instruction == "pick": if self.adventurer.location.monster.blood <= 0 and self.adventurer.location.monster.potionId != -1 and self.adventurer.location.monster.nPotion > 0: self.adventurer.potions[self.adventurer.location.monster.potionId] += self.adventurer.location.monster.nPotion print(f"你拾起了{self.adventurer.location.monster.nPotion}個{self.adventurer.location.monster.potion.name}") self.adventurer.location.monster.nPotion = 0 else: print("沒有東西可檢") elif instruction == "blood": print(f"你還剩下{self.adventurer.blood}點血量") elif instruction == "check monster" or instruction == "checkmonster": print(f"{self.adventurer.location.monster.howl()}") print(f"{self.adventurer.location.monster.name}還有{self.adventurer.location.monster.blood}血量") elif instruction == "save": self.saveGame() else: print(f"Invalid instruction.") def potionsGenerator(self): with open("potions.txt", "r") as f: for i, line in enumerate(f): if i==0: continue else: lineArr = line.split("\t") ## id name increment potion = roles.Potion(int(lineArr[0]), lineArr[1], float(lineArr[2])) self.potions.append(potion) def monstersGenerator(self): with open("monsters.txt", "r") as f: for i, line in enumerate(f): if i==0: continue else: lineArr = line.split("\t") ## id mtype name level blood description firepower monster = maps.Rabbit(lineArr[1], lineArr[2], int(lineArr[3]), float(lineArr[4]), lineArr[5], float(lineArr[6]), int(lineArr[7])) monster.potion = self.potions[monster.potionId] self.monsters.append(monster) def mapGenerator(self): """ * To generate all the tiles and set up the all the pointers """ with open("maps.txt", "r") as f: for i, line in enumerate(f): if i==0: continue lineArr = line.strip().split("\t") tile = maps.Base(int(lineArr[0]), lineArr[1], lineArr[2], lineArr[3], lineArr[4], lineArr[5], lineArr[6], lineArr[7]) for i in range(8, len(lineArr)): if lineArr[i]=="none": continue else: if i==8: tile.lookEast = lineArr[i] if i==9: tile.lookWest = lineArr[i] if i==10: tile.lookSouth = lineArr[i] if i==11: tile.lookNorth = lineArr[i] if lineArr[12] != "none": tile.npc = roles.NPC(lineArr[12], lineArr[13], lineArr[14], lineArr[15]) mId = int(lineArr[16]) if mId != -1: tile.monster = self.monsters[mId] self.bases.append(tile) ## connect the tiles for tile in self.bases: if tile.eastid != "none": tile.setEast(self.bases[int(tile.eastid)]) if tile.westid != "none": tile.setWest(self.bases[int(tile.westid)]) if tile.southid != "none": tile.setSouth(self.bases[int(tile.southid)]) if tile.northid != "none": tile.setNorth(self.bases[int(tile.northid)]) def saveGame(self): game = f"{dt.datetime.now()}, {self.adventurer.name}, {self.adventurer.level}, {self.adventurer.location.baseid}, {self.adventurer.maxBlood}, {self.adventurer.blood}, {self.adventurer.firepower}, {self.adventurer.weapon}, {self.adventurer.occupation}" for i in self.adventurer.potions: game = game + f',{i}' self.savedgames.append(game) with open("SavedGames.txt", "w") as f: # if len(self.savedgames) == 0: # f.write(f"{g}") # else: for i, g in enumerate(self.savedgames): if i==len(self.savedgames)-1: if i==0: f.write(f"{g}") else: f.write(f"\n{g}") else: f.write(f"{g}") def loadGame(self): ## get all the saved games with open("SavedGames.txt", "r") as f: for g in f: self.savedgames.append(g) # print(f"{self.savedgames}") while True: instruction = input("""Please choose from the followings: 0) Exit: Exit the game 1) New: Open a new game 2) Load: Load an existed game\n>> """) instruction = instruction.lower().strip() ## Exit game if instruction=="exit" or instruction=="quit" or instruction=="0": return False ## Start a new game elif instruction=="new" or instruction=="1": name = input("請輸入你的名字\n>> ") print(f"Greeting, {name}") while True: occupation = input("""請選擇你的職業 1) 戰士(warrior)\n>> """) if occupation.strip() == "1" or occupation.strip().lower() == "warrior": self.adventurer = roles.Warrior(name, 0, self.bases[0]) self.adventurer.potions = [0 for _ in range(len(self.potions))] return True else: print("There is no such occupation, please reselect.") return True ## Load a saved game elif instruction=="load" or instruction=="2": if len(self.savedgames) == 0: print(":: There is NO saved games.") continue for i, g in enumerate(self.savedgames): print(f"{i}) {g}", end="") while True: game = int(input("選擇一個遊戲:\t >> ")) if game >= len(self.savedgames): print("There is no this game, please reselect.") else: temp = self.savedgames[game].split(",") print(f"You have chosen game number {game}") if temp[8].lower().strip() == "戰士": self.adventurer = roles.Warrior(temp[1].lower().strip(), # name int(temp[2].lower().strip()), # level self.bases[int(temp[3].lower().strip())], # location float(temp[4].lower().strip()), # maxBlood float(temp[5].lower().strip()), # blood float(temp[6].lower().strip()), # firepower temp[7].lower().strip(), # weapon temp[8].lower().strip() # occupation ) po = temp[9:(9+len(self.potions))] po = list(map(lambda x: int(x), po)) self.adventurer.potions = po print(f"你好 {self.adventurer.name} ,歡迎重新你的冒險") break return True else: print("Unknown instruction, please try again") if __name__=="__main__": rpg = SimpleRPG()