#๐Ÿ”’ How to use this specific room from another file

61 messages ยท Page 1 of 1 (latest)

mild vector
#

Sorry for how terrible the title is... I'm a complete Python beginner, I don't know what any of this is called.

I have 2 files, main.py and stuffs.py. I removed parts of my code unnecessary to the question.

My code:

# stuffs.py
from main import main
class Location:

    def __init__(self, name, des):
        self.name = name
        self.des = des
        self.connection = {}
        self.items = []
        self.mats = []

    def connect(self, nesw, loc):
        self.connection[nesw] = loc
        reverse = {
            "south": "north",
            "east": "west",
            "in": "out",
            "down": "up"
        }[nesw]
        loc.connection[reverse] = self

class Player:
    def __init__(self, current_loc, hp, stamina, warmth):
        self.current_loc = current_loc
        self.hp = hp
        self.sta = stamina
        self.warmth = warmth
        self.inventory = Inventory()

class Stuff:

    def __init__(self, time, day, mvp):
        self.time = time
        self.day = day
        self.mvp = mvp
        self.player = Player(main.r1, 100, 100, 100) # The part I need help with!!


# main.py
def main():
     r1 = Location("Your bedroom", "[]")

Obviously main.r1 wouldn't work but I'm not sure what to do... (Also, I'm aware my code is probably very inefficient, but for now it just needs to work, haha.) Thank you!

icy shardBOT
#

@mild vector

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

spring onyx
#
# main.py
from stuffs import Location, Player, Stuff

def main():
  r1 = Location("Your bedroom", "[]")
  ...
mild vector
# spring onyx I think you should be importing stuffs into main.py

Thanks you! Yep, I've done that, but I also need to access things from Player() in Stuff(), for quests.

# stuffs.py
    def quest_complete(self):
        if self.day == 1:
            if self.player.has_item("radio") and self.player.has_item(
                    "flashlight"
            ) and self.player.inventory.mats["battery"] > 0:
                return True
            else:
                return False
# There's more below this.
mild vector
spring onyx
#

Just instantiate the starting location in place of main.r1

#

self.player = Player(Location("Your bedroom", '[]'), 100, 100, 100)

mild vector
#

ohhh, that makes sense. thank you so much!!!

mild vector
spring onyx
#

is that what you want?

#

Maybe you need to redesign your classes so it makes much more sense

#

What is class Stuff supposed to do ? @mild vector

mild vector
#

I'm actually not entirely sure what classes and files do... Part of this was generated by AI and I added on to it. class Stuff is supposed to count days (in game), time, movement points, quests and other stuff that are necessary but doesn't fit in to the other classes.

spring onyx
spring onyx
#

OK so I'm trying to understand more on what you're doing

#

What does the r1.connect method supposed to do?

mild vector
#

Uh, it connects the locations (using string) so the player can "move" through it. Am I allowed to send links (this was done through Replit)?

mild vector
spring onyx
#

so if there's 2 locations:

  1. Forest_Location
  2. Dungeon_Location

You want them to connect with each other on each other's "direction", I assume?

mild vector
#

yes. the "direction" is just "north" or "south", etc.

spring onyx
#

so far, your code only uses south, east, in and down for the direction

mild vector
#

I made a reverse thing in stuffs.py:

    def connect(self, nesw, loc):
        self.connection[nesw] = loc
        reverse = {
            "south": "north",
            "east": "west",
            "in": "out",
            "down": "up"
        }[nesw]
        loc.connection[reverse] = self
spring onyx
#

yeah I see it

spring onyx
#

So instead of "connection", define "directions" and the directions' connections

mild vector
#

I'm not really sure what you mean... Um, besides being a little hard to understand, connect works fine though? I kind of need to submit this project soon...

I just need help with setting the player's starting location in line 107 of stuffs.py....

spring onyx
#

stuffs.py

class Location:

    def __init__(self, name, des):
        self.name = name
        self.des = des
        self.connection = {}
        self.items = []
        self.mats = []

    def connect_to(self, location, direction):
        self.connection[direction] = location

    def __str__(self):
        return self.name
    def __repr__(self):
        return self.name

main.py

from stuffs import Location, Player
def main():
     forest = Location("Forest", "[]")
     dungeon = Location("Dungeon", "[]")
     forest.connect_to(dungeon, "east")
     p1 = Player(forest, 100, 100, 100)
     print(f"{p1.current_loc=}")
     print(f"{p1.current_loc.connection=}")

if __name__ == '__main__':
    main()
mild vector
#

How can I set p1 = Player(forest, 100, 100, 100) in stuffs.py though? Since forest will be defined in main.py

spring onyx
# mild vector How can I set `p1 = Player(forest, 100, 100, 100)` in `stuffs.py` though? Since ...
from game import Location, Player, GameEnvironment
def main():
     forest = Location("Forest", "[]")
     dungeon = Location("Dungeon", "[]")
     forest.connect_to(dungeon, "east")
     p1 = Player(forest, 100, 100, 100)
     game_time = 0.0
     game_day = 0.0
     game_mvp = 0.0
     game_env = GameEnvironment(game_time, game_day, game_mvp, p1)
     print(game_env.player.current_loc)
     print(f"{p1.current_loc=}")
     print(f"{p1.current_loc.connection=}")
     


if __name__ == '__main__':
    main()

I changed your Stuffs class to GameEnvironment to make it make sense.

#

Here's the new class. You just have to keep it simple

class GameEnvironment:

    def __init__(self, time, day, mvp, player):
        self.time = time
        self.day = day
        self.mvp = mvp
        self.player = player

#

Don't overcomplicate it

#

Does this make sense? @mild vector

mild vector
#

if self.player = player, then what about class Player?

#

Thank you so much by the way, I'm just having a hard time understanding.....

spring onyx
#

And to call this class GameEnvironment and instantiate it with initial values, you add the values as you instantiate

#

like GameEnvironment(time=0.0, day=0.0, mvp=0.0, player=p1)

#

And if you backtrack some more

#

p1 is defined with Player(forest, 100, 100, 100)

#

And that goes to... class Player

#

The idea behind this implementation is to separate the soft values from behaviors in OOP

#

values can change anytime (like in your earlier example, Bedroom. I've used Forest and Dungeon)

#

But that shouldn't change the behavior of the class object in your stuffs.py

mild vector
#

Oh, that makes sense (at least your code does, not sure about soft values and behaviors and OOP)!!!! Thank you!

spring onyx
#

Basically what I'm saying is don't hardcode "Your bedroom" in stuffs.py

#

What if you want to make a new game project? Does it always start in "Your Bedroom"?

#

Make the classes re-usable

icy shardBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.