def take_damage (self, damage): self.health -= damage if 0 > self.health: S.add_status("%s is killed!" % (self.name)) self.health = 0 M.gameworld[self.x][self.y].characters.remove(self) self.drop_all()
def drop(self): if self.items: items = self.items[:] if self.equipped: for i in self.equipped: items.remove(i) if items: count = item_selector(items, title="DROP ITEM") if not count==-1: item = items[count] self.items.remove(item) M.gameworld[self.x][self.y].items.append(item) S.add_status("%s dropped %s." % (self.name, item.name)) else: S.add_status("Nothing to drop!")
def pick_up(self): if M.gameworld[self.x][self.y].items: if len(M.gameworld[self.x][self.y].items) > 1: count = item_selector(M.gameworld[self.x][self.y].items) else: count = 0 if not count==-1: item = M.gameworld[self.x][self.y].items[count] M.gameworld[self.x][self.y].items.remove(item) if item.money: self.money = self.money + item.money.value else: self.items.append(item) S.add_status ("%s picked up %s." % (self.name, item.name)) else: S.add_status ("Nothing to pick up!")
def fire(self, x, y, dx, dy): # The distance this bullet has traveled. steps = 0 shot_accuracy=self.accuracy libtcod.map_compute_fov (P.player.fov, x, y, int(P.player.view_distance*1.5), True,libtcod.FOV_SHADOW) R.render() libtcod.line_init(x, y, dx, dy) lx,ly=libtcod.line_step() while (not lx is None): steps = steps + 1 if not M.gameworld[lx][ly].characters: libtcod.console_set_char_background( cons.game_console, lx, ly, libtcod.white, libtcod.BKGND_OVERLAY) libtcod.console_blit(cons.game_console,0,0,M.MAP_WIDTH,M.MAP_HEIGHT,0,0,0,1) libtcod.console_flush() lx,ly=libtcod.line_step() else: if random.random() <= shot_accuracy: S.add_status("You hit!") libtcod.console_set_char_background( cons.game_console, lx, ly, libtcod.red, libtcod.BKGND_OVERLAY) libtcod.console_blit(cons.game_console,0,0,M.MAP_WIDTH,M.MAP_HEIGHT,0,0,0,1) libtcod.console_flush() M.gameworld[lx][ly].characters[-1].take_damage( int(self.damage*random.uniform(self.accuracy, 1.0))) else: S.add_status("You fire and miss!") break self.ammo = self.ammo-1 self.owner.name = self.owner.name.rsplit('(')[0] + '(' + str(self.ammo) + ')' P.player.compute_fov()
def attack (self, character): damage = self.strength*random.randint(self.strength//2, self.strength*2) if random.random() <= self.to_hit: S.add_status("%s hits %s!" % (self.name, character.name)) if damage > (0.5*character.max_health): S.add_status("It's super effective!") character.take_damage(damage) else: S.add_status("%s swings and misses." % (self.name))
def show_inventory(self): if self.items: count = I.item_selector(self.items, equipped=self.equipped) if not count == -1: item = self.items[count] if item.health: self.health = self.health+item.health.value if self.health > self.max_health: self.health = self.max_health S.add_status("%s health added." % (item.health.value)) self.items.remove(item) else: if not item in self.equipped: self.equipped.append(item) S.add_status("%s equipped." % (item.name)) else: S.add_status("%s unequipped." % (item.name)) self.equipped.remove(item)
def shoot(self): gun = -1 for i in range(len(self.equipped)): if self.equipped[i].gun and self.equipped[i].gun.ammo > 0: gun = i if not gun==-1: class Target: def __init__(self, x, y): self.x = x self.y = y target = Target(self.x, self.y) libtcod.console_blit(cons.game_console,0,0,M.MAP_WIDTH,M.MAP_HEIGHT,0,0,0,1) libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) while not key.vk == libtcod.KEY_SPACE: R.render() if key.pressed: if ord('k') == key.c: target.y=target.y-1 elif ord('j') == key.c: target.y=target.y+1 elif ord('h') == key.c: target.x=target.x-1 elif ord('l') == key.c: target.x=target.x+1 elif ord('y') == key.c: target.x=target.x-1 target.y=target.y-1 elif ord('u') == key.c: target.x=target.x+1 target.y=target.y-1 elif ord('i') == key.c: target.x=target.x-1 target.y=target.y+1 elif ord('o') == key.c: target.x=target.x+1 target.y=target.y+1 libtcod.line_init(self.x, self.y, target.x, target.y) x,y=libtcod.line_step() # Clear the console that shows our target line. libtcod.console_clear(cons.gun_console) # Draw the target line on the gun console. while (not x is None): if (M.gameworld[x][y].is_floor() and libtcod.map_is_in_fov (player.fov, x, y) ): libtcod.console_set_char_background(cons.gun_console, x, y, libtcod.white, libtcod.BKGND_OVERLAY) target.x=x target.y=y x,y=libtcod.line_step() else: break # Draw the gun console to the root console. libtcod.console_blit(cons.gun_console,0,0,M.MAP_WIDTH,M.MAP_HEIGHT,0,0,0,0,0.5) libtcod.console_flush() key = libtcod.console_wait_for_keypress(True) self.equipped[gun].gun.fire(self.x, self.y, target.x, target.y) else: S.add_status("No gun in hand!")