Difference between revisions of "Complete Roguelike Tutorial, using python+libtcod, part 5"
m (next part link was in wrong location) |
(added "fighting orderly" section) |
||
Line 111: | Line 111: | ||
== Game states == | == Game states == | ||
Last stop before we get to the actual combat! Our input system has a fatal flaw: player actions (movement, combat) and other keys (fullscreen, other options) are handled the same way. We need to separate them. This way, if the player pauses or dies he can't move or fight, but can press other keys. We also want to know if the player's input means he finished his turn or not; changing to fullscreen shouldn't count as a turn. I know, I know, they're just simple details -- but the game would be very annoying without them! We just need two global variables, the game state and the player's last action (set before the main loop). | Last stop before we get to the actual combat system! Our input system has a fatal flaw: player actions (movement, combat) and other keys (fullscreen, other options) are handled the same way. We need to separate them. This way, if the player pauses or dies he can't move or fight, but can press other keys. We also want to know if the player's input means he finished his turn or not; changing to fullscreen shouldn't count as a turn. I know, I know, they're just simple details -- but the game would be very annoying without them! We just need two global variables, the game state and the player's last action (set before the main loop). | ||
Line 146: | Line 146: | ||
== Fighting orderly == | |||
For the first part of the combat system we have to manage the player and monsters taking combat turns, and the player making an attack. To make it simple, the player takes his turn first, in ''handle_keys''. If he took a turn, all the monsters take theirs. This goes after the ''handle_keys'' block in the main loop: | |||
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python"> #let monsters take their turn | |||
if game_state == 'playing' and player_action != 'didnt-take-turn': | |||
for object in objects: | |||
if object != player: | |||
print 'The ' + object.name + ' growls!'</syntaxhighlight></div> | |||
That's just a debug message, in the next part we'll call an AI routine to move and attack. Now we'll take care of the player input. Since he can attack now, instead of calling ''move'' (inside ''handle_keys'') like this: | |||
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python"> player.move(0, -1) | |||
fov_recompute = True</syntaxhighlight></div> | |||
...we'll make a function called ''player_move_or_attack'' and replace all those 4 blocks with calls like this: | |||
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python"> player_move_or_attack(0, -1)</syntaxhighlight></div> | |||
The function itself has a few lines of code but doesn't do anything extraordinary. It has to check if there's an object in the direction the player wants to move. If so, a debug message will be printed (replaced later by an actual attack), if not, just move there. | |||
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python">def player_move_or_attack(dx, dy): | |||
global fov_recompute | |||
#the coordinates the player is moving to/attacking | |||
x = player.x + dx | |||
y = player.y + dy | |||
#try to find an attackable object there | |||
target = None | |||
for object in objects: | |||
if object.x == x and object.y == y: | |||
target = object | |||
break | |||
#attack if target found, move otherwise | |||
if target is not None: | |||
print 'The ' + target.name + ' laughs at your puny efforts to attack him!' | |||
else: | |||
player.move(dx, dy) | |||
fov_recompute = True</syntaxhighlight></div> | |||
Alright, the code is ready to test! No damage is done yet but you can see the monsters taking their turns after you (notice when you switch to fullscreen it doesn't count as a turn, yay!), and you can bump into them to heroically but unsuccessfully try to destroy them! | |||
The whole code so far is available [[Complete Roguelike Tutorial, using python+libtcod, part 5 code|here]]. | |||
Guess what's next? | |||
[[Complete Roguelike Tutorial, using python+libtcod, part 6|Go on to the next part]]. | [[Complete Roguelike Tutorial, using python+libtcod, part 6|Go on to the next part]]. |
Revision as of 16:16, 18 August 2010
This is part of a series of tutorials; the main page can be found here. |
Preparing for combat
Populating the dungeon
Can you feel that? It's the sense of anticipation in the air! That's right, from now on we won't rest until our game lets us smite some pitiful minions of evil, for great justice. It'll be a long journey, and the code will become more complicated, but there's no point in tip-toeing around it any more; it's a game and we wanna play it. Some sections will be a bit of a drag, but if you survive that, the next part will be much more fun.
First, the monster placement. With our generic object system, it's pretty easy to create a new object and append it to the objects list. All we need to do is, for each room, create a few monsters in random positions. So we'll create a function to populate a room:
def place_objects(room):
#choose random number of monsters
num_monsters = libtcod.random_get_int(0, 0, MAX_ROOM_MONSTERS)
for i in range(num_monsters):
#choose random spot for this monster
x = libtcod.random_get_int(0, room.x1, room.x2)
y = libtcod.random_get_int(0, room.y1, room.y2)
if libtcod.random_get_int(0, 0, 100) < 80: #80% chance of getting an orc
#create an orc
monster = Object(x, y, 'o', 'orc', libtcod.desaturated_green,
blocks=True)
else:
#create a troll
monster = Object(x, y, 'T', 'troll', libtcod.darker_green,
blocks=True)
objects.append(monster)
The constant MAX_ROOM_MONSTERS = 3 will be defined along with the other constants so it can be easily tweaked.
I decided to create orcs and trolls, but you can choose anything else. In fact, you should change this function as much as you want; this is probably the simplest method. As an alternative, you could define a number of pre-set squads and choose one of them randomly, each squad being a combination of some monsters (for example, one troll and a few orcs, or 50% orcs and 50% goblin archers). The sky is the limit! You can also place items in the same manner; we'll get there later. Remember that this tutorial is only a starting point!
Now, for the dungeon generator to place monsters in each room, call this function right after create_room, inside make_map:
#add some contents to this room, such as monsters
place_objects(new_room)
There! I also removed the dummy NPC from the initial objects list (before the main loop), it won't be needed anymore.
Blocking objects
Here, we'll add a few bits that are necessary before we can move on. First, blocking objects. We don't want more than one monster standing in the same tile, because only one will show up and the rest will be hidden! Some objects, especially items, don't block (it would be silly if you couldn't stand right next to a healing potion!), so each object will have an extra "blocks" property. We'll take the opportunity to allow each object to have a name, which will be useful for game messages and the GUI. Just add those 2 properties to the beggining of the Object 's __init__ method:
def __init__(self, x, y, char, name, color, blocks=False):
self.name = name
self.blocks = blocks
Now, we'll create a function that tests if a tile is blocked, whether due to a wall or an object blocking it. It's very simple, but it will be useful in a bunch of places.
def is_blocked(x, y):
#first test the map tile
if map[x][y].blocked:
return True
#now check for any blocking objects
for object in objects:
if object.blocks and object.x == x and object.y == y:
return True
return False
Ok, time to give it some use! First, the Object 's move method, change the if condition to:
if not is_blocked(self.x + dx, self.y + dy):
The player (or anyone, for that matter) can't move over a blocking object now! Next, the place_objects function -- see if the tile is unblocked before placing a new monster:
#only place it if the tile is not blocked
if not is_blocked(x, y):
Don't forget to indent the lines after that. This guarantees that monsters don't overlap! And since objects have 2 more properties, we need to define them whenever we create one, such as the line that creates the player object:
#create object representing the player
player = Object(0, 0, '@', 'player', libtcod.white, blocks=True)
And the code that creates the monsters:
if libtcod.random_get_int(0, 0, 100) < 80: #80% chance of getting an orc
#create an orc
monster = Object(x, y, 'o', 'orc', libtcod.desaturated_green,
blocks=True)
else:
#create a troll
monster = Object(x, y, 'T', 'troll', libtcod.darker_green,
blocks=True)
Game states
Last stop before we get to the actual combat system! Our input system has a fatal flaw: player actions (movement, combat) and other keys (fullscreen, other options) are handled the same way. We need to separate them. This way, if the player pauses or dies he can't move or fight, but can press other keys. We also want to know if the player's input means he finished his turn or not; changing to fullscreen shouldn't count as a turn. I know, I know, they're just simple details -- but the game would be very annoying without them! We just need two global variables, the game state and the player's last action (set before the main loop).
game_state = 'playing'
player_action = None
Inside handle_keys, the movement/combat keys can only be used if the game state is "playing":
if game_state == 'playing':
#movement keys
We'll also change the same function so it returns a string with the type of player action. Instead of returning True to exit the game, return a special string:
return 'exit' #exit game
And testing for all the movement keys, if the player didn't press any, then he didn't take a turn, so return a special string in that case:
else:
return 'didnt-take-turn'
After the call to handle_keys in the main loop, we can check for the special string 'exit' to exit the game. Later we'll do other stuff according to the player_action string.
player_action = handle_keys()
if player_action == 'exit':
break
Fighting orderly
For the first part of the combat system we have to manage the player and monsters taking combat turns, and the player making an attack. To make it simple, the player takes his turn first, in handle_keys. If he took a turn, all the monsters take theirs. This goes after the handle_keys block in the main loop:
#let monsters take their turn
if game_state == 'playing' and player_action != 'didnt-take-turn':
for object in objects:
if object != player:
print 'The ' + object.name + ' growls!'
That's just a debug message, in the next part we'll call an AI routine to move and attack. Now we'll take care of the player input. Since he can attack now, instead of calling move (inside handle_keys) like this:
player.move(0, -1)
fov_recompute = True
...we'll make a function called player_move_or_attack and replace all those 4 blocks with calls like this:
player_move_or_attack(0, -1)
The function itself has a few lines of code but doesn't do anything extraordinary. It has to check if there's an object in the direction the player wants to move. If so, a debug message will be printed (replaced later by an actual attack), if not, just move there.
def player_move_or_attack(dx, dy):
global fov_recompute
#the coordinates the player is moving to/attacking
x = player.x + dx
y = player.y + dy
#try to find an attackable object there
target = None
for object in objects:
if object.x == x and object.y == y:
target = object
break
#attack if target found, move otherwise
if target is not None:
print 'The ' + target.name + ' laughs at your puny efforts to attack him!'
else:
player.move(dx, dy)
fov_recompute = True
Alright, the code is ready to test! No damage is done yet but you can see the monsters taking their turns after you (notice when you switch to fullscreen it doesn't count as a turn, yay!), and you can bump into them to heroically but unsuccessfully try to destroy them!
The whole code so far is available here.
Guess what's next?