How to Write a Text Adventure in Python Part 2: The World Space

This is an abbreviated version of the book Make Your Own Text Adventure With Python. If you’d prefer to stick with the condensed tutorial, click here for the introduction.

All games take place in some sort of world. The world can be as simple as a chess board or as complex as the Mass Effect universe and provides the foundation for the game as a whole. All elements of a game reside in the world and some elements interact with the world. In this post, you’ll learn how to add items and enemies to your world.

The coordinate plane

A text adventure usually involves a player moving through the world one section per turn. We can think of each section as a tile on an x-y grid. Note: in most game programming the x-y coordinate plane is different from the one you learned in algebra. In the game world, (0,0) is in the top left corner, x increases to the right, and y increases to the bottom.

Creating tiles

Start by creating a module with this class:

import items, enemies

class MapTile:
    def __init__(self, x, y):
        self.x = x
        self.y = y

The import keyword means “give this module access the ‘items’ module and ‘enemies’ module. We need this because we will want to put these elements inside some of our rooms.

The MapTile class is going to provide a template for all of the tiles in our world, which means we need to define the methods that all tiles will need. First, we’ll want to display some text to the user when they enter the tile that describes the world. We also expect that some actions may take place when the player enters the tile, and that those actions change the state of the player (e.g., they pick something up, they win the game, something attacks them, etc.). Let’s add those methods now.

    def intro_text(self):
        raise NotImplementedError()

    def modify_player(self, player):
        raise NotImplementedError()

We haven’t talked about the code for the player yet, but that’s OK. The player parameter will serve as a placeholder. As you might guess, these methods aren’t going to do much in their current state. In fact, they will actually cause the program to crash! This might seem silly, but this behavior is to help us as programmers.

When thinking about our world, we don’t want to have tiles that do nothing. We may want tiles of water, tiles in a spaceship corridor, tiles with other characters, or tiles with treasure, but not empty tiles. So this MapTile class is actually just a template that all other tiles will expand on.

In the last post we learned about base classes. MapTile is actually a specific flavor of a base class. We call it an abstract base class because we don’t want to create any instances of it. In our game, we will only create specific types of tiles. We will never create a MapTile directly, instead we will create subclasses. The code raise NotImplementedError() will warn us if we accidentally create a MapTile directly.

Now on to our first tile subclass!

class StartingRoom(MapTile):
    def intro_text(self):
        return """
        You find yourself if a cave with a flickering torch on the wall.
        You can make out four paths, each equally as dark and foreboding.

    def modify_player(self, player):
        #Room has no action on player

This class extends MapTile to make a more specific type of tile. We override the intro_text and modify_player methods to implement the specific behavior that this tile should have. A method is overridden when a subclass has the same method name as a superclass. Because it’s the starting room, I didn’t want anything to happen to the player. The pass keyword simply tells Python to not do anything. You might wonder why the method is even in this class if it doesn’t do anything. The reason is because if we don’t override modify_player, the superclass’s modify_player will execute and if that happens the program will crash because of raise NotImplementedError().

Next, let’s add a class for the tile where a player will find a new item.

class LootRoom(MapTile):
    def __init__(self, x, y, item):
        self.item = item
        super().__init__(x, y)

    def add_loot(self, player):

    def modify_player(self, player):

Remember, we haven’t created player yet, but we can guess that the player will have an inventory.

Let’s define one more type of room: a room in which the player encounters an enemy.

class EnemyRoom(MapTile):
    def __init__(self, x, y, enemy):
        self.enemy = enemy
        super().__init__(x, y)

    def modify_player(self, the_player):
        if self.enemy.is_alive():
            the_player.hp = the_player.hp - self.enemy.damage
            print("Enemy does {} damage. You have {} HP remaining.".format(self.enemy.damage, the_player.hp))

This constructor should look familiar to you now. It’s very similar to the LootRoom constructor, but instead of an item, we are working with an enemy.

The logic for this room is a bit different. I didn’t want enemies to respawn. So if the player already visited this room and killed the enemy, they should not engage battle again. Assuming the enemy is alive, they attack the player and do damage to the player’s hit points.

Now that we have some basic types of tiles defined, we can make some even more specific versions. Here are some that I created:

class EmptyCavePath(MapTile):
    def intro_text(self):
        return """
        Another unremarkable part of the cave. You must forge onwards.

    def modify_player(self, player):
        #Room has no action on player

