简体   繁体   English

在弄清楚如何在我的python游戏中提出库存系统时遇到麻烦

[英]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. 我用python和pygame制作了一个简单的生存游戏,而我想出了一个库存系统。 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. 因此,当您在按下空格的时候与树等节点发生碰撞时,您会砍下这棵树,它将项目类的对象添加到名为清单的列表中,然后将一个字符串添加到清单库存中,然后将库存打印到屏幕,但我不能得到一个int将被字符串bblted,因为它不会接受它。 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. 一部分定义了世界的行为方式(可以砍伐树木,产生x根木头),另一部分定义了玩家在任何特定时间所持有的东西,而最后一部分则获取了该信息并将库存呈现到屏幕上。

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. 这样,您可以执行其他操作,例如:使NPC可以砍柴/拥有自己的清单/等等,而不会在各处散布重复的定义。 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... 因此,您的GlobalItems将是一本包含如下内容的字典:

{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. 然后,用于绘制库存的代码可以遍历库存列表,计算每个物料的当前图块位置,然后在GobalItems查找Type以确定图标和其他有用信息。

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) 使更新变得更加整洁(如果您决定将木材堆放在25个而不是20个中会发生什么?现在只需要在一个地方进行更换即可)

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 . 使用上述方法,您将在世界上创建一个对象并将其标记为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. 为了避免错误,我添加了“ Portable”标志,以确保您永远不会在清单中遇到一棵树,因为您错误地标记了某些东西,而会得到一个错误。

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. 列出可能会出现在玩家清单中的物品和可能出现在游戏世界中的物品,可能会更好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我无法弄清楚如何自动进行 OAuth 身份验证以访问 Google Drive。 (Python) - I'm having trouble figuring out how to automate OAuth authentication to access Google Drive. (Python) 我在弄清楚以下代码的工作方式时遇到了麻烦? - I'm having trouble with figuring out how the following code works? 设置库存时遇到问题 - Having trouble setting up inventory 无法弄清楚此代码的作用 - Having trouble figuring out what this code does 我想在 Python V 3.4.3 中为我的游戏制作一个库存系统 - I want to make an inventory system for my game in Python V 3.4.3 如果/其他循环系统该如何解决? [基于Python GUI的冒险游戏] - How would I fix my inventory if/else loop system? [Python GUI Based Adventure Game] python 新手,无法弄清楚为什么扣除额不包括税收 - New to python and having trouble figuring out why the deductions are not including the tax in the total 在弄清楚如何在字符串报告中一起循环和使用isalpha()和isspace()时遇到麻烦 - Having trouble with figuring out how to loop and use isalpha() and isspace() together in a String Report 无法弄清楚如何使用 argsort 在一个数组上创建索引并使用它对另一个数组进行排序(没有展平) - Having trouble figuring out how to use argsort to create an index on one array and use it to sort another (without flattening) 我在弄清楚这个压缩代码时遇到麻烦 - I'm having trouble figuring out this condensed code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM