擅长:python、mysql、java
<p>我很好奇,你在做一个堆栈,但在这个游戏中没有使用堆栈操作。我在前一篇文章中向您展示了堆栈操作。你知道吗</p>
<p>我给了你发票pop()命令,只需使用发票推送(whateverisbeingpush)</p>
<p>这将大大减少您的代码。你知道吗</p>
<pre><code>if event.key == pygame.K_x:
if len(inv) < 4: #inventory isn't full, 4 is max size of inventory.
inv.push("an_item")
else: #inventory == 4, therefore no space.
print("No inventory space")
if event.key == pygame.K_z:
if len(inv) == 0: #length of inventory is 0, meaning no items to drop.
print("Nothing else to drop, inventory is already empty.")
else:
inv.pop() #removes the most recently added item (which is what your code is doing currently)
</code></pre>