Spawning enemies is just a case of picking a tile that’s either a floor or a corridor. As BRP highlighted: How you go about this is completely up to you and will depend on your game.
So, to spawn 10 enemies in any open space (corridors and rooms) you could:
import dungeonGenerator
from random import randint
# generate your level here, then
enemiesSpawned = 0
while enemiesSpawned < 10:
# where levelSize is the size of the dungeon you've generated
x = randint(0, levelSize)
y = randint(0, levelSize)
if dungeon.grid[x][y] == dungeonGenerator.FLOOR or dungeon.grid[x][y] == dungeonGenerator.CORRIDOR:
enemiesSpawned += 1
# spawn your enemy however you like,
# set their world positions by multiplying x and y by the level tile size
Or you could pick a room at random then pick a tile within the room:
from random import randint, choice
room = choice(dungeon.rooms)
x = randint(room.x, room.x + room.width)
y = randint(room.y, room.y + room.height)
ob = scene.addObject('Enemy')
ob.worldPosition.x = x * tileSize
ob.worldPosition.y = y * tileSize
Or you could scatter them randomly across the entire level based on some probability:
from random import randint
# generate your level
for x, y, tile in dungeon:
if tile == dungeonGenerator.FLOOR or tile == dungeonGenerator.CORRIDOR:
if randint(0, 100) < 35: # or whatever probability you want to use
# spawn you enemy
# set their world position by multiplying the x, y values by the tile size
(all python untested)
Or a host of many other methods, like using the findNeighbour() and findNeighbourDirect() functions to place them away from things, or picking random tiles and then using the pathFinding() functions to make sure they’re not too close to the player, or using modulo on the x and y co-ordinates to make a pattern of enemy placements. And so forth…
@BRP: Thanks for your feedback :). In the module the path finding functions are right at the end and have doc strings (Google style). In the article they’re covered (albeit briefly) at the end of the ‘Helper functions and everything else’ section. There’s no pictures to show for them, so it’s easy to miss. It also covers converting world co-ordinates to grid ones and vice-versa. If those bits (or any other bits) don’t make sense or aren’t clear enough let me know and I’ll make some edits.