def target_tile(max_range=None): """Selects a tile either from a mouse click or selection""" x = player.x y = player.y mx = t.state(t.TK_MOUSE_X) my = t.state(t.TK_MOUSE_Y) render.draw_cursor(x, y) render.draw_max_range(player, max_range) message( "Select using the mouse or movements keys. Use M1, Enter, 5 to confirm and ESC/SPACE to cancel", colours.light_cyan) render_all() key = t.read() while key not in (t.TK_MOUSE_LEFT, t.TK_ENTER, t.TK_KP_ENTER, t.TK_KP_5, t.TK_ESCAPE, t.TK_SPACE): dx = x dy = y if key in DIR: (dir_x, dir_y) = DIR.get(key) dx += dir_x dy += dir_y zx = t.state(t.TK_MOUSE_X) zy = t.state(t.TK_MOUSE_Y) if zx != mx or zy != my: mx = zx my = zy dx = mx dy = my #Check for Out of Bounds/Range if 0 < dx and dx < MAP_WIDTH and 0 < dy and dy < MAP_HEIGHT: #Check for within range if player.distance(dx, dy) <= max_range and not gmap.is_blocked(dx, dy, None, tilesOnly=True) \ and gmap.is_explored(dx, dy): x = dx y = dy render.draw_cursor(x, y) if t.has_input(): key = t.read() else: key = None #Check if it was a mouse confirm or a key confirm/cancel render.draw_cursor(None, None) render.draw_max_range(None, None) if key == t.TK_MOUSE_LEFT: x = t.state(t.TK_MOUSE_X) y = t.state(t.TK_MOUSE_Y) #Cancel selection process elif key in (t.TK_ESCAPE, t.TK_SPACE): return (None, None) #Return coordinates selected return (x, y)
def get_free_spaces(self, gamemap: gamemap.GameMap, number: int) -> Iterator[Tuple[int, int]]: """Iterate over the x,y coordinates of up to `number` spaces.""" for _ in range(number): x = random.randint(self.x1 + 1, self.x2 - 2) y = random.randint(self.y1 + 1, self.y2 - 2) if gamemap.is_blocked(x, y): continue yield x, y
def draw_max_range(player, max_range): """Draws a dark overlay over anything not in range""" t.layer(OVERLAY_LAYER) if max_range is None: t.clear_area(0, 0, MAP_WIDTH, MAP_HEIGHT) return set_colour(colours.black, 200) for x in range(0, MAP_WIDTH): for y in range(0, MAP_HEIGHT): if player.distance(x, y) > max_range or is_blocked(x, y, None, tilesOnly=True): t.put(x, y, BLOCK_CHAR)
def draw_fireball(mx, my): """Draw an expanding/contracting fireball""" t.layer(EFFECT_LAYER) effected_points = [(mx + dx, my + dy) for (dx, dy) in ((-1, -1), (-1, 1), (-1, 0), (0, -1), (0, 0), (0, +1), (+1, 0), (+1, -1), (+1, +1))] for (x, y) in effected_points: if is_blocked(x, y, None, tilesOnly=True): effected_points.remove((x, y)) set_colour(colours.red) for (x, y) in effected_points[4:-4]: t.put(x, y, chr(177)) t.refresh() t.delay(50) set_colour(colours.orange) for (x, y) in effected_points[2:-2]: t.put(x, y, chr(177)) t.refresh() t.delay(50) set_colour(colours.red) for (x, y) in effected_points: t.put(x, y, chr(177)) t.refresh() t.delay(50) set_colour(colours.orange) for (x, y) in effected_points: t.put(x, y, chr(177)) t.refresh() t.delay(50) set_colour(colours.red) for (x, y) in effected_points: t.put(x, y, chr(177)) t.refresh() t.delay(100) t.clear_area(effected_points[0][0], effected_points[0][1], effected_points[-1][0], effected_points[-1][1]) set_colour(colours.orange) for (x, y) in effected_points[2:-2]: t.put(x, y, chr(177)) t.refresh() t.delay(100) t.clear_area(effected_points[0][0], effected_points[0][1], effected_points[-1][0], effected_points[-1][1]) set_colour(colours.dark_red) for (x, y) in effected_points[4:-4]: t.put(x, y, chr(177)) t.refresh() t.delay(150) for point in effected_points: t.clear_area(point[0], point[1], 1, 1) t.refresh() return effected_points
def move(self, dx, dy): """Moves the object by the x,y deltas""" if not gmap.is_blocked(self.x + dx, self.y + dy, objects): self.x += dx self.y += dy
def place_objects(room): """places objects like monsters and items into the room""" num_monsters = randint(0, MAX_ROOM_MONSTERS) for i in range(num_monsters): x = randint(room.x1 + 1, room.x2 - 1) y = randint(room.y1 + 1, room.y2 - 1) if not gmap.is_blocked(x, y, objects): if randint(0, 100) < 80: fighter_c = Fighter(hp=10, defense=0, power=3, death_function=monster_death) ai_c = BasicMonster() monster = GameObject(x, y, chr(7), 'blob', colours.desaturated_green, blocks=True, fighter=fighter_c, ai=ai_c) else: fighter_c = Fighter(hp=12, defense=1, power=3, death_function=monster_death) ai_c = BasicMonster() monster = GameObject(x, y, chr(4), 'Big blob', colours.darker_green, blocks=True, fighter=fighter_c, ai=ai_c) objects.append(monster) num_items = randint(0, MAX_ROOM_ITEMS) for i in range(num_items): x = randint(room.x1 + 1, room.x2 - 1) y = randint(room.y1 + 1, room.y2 - 1) if not gmap.is_blocked(x, y, objects): num = randint(0, 100) if num < 55: item_component = Item(use_function=cast_heal) item = GameObject(x, y, '!', 'healing potion', colours.violet, item=item_component, always_visible=True) elif num < 55 + 15: item_component = Item(use_function=cast_lightning) item = GameObject(x, y, '#', 'Bolt Scroll', colours.light_blue, item=item_component, always_visible=True) elif num < 55 + 15 + 15: item_component = Item(use_function=cast_confuse) item = GameObject(x, y, '#', 'Confuse Scroll', colours.blue, item=item_component, always_visible=True) elif num <= 55 + 15 + 15 + 15: item_component = Item(use_function=cast_fireball) item = GameObject(x, y, '#', 'Fireball Scroll', colours.dark_blue, item=item_component, always_visible=True) objects.append(item) item.send_to_back()