简体   繁体   中英

Having trouble With figuring out how to come up with a inventory system in my python game

Im making a little simple survival game in python and pygame and im having trouble coming up with a inventory system. i have been trying to figure it out without any help to challenge myself but its time to ask for help. i have been playing around with this code

Item class

class item():
   def __init__(self,name,amount):
      self.name = name
      self.amount = amount

Resource class

class wood(pygame.sprite.Sprite):
   def __init__(self):
    self.x = 700
    self.y = 100
    self.cut = False
    self.frame = 0
   def draw(self,display):
    self.rect = pygame.Rect((self.x + 38,self.y + 10,20,80))
    display.blit(Art_lists.trees_list[self.frame], (self.x,self.y))
    if pygame.sprite.collide_rect(self,Global.lion):
        if (pygame.key.get_pressed() [pygame.K_SPACE] != 0):
            if self.cut == False:
                if Global.wood not in Global.inventory:
                    Global.inventory.append(Global.wood)
                    Global.oinventory.append('wood')
                Global.wood.amount += 1
                self.frame = 1
                self.cut = True
            if self.cut == True:
                Global.wood.amount += 0

so when you collide with the node for example a tree when you press space you cut the tree and it adds a object of the items class to a list called inventory and then adds a string to the list oinventory and then oinventory then gets printed on to screen but i cant get a int to be blited with a string bcause it wont accept it. And i really dont think that this is the best way to make an inventory. I want the inventory to just display its items in text on screen with the amount next to it. Like if you have ever played fallout. Sorry if my code looks bad or if my question is not understandable im a novice programmer.

I would set the inventory up as an array. Each value of the array would be an object variable which would change depending on what the user has in their inventory.

You need to break the problem down into multiple parts. One part defines how the world behaves (trees can be chopped, resulting in x wood), another part defines what the player is holding at any particular time while the last part takes that information and renders the inventory to the screen.

That way, you can do other things like: Have NPCs that can chop down wood / have their own inventories / etc. without having duplicate definitions scattered all over the place. Added to which, you'll reduce the amount of data that needs to be saved per-game as the definition of wood doesn't change, whereas how much wood a player is carrying does.

So your GlobalItems would be a dictionary that contains something like...

{Global.wood: {"Name": "Lumber",
               "Icon": Art_lists.xxx,
               "MaxStack": 20,
               "Portable": True}
 Global.tree: {"Name": "Ash tree"
               "Chop": {Global.wood: 50},
               "Portable": False}
}

And your inventory becomes a list/array:

[{"Type": Global.wood, "Count": 10},
 {"Type": Global.leather, "Count": 5}]

If you want to handle stacks / preserve item location, you can have multiple inventory entries:

[{"Type": Global.wood, "Count": 20},
 None, # Blank space
 {"Type": Global.wood, "Count": 15}
]

Then your code to draw the inventory can loop through the inventory list, calculating current tile position for each item, then lookup the Type in GobalItems to determine icon and other useful information.

The separation of concerns has multiple benefits:

  • It allows different people to work on different parts of the system
  • Makes the whole thing far less fragile when changes are made - you're either changing what an item is, how many someone has or how it's drawn.
  • Makes it explicit what information needs to be written to disk for saves
  • Makes updating much cleaner (What happens if you decide wood should stack in 25 not 20? Now you've only got to change it in one place)

Note that there's probably an argument for separating things that can be picked up from things in the world, but this depends on how your game is intended to work. Using the above method, you'd create an object in the world and tag it as Global.tree . For bug-avoidance, I've added the "Portable" flag to ensure you never end up with a tree in your inventory because you mis-tagged something, you'll get an error instead.

If the player can't drop arbitrary items into the world and pick them up later, There's no reason for portable/non-portable to be mixed up in the same list. You might be better off with a list of things that can end up in player inventory, and a list of things that can appear in the game world.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM