By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Python tutorial: apparently gamemap: GameMap in entity.py does not work?


Active Coder

I am following the Python Roguelike tutorial. I am currently in the 6th part of it. And in my source code there is an error I cannot fix myself.

When I am trying to run main.py, the window with the game gets generated. However, when I try to make any move, an error occurs:

Traceback (most recent call last):
  File "C:\Users\DELL\Desktop\My_Roguelike\main.py", line 65, in <module>
  File "C:\Users\DELL\Desktop\My_Roguelike\main.py", line 62, in main
  File "C:\Users\DELL\Desktop\My_Roguelike\input_handlers.py", line 32, in handle_events
  File "C:\Users\DELL\Desktop\My_Roguelike\actions.py", line 89, in perform
    if self.blocking_entity:
  File "C:\Users\DELL\Desktop\My_Roguelike\actions.py", line 51, in blocking_entity
    return self.engine.game_map.get_blocking_entity_at_location(*self.dest_xy)
  File "C:\Users\DELL\Desktop\My_Roguelike\actions.py", line 17, in engine
    return self.entity.gamemap.engine
AttributeError: 'Entity' object has no attribute 'gamemap'

I am not really sure what is not working, but I have a suspicion. In entity.py I have a class Entity with a line:

gamemap: GameMap

And I believe that for some kinda reason, this is where things go bad (I think?). However, I have totally no idea WHY it's not working... 🙁

The full source code is on my GitHub, here. I would appreciate any help.
  1. Entity Class in entity.py:
    • In the Entity class, you added a gamemap attribute to the constructor and initialized it with gamemap=None. This attribute is then set when an entity is spawned or placed. This looks fine.
    • You added a `place`method to the Entity class that sets the location and optionally changes the gamemap.
  2. Actions in actions.py:
    • You modified the `Action`classes to take the entity in the constructor, and the perform method no longer takes engine as an argument.
  3. Changes in main.py:
    • You changed the creation of the Engine object, passing the event_handler and game_map directly.
  4. Event Handling in input_handlers.py:
    • In the ev_keydown method of EventHandler, you changed the initialization of BumpAction and EscapeAction to include the entity.
Now, let's check the error in the traceback:

AttributeError: 'Entity' object has no attribute 'gamemap'

This error is pointing to the fact that somewhere in your code, you're trying to access the gamemap attribute of an Entity object, but it doesn't exist.
Let's check where you use gamemap in actions.py. It seems to be in the ActionMove class:

class ActionMove(BaseAction):
def perform(self):
if not self.engine.game_map.is_blocked(*self.dest_xy):
if self.engine.game_map.get_blocking_entity_at_location(*self.dest_xy):
if self.blocking_entity:
return False

Here, self.engine.game_map is used, and it relies on self.entity.gamemap.engine. If the gamemap attribute is not set for the Entity, this would cause the mentioned error.
Check where you create an ActionMove object and ensure that the associated Entity object has the gamemap attribute set correctly.
Also, ensure that the changes you made in Entity class initialization and placement methods are consistent with the way you create entities and handle their placement.
If the issue persists, provide the specific part of the code where you create an ActionMove object, and I can help you further.

New Threads

Latest posts

Buy us a coffee!

Top Bottom