class GiantSpiderRoom(EnemyRoom):
    def __init__(self, x, y):
        super().__init__(x, y, enemies.GiantSpider())

    def intro_text(self):
        if self.enemy.is_alive():
            return """
            A giant spider jumps down from its web in front of you!
            return """
            The corpse of a dead spider rots on the ground.

class FindDaggerRoom(LootRoom):
    def __init__(self, x, y):
        super().__init__(x, y, items.Dagger())

    def intro_text(self):
        return """
        Your notice something shiny in the corner.
        It's a dagger! You pick it up.

If you remember, I also created an Ogre enemy and Gold item. You may choose to create corresponding rooms too.

Creating the world

We’re going to close out this post by actually creating a world based on the tiles we’ve defined. This delves into some advanced features so it’s OK if you don’t follow everything. I’ll explain everything briefly here, but I encourage you to read up on anything you’re interested in learning more about.

Create a new module in the same directory called Next, make a folder called “resources” that is in the same directory as the “adventuretotrial” directory and create map.txt inside. We’re going to build the world in this external file and load it into the game programatically.

I like to use a spreadsheet program and then copy the text into the map file, but you can just edit the file directly too. The goal is to lay out a grid of tiles whose names match the class names and are separated by tabs. Here’s an example in a spreadsheet:
Game tiles in spreadsheet

Remember, your map should not include MapTile, LootRoom, or EnemyRoom! Those are base classes that should not be created directly.

In the world module, add the following dictionary and method to parse the file you created.

_world = {}
starting_position = (0, 0)

def load_tiles():
    """Parses a file that describes the world space into the _world object"""
    with open('resources/map.txt', 'r') as f:
        rows = f.readlines()
    x_max = len(rows[0].split('\t')) # Assumes all rows contain the same number of tabs
    for y in range(len(rows)):
        cols = rows[y].split('\t')
        for x in range(x_max):
            tile_name = cols[x].replace('\n', '') # Windows users may need to replace '\r\n'
            if tile_name == 'StartingRoom':
                global starting_position
                starting_position = (x, y)
            _world[(x, y)] = None if tile_name == '' else getattr(__import__('tiles'), tile_name)(x, y)

The parsing method goes through each line of the file and splits the line into cells. Using a double for loop is a common way of working with grids. The x and y variables keep track of the coordinates. When we find the Starting Room, that position is saved because we will use it later. We use the global keyword to let us access the starting_position variable that lives outside of this method. The last line is the most interesting, but it’s fine if you don’t fully understand it.

The variable _world is a dictionary that maps a coordinate pair to a tile. So the code _world[(x, y)] creates the key (i.e. the coordinate pair) of the dictionary. If the cell is an empty string, we don’t want to store a tile in it’s place which is why we have the code None if tile_name == ''. However, if the cell does contain a name, we want to actually create a tile of that type. The getattr method is built into Python and lets us reflect into the tile module and find the class whose name matches tile_name. Finally the (x, y) passes the coordinates to the constructor of the tile.

Looking for an easier approach to building your world? Check out Chapter 13 of Make Your Own Text Adventure With Python.

Essentially what we’re doing is using some advanced features in Python as an alternative to something like this:

tile_map = [[FindGoldRoom(),GiantSpiderRoom(),None,None,None],

That’s hard to read and maintain. Using a text file makes changing our world easy. It’s also a lot simpler to visualize the world space.

Keep in mind that the only reason we are able to do this is because all of our tile classes derive from the same base class with a common constructor that accepts the parameters x and y.

Let’s add one more method to the world module that will make working with the tiles a little easier:

def tile_exists(x, y):
    return _world.get((x, y))

Congratulations for making it this far! If you’d like to see how to make the game easier or more difficult with different enemy types, see Chapter 12 of Make Your Own Text Adventure With Python.

Click here to get the book!

Click here for Part 3 of the abridged tutorial.

Tagged on: , , , ,

68 thoughts on “How to Write a Text Adventure in Python Part 2: The World Space

  1. Kenneth Prockl

    Thank you for this tutorial. I am getting an error on the line: tile_name = cols[x].replace(‘\n’, ”), the error is Index Error: List index is out of range. I’m pretty sure it replaces line breaks (\n) with empties (”) in the cols list and assigns it to a new varrible: title_name. I cant seem to get it to work for me. I’ve copied and pasted your github code to be sure its not a something I overlooked. What version of python are you using?

    1. Phillip

      My guess is that your text file containing the rooms does not have the same number of tabs in each rows. The error you are getting is because x is larger than the number of columns in the current row. A few lines above x_max is set to the number of columns in the first row.This assumes that all rows are “padded” with tabs to make sure they are the same length.

      This code should work on Python 3.4.x.

  2. Radd

    Im getting an error on “_world[(x, y)] = None if title_name == ” els getattr(_import_(‘titles’) . . . “. It’s tellimg me name ‘_import_ isn’t defined??

  3. Mika

    Hello Phillip,
    I’ve got some problems with enemyroom. When I create tile enemyroom, it look like this:
    world = {(2,5): tiles.Enemyroom(2,5, enemy.Cerberus(1)
    That’s how it should look like but when I write “enemy.” ,window should appear with names of superclassess, but instead in this window hp, attack and other stuff like this is written. So I am trying to write enemy.Cerberus, but error appears- “unresolved reference enemy”
    What should I do? I don’t understand what’s happening. I am just trying to call enemy just like an item, but it gives me error.

  4. Alfred

    Thanks for this tutorial, but I am getting an error, despite the fact that I have copied and pasted it. The section of code is:
    tile_name = cols[x].replace(‘\n’, ”)
    _world[(x, y)] = None if tile_name == ” else getattr(__import__(’tiles’), tile_name)(x, y)
    When I run it, I get the error (‘module’ object has no attribute ‘ ‘). Any thoughts on how to fix this? I have tried replacing ‘\n’ with ‘\r\n’, because I am running Windows, but I still got the same error. Any help will be greatly appreciated.

    1. Phillip

      Well, that suggests that cols[x] does not exist, so you are just getting an empty string. Naturally, that empty string is not found in the possible names. I would verify that your tile map is correct and make sure you understand the assumptions the code makes about that map. Happy coding!

  5. Mark

    I can’t import either of the or modules. When I type ‘import items, enemies’, I receive the following error: Traceback (most recent call last): File “”, line 1, in <modules import items ImportError: No module named 'items'. I have tried several things to fix this, like adding the file to all folders in the path, saved in its own subfolder, and only trying to import one module at a time. I have checked the code in the modules I created previous to, and they are all ok. Any ideas about what else I could try? Thanks for any help in advance!

    1. Phillip

      At this point in the tutorial there’s nothing really to run. But if you are trying to run something, try taking a look at the instructions in Part 4. You need to make sure you are executing the code from the correct directory and (possibly) that your PYTHONPATH is set properly.

  6. Ricky

    This guide as been a huge help I have only come a across one bug in my coding that I cant figure out why it is creating problems. it tells me I have invalid syntax and it highlights the import in the

    world[(x, y)] = None if tile_name == '' else getattr(import ('tiles'), tile name)(x, y)

    statement I don’t know why. can you think of why it would be doing this?

    1. Phillip

      You have a few typos. Here’s that line in my code:

      _world[(x, y)] = None if tile_name == '' else getattr(__import__('tiles'), tile_name)(x, y)
  7. Ryan

    I am getting this error when I try to load my game:

    Traceback (most recent call last):
    File “C:\Users\Ryan\PycharmProjects\Final Project – Coffee Shop\”, line 27, in
    File “C:\Users\Ryan\PycharmProjects\Final Project – Coffee Shop\”, line 12, in play
    AttributeError: ‘NoneType’ object has no attribute ‘modify_player’

    Any idea why that is happening?

    1. Phillip

      This means that no room was found at the given coordinates, so Python returned None. Make sure to review the assumptions about the map file above. If all else fails, use the file included in the GitHub project, and then make modifications to it. Happy coding!

  8. Andrew Maenza

    File “C:/Users/Drew/Desktop/Intro Game Dev Project 1\”, line 21, in load_tiles
    _world[(x, y)] = None if tile_name == ” else getattr(__import__(’tiles’), tile_name)(x, y)
    ImportError: No module named tiles

    What is the reason for this error? I checked to make sure things aren’t mistyped

  9. Cameron Leslie

    Ok, so whenever I type “import items, enemies”, it says unresolved import and unused import for both names. Any ideas on how to fix this?

      1. Caleb

        when I run the program it says File “C:\Users\ncssurfabc\Desktop\adventure\”, line 1, in
        import world
        File “C:\Users\ncssurfabc\Desktop\adventure\”, line 18, in
        tile_map = [[FindDaggerRoom(),GiantSpiderRoom(),None,None,None],
        NameError: name ‘FindDaggerRoom’ is not defined

  10. James

    Hey Phillip, I’ve been getting an attribute error on line 27,

    _world[(x, y)] = None if tile_name == ” else getattr(__import__(’tiles’), tile_name)(x, y)

    It says that the module ’tiles’ has no attribute (random mess in quotes).

    Would you happen to know what I should be looking at?

    1. Phillip Johnson Post author

      There’s a difference between '('module' object has no attribute '') and '('module' object has no attribute 'OgreRoom'). The former has been discussed previously in the comments above. If you’re getting the second error, then it is probably because Python cannot find all of your code at runtime. Make sure you are in the correct directory when you run the code and that you are running it from a terminal/command line. If all else fails, you may need to adjust your PYTHONPATH, but that’s probably the solution to another more systemic problem.

  11. Jacob

    When creating the world, how would you create a room so that if the player had a certain item (ex: the dagger), they would be able to pass, and if not, the room would be impossible to go through, so you couldn’t get to the next room without this item?

    1. Phillip Johnson Post author

      I would probably implement that in the available_actions() method on the Player class. You would need to do a check to see if the adjacent room is of type XYZ and check the player’s inventory to see if they have the required item. If either of those conditions is false, remove the action from the list of available actions.

      1. Jacob

        Thanks, your idea worked perfectly! Only I’ve come across yet another error. I’ve decided to modify the entire game to create a Pokemon-type adventure. Everything works fine, only with Pikachu (a weapon), it says that it’s missing the statement “collected = 0”. And the command is clearly there. I’ve tried deleting and retyping this part of the code, and it’s still not cooperating. Suggestions?

  12. xxsns


    First of all I’d like to thank you for your great guide. I had no idea how to get a game loop going and this really helped me out a lot.

    However, I’m running into an error that already has been answered by you, though I don’t quite follow your explanation. (the error was a little different too)

    ” _world[(x, y)] = None if tile_name == “” else getattr(__import__(“tiles”), tile_name)(x, y)
    AttributeError: module ’tiles’ has no attribute ‘ ”
    is the error I encounter.

    I’m genuinely clueless, I’m very sorry

    1. xxsns

      AttributeError: module ’tiles’ has no attribute ‘StartingRoom

      sorry I quoted the error wrongly.

      I’m running it from command line and pythonpath has to be okay since any other python script runs smoothly from commandline.
      what am I missing?

      1. Phillip Johnson Post author

        It looks like you’re having the same problem as Alfred was above. I would suggest adding a print(cols[x]) so you can actually see the value of tile_name. At some point, you’ll see that tile_name does not have a value. This should give you a hint as to where the problem is in your map file because you’ll see where the code fails.

        1. xxsns

          Thanks a lot, for some reason cols[x] prints empty a few empty lines even though I replace line breaks with ”

          I tripple checked the map.txt and there is no whitespace, only linebreaks, text and tabs.
          the rest of the print looks normal

  13. Jake

    Thanks for your continued support for this tutorial! It’s been really fun and informative as a novice programmer. I’ve solved a few things from your responses above but now I’m running into this error:

    _world[(x, y)] = None if tile_name == '' else getattr(__import__('tiles'), tile_name)(x, y)
    TypeError: __init__() missing 1 required positional argument: 'y'

    I’m really at a loss as to what it’s referencing. Any thoughts?

    1. Phillip Johnson Post author

      It most likely means there is a problem with the __init__ method of the tile you are trying to create. To figure out the problem tile, try printing out tile_name, x, and y. When you know the tile causing the problem, make sure it’s initializer is correct. If you’re still stuck, compare your code with the code in the GitHub repo and that should pinpoint the error.

  14. Max

    I have been using this tutorial heavily and I have to say it explains quite a lot. However, I get this error every time I attempt to run it

    _world[(x, y)] = None if tile_name == ” else getattr(__import__(’tiles’), tile_name)(x, y)
    AttributeError: ‘module’ object has no attribute ‘End_game’
    I have not used End_game anywhere and I am incredibly confused.
    Thanks for the tutorial

  15. Meris

    I’m having a problem running the GiantSpiderRoom class on Python 3.5.2. I keep getting the error:

    ” File “/home/adventuretutorial/”, line 81, in __init__
    super().__init__(x, y, enemies.GiantSpider())
    TypeError: super() takes at least 1 argument (0 given)”

    My code is this:

    “class GiantSpiderRoom(EnemyRoom):
    def __init__(self, x, y):
    super().__init__(x, y, enemies.GiantSpider())”

    I’ve looked at your previous answer to this problem, but it was for Python 2.x, not 3.x, so I’m very confused.

      1. Meris

        Nevermind, I started this project on WIndows 8.1, but became so frustrated with Windows Command Line that I dual booted my laptop with Ubuntu 16.04 and the default call of “python” is for python 2.7.11, not python 3.x.

  16. Meris

    I’ve been having some problems with the map.txt, actually. I’ve created a 13×13 map using LibreOffice, but no matter what I do, it says:

    ” File “/home/adventuretutorial/”, line 12, in load_tiles
    tile_name = cols[x].replace(‘\n’, ”)
    IndexError: list index out of range”

    I understand that this means that what I actually created did not have the same number of rows in each column, but I cannot figure out what is wrong. I copied and pasted your map.txt in place of mine and the code still gave me the same error. (I also copied and pasted your script to see if mine was the problem, but I received the same error again.

    I downloaded your GitHub file and have modified it to include everything I wanted, like healing items and more weapons and enemies, but whenever I try to modify your map.txt file, I receive this same error. Is there another way to creating a large map?

    1. Phillip Johnson Post author

      Try printing x_max and x to help debug. You will probably see that x_max is something like 12 when you expect 13. You don’t have to use LibreOffice. Just open the file in a text editor and turn on “show whitespace characters”.

      Take a look at the first comment string on this post. That may also help you out.

  17. Jake

    Hi Phillip,

    Excellent tutorial for a beginner like myself – I’ve really enjoyed it, so thank you!

    I’m running this in Python 2.7 on Ubuntu, and I’ve changed the super() calls to suit, but I’m getting an error, and not sure if it is because of my version, or I’m doing something wrong. Could you help?

    _world[(x, y)] = None if tile_name == ” else getattr(__import__(’tiles’), tile_name)(x, y)
    TypeError: GiantSpiderRoom() takes exactly 1 argument (2 given)

    Here is the tile code:
    def GiantSpiderRoom(EnemyRoom):
    def __init__(self, x, y):
    super(GiantSpiderRoom, self).__init__(x, y, enemies.GiantSpider())

    1. Phillip Johnson Post author

      This tutorial is written for Python 3, not 2. I believe some people in the comments may have posted some code changes that make it compatible with Python 2, but I highly recommend using Python 3 in general.

  18. Benjamin Bridges

    I have everything in place but when I run, only the ViewInventory action is utilized. I don’t think any of the adjacent rooms are being noticed.

    I tried modifying to fix it and I still get the same issue. The game runs, but I can only view my inventory.

    1. Phillip Johnson Post author

      You might try debugging by printing out information. In load_tiles(), you can do print("X: " + str(x) + " Y: " + str(y)) and print(_world[(x, y)]) to see all the rooms being added. Then you can try adding print statements to the adjacent_moves method of the Player class. If all else fails, compare your code and map to what I have up at GitHub.

  19. Allie Curtis

    Hello Phillip!

    I’m in Chapter 13 of your book, Expanding the World. I have isolated my problem down to this block of code:

    def parse_world_dsl():
        if not is_dsl_valid(world_dsl):
            raise SyntaxError("DSL is invalid!")
        dsl_lines = world_dsl.splitlines()
        dsl_lines = [x for x in dsl_lines if x]
        for y, dsl_row in enumerate(dsl_lines):
            row = []
            dsl_cells = dsl_row.split()
            dsl_cells = [c for c in dsl_cells if c]
            for x, dsl_cell in enumerate(dsl_cells):
                tile_type = tile_type_dict[dsl_cell]
                row.append(tile_type(x, y) if tile_type else None)

    When I run my, I get the error:

    AttributeError: ‘NoneType’ object has no attribute ‘intro_text’

    I have erased the entire chapter’s code and re-entered it, so I know it’s all entered correctly. However I must be missing something. My assumption is that the rows are not correctly appending to the world_map list, so when my game tries to access the intro_text for the selected tile it returns ‘None’ as the tile type. This is totally a guess, however.

    1. Allie Curtis

      The actual error is at this point:

      room = world.tile_at(player.x, player.y)
      choose_action(room, player)

      and when I added print(room) below room = world.tile I got ‘None’ and then the error again, so I’m not getting a value for my tile_at function D:

      And I’d like to say that your book is fantastic! I’ve only ever made choose-your-own-adventure games with tons and tons of if and elif statements, so this tutorial has been invaluable in making a more involved game. Thank you so much!

      1. Allie Curtis

        I’m so sorry, I hate that you can’t edit comments on this. Found one error:

        I changed this:
        dsl_cells = dsl_row.split()

        to this:
        dsl_cells = dsl_row.split("|")

        but I still get the same error, unfortunately. From I tried print(world.world_map) and got an empty list, so the items “ST” and “VT” etc. aren’t being added to the world_map list for some reason :S

        1. Phillip Johnson Post author

          Hi Allie,

          You’re on the right track here! The error you are getting is because nothing is loaded into the map. When the game tries to find the starting tile, nothing is there, so Python uses None, which of course does not have an intro_text method.

          The trick is going to be figuring out what’s wrong with your DSL. I would add print statements to the parse_world_dsl() method. If you need to, do it after each line of code. Make sure your tile_type_dict is also correct. If all else fails, you can do an online diff between my code and yours.

          Happy coding!

          1. Allie Curtis

            Hey Phillip!

            Thank you so much for answering. I ended up getting it, I needed to add world.parse_world_dsl() above player = Player() in 😀

            Thanks for your help!

  20. Kelvin

    Just bought the book and I’m in the world building section. Which is more “pythonic” using the excel spreadsheet to make the world or the DSL method?

    1. Phillip Johnson Post author

      Both methods use a DSL, so it’s more a question of putting the layout as a string directly in the code or storing it in a file. I don’t think either method is more pythonic than the other. However, I personally prefer to store it in a file. Especially if you had a game with a large world and/or multiple levels, trying to put all of that directly in code would get annoying. However, reading from a file adds some complexity that I wanted to avoid when writing the book so I went with the simpler method.

      1. Kelvin

        Thanks for answering my questions! This book is awesome and been a great resource for understanding classes and python in general in a fun way.

  21. Jarvis

    Hey Phillip,

    First of this is a great resource for beginners like me, so thank you!

    Second, if I wanted to input the coordinates of the rooms directly into my tiles, how would I do it? Currently this is an example of my code:

    class Room():#template for all rooms
        def __init__(self, x, y):#constructor
        def intro_text(self):
            raise NotImplementedError()
        def modify_player(self, player):
            raise NotImplementedError()
    class road_four(Room):
        def __init__(self):
            super().__init__(2, 2)
        def intro_text(self):
            return """It's an empty road. There's a path forward and to your left.
    The right is blocked.
    Where will you go?"""
        def modify_player(self, player): #no action on player

    Notice how I input the coordinates into the super().__int__ of the subclass itself. But this doesn’t seem to be working. for example when I use:

    __world = {}
    starting_position = (0, 0)
    def tile_exists(x, y):
             return __world.get((x, y))

    to pull up the room at any particular coordinate I get None.

    Please help!

    1. Phillip Johnson Post author

      Well if you aren’t using the map method of creating the tiles, you need something else to put the tiles into the __world variable. Something like:

      __world[(0,0)] = starting_room()
      __world[(2,2)] = room_four()

      But this is really not a great idea and will give you a lot of headaches. If you don’t want to use the map file, at least something like this is still preferable:

      tile_map = [[FindGoldRoom(),GiantSpiderRoom(),None,None,None],
  22. Someone

    Can you help? The section on making the map is rather vague, and I have no money to buy the book.
    I’ve made the map to the best of my ability, and I keep getting an error:

    _world[(x, y)] = None if tile_name == '' else getattr(__import__('tiles'), tile_name)(x, y)
    AttributeError: module 'tiles' has no attribute ''

    I really have no idea what to do at this point. I apologize if someone has asked this before, but I really can’t find anything that will help.

  23. Daniel


    Bought your book.

    Really enjoyed it.

    Looking to expand my game a bit more than I have and make enemy tiles respawn.

    Not sure how I would do it though.

    1. Phillip Johnson Post author

      First I would create a respawn method in the Enemy class to set the HP back to 100.

      Next, I would create a new method called pre_check and put it into MapTile with a pass body. Then call that method in the play method in Finally, override the pre_check method in EnemyTile and do something like this:

      r = random.random()
      if r < 0.20:

      Hope that helps and happy programming!

      1. Daniel

        Wouldn’t this just give the same enemy that was generated originally 100 HP again?

        Trying to also make the specific tile re spawn a random enemy when it does.

        1. Phillip Johnson Post author

          Sure, that’s actually easier. Move the code that creates an enemy into a new method like def create_enemy(self). Then call the create_enemy method in both the init and pre_check methods.

  24. Caleb

    when I run the code I keep getting the error File “C:\Users\ncssurfabc\Desktop\adventure\”, line 16, in load_tiles
    _world[(x, y)] = None if tile_name == ” else getattr(__import__(’tiles’), tile_name)(x, y)
    AttributeError: module ’tiles’ has no attribute ‘ def play():’
    and I looked over my code and cannot find the problem _world[(x, y)] = None if tile_name == ” else getattr(__import__(’tiles’), tile_name)(x, y)

    1. Phillip Johnson Post author

      Go back and review your code against the code in the tutorial. You can also view all the code on GitHub. I can’t tell specicially without seeing your code, but there’s no reason that the tiles module should be trying to load something called  def play(). It’s also suspicious that there is a space before def.

Leave a Reply

Your email address will not be published. Required fields are marked *