def attack(self,Orc): injury = random.randint(10, 15) hurt_target = possible_target(self, Orc) hurt_target.health_meter = max(hurt_target.health_meter - injury, 0) print_bold('%s get damage:%d\t' % (hurt_target.name, injury)) self.show_health(end='\t') Orc.show_health(end='\t')
def _play_choose(self): print('\tCurrent occupants:%s' % self.get_hut_occupants()) idx = 0 flag = True while flag: try: idx = int(input('choose the hut from 1-5:')) if idx <= 0: raise IdxTooBigError('not in 1-5!', 103) # raise GameUnitError('not in 1-5!',101) except ValueError as e: print_bold( 'the idx type must be int,but %s. please try again' % e) continue except IdxTooBigError as e: print(e) print(e.error_message) continue try: if self.huts[idx - 1].is_acquired: print( 'The hut which you choose has been required,choose another one' ) else: flag = False except IndexError as e: print_bold( 'index range out ,must be 1-5,but %s. please try again' % e) continue return idx
def heal(self,heal_blood=2,full_blood=True): if self.health_meter==self.max_health: return if full_blood: self.health_meter=self.max_health else: self.health_meter=max(self.health_meter+heal_blood,self.max_health) print_bold('you are HEALED') self.show_health(bold=True)
def play(self): self.show_mission() dotted_line() self._occupy_huts() # continu_play=True self.player = Knight() occupant_nums = 0 #已占领数量 while occupant_nums < 5: idx = self._play_choose() self.player.acquire_hut(self.huts[idx - 1]) if self.player.health_meter <= 0: print('You lost:(better luck next time') break if self.huts[idx - 1].is_acquired: occupant_nums += 1 if occupant_nums == 5: print_bold('\nCongratulation! you win')
def acquire_hut(self, hut): print_bold("Entering hut %d..." % hut.number, end=' ') if (isinstance(hut.occupant, GameUnit) and hut.occupant.unit_type == 'enemy'): play_option = True self.show_health(end=' ') hut.occupant.show_health(end=' ') while play_option: play_option = input('attack?YES(y)/NO(n):') if play_option == 'n': self.run_away() break self.attack(hut.occupant) if hut.occupant.health_meter <= 0: print('kill Orc') hut.acquire(self) break if self.health_meter <= 0: print('you dead') break else: if hut.get_occupant_type == 'empty': print_bold('Hut is empty') else: print_bold('Hut is Signed') hut.acquire(self) self.heal()
def show_health(self,bold=False,end='\n'): msg = '%s:%d' % (self.name, self.health_meter) if bold: print_bold(msg,end=end) else: print(msg,end=end)
def show_mission(self): print_bold('MISSION:', end='\n') print('\t1.you see some huts') print('\t2.you must fight with Orc to occupy all the huts')