gpt4 book ai didi

Python:创建基于槽位的库存系统

转载 作者:行者123 更新时间:2023-12-01 08:12:34 26 4
gpt4 key购买 nike

我正在尝试使用 pygame 模块在 Python 中创建一个库存系统。库存系统类似于 Minecraft。它是一个基于槽位的库存系统,这意味着每个项目都附加到一个槽位,并且可以在库存中移动。

这是代码:

#Module that allows me to create a screen and add images to the screen
import pygame
pygame.init()
win = pygame.display.set_mode((800,600))

#Variable that will keep track of the index of what slot the player is
#selecting
selectedSlot = None

#Function that checks whether there is collision or not
def collision(x,y,x2,y2,w):
if x + w > x2 > x and y+w > y2 > y:
return True
else:
return False

#Slot Class
class slotClass:
def __init__(self,x,y):
self.x = x
self.y = y

def draw(self,win):
#Draws the slots using the x and y values
pygame.draw.rect(win,(255,0,0),(self.x,self.y,50,50))

#Uses a function to test for collision with the mouse's x and y values
if collision(self.x,self.y,mx,my,66):
global selectedSlot
pygame.draw.rect(win,(128,0,0),(self.x,self.y,50,50))

#This will set an integer value to a varaible, dependent on what the index of the element the player selecting is
selectedSlot = slotArray.index(self)

#Problem with code:
#When the following 2 lines are uncommmented, the variable selectedSlot is set to "None", regardless of whether there is collision or not
#else:
#selectedSlot = None

#Slot array
slotArray = []
#Slot information
slotCount = 9


#Populates the slotArray with the desired slotCount
while len(slotArray) != slotCount:
slotArray.append(slotClass(100+len(slotArray)*70,50))

#main loop
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

print(selectedSlot)

#Mouse x and y value to set to the vars mx and my
mx,my = pygame.mouse.get_pos()
win.fill((0,0,0))


#For every element in the slotArray, the function draw is called from the slotClass
for i in slotArray:
i.draw(win)

pygame.display.update()
pygame.quit()

它的工作原理是我有一个类,它将保存每个插槽的信息。例如 x 值、y 值以及每个槽中有什么项目。然后我有一个数组,它将包含每个插槽实例。我还定义了我完全想要多少个插槽。

为了用槽填充这个数组,我首先不断地追加到这个槽数组中,直到它等于每行中所需的槽数量。我将 55 乘以数组中的元素数量来分散槽。

当我尝试创建玩家将鼠标悬停/选择每个插槽的功能时,我遇到的问题就出现了。我想要的是玩家只需将鼠标悬停在某个插槽上,该插槽就会变成不同的颜色,然后玩家可以从该插槽中选择一个项目。

我为此创建了一个碰撞函数,并在 slotClass 内的绘图函数中调用该函数。我还有一个名为 slotSelected 的变量,它跟踪玩家将鼠标悬停在其上的插槽的索引。

我遇到的问题是,每当玩家将鼠标悬停在某个插槽上,然后停止悬停在任何插槽上时,我设置的插槽索引仍然是玩家刚刚所在插槽的索引。当我添加 else 语句来检查是否与插槽不发生冲突,并将 var slotSelected 设置为类似 None 的值(以表明玩家没有与任何插槽发生冲突)时,var slotSelected 始终设置为 None ,无论是否发生碰撞。我有一个打印语句,用于打印 slotSelected 的值。您会注意到它打印了玩家正在碰撞的插槽的索引。但是,当您取消注释我在 slotClass 中包含的 else 语句时,var slotSelected 将始终设置为 None。

关于如何解决这个问题有什么建议吗?

最佳答案

也许您也可以尝试测试与背景的另一次碰撞。我不是 pygame 方面的专家,但这是我将使用的伪代码:

if collision("with game window"): # If mouse is within your game screen
global selectedSlot
if collision(self.x, self.y, mx, my, 66): # mouse is on screen AND on a box
selectedSlot = slotArray.index(self)
else: # mouse on screen, but not on box -> return None
selectedSlot = None

这样当鼠标位于游戏窗口中但不在元素槽上时,您可以分配“无”。

编辑

我明白了为什么它会这样响应。你有以下几行:

for i in slotArray:
i.draw(win)

将转到插槽 0,看到它已被选中 -> 设置 selectedSlot = 0,然后转到插槽 1,看到它未被选中 -> 设置 selectedSlot = None > 覆盖您之前设置的值。如果 selectedSlot 不是 None,您将需要中断循环!这是解决该问题的代码:

#Module that allows me to create a screen and add images to the screen
import pygame
pygame.init()
win = pygame.display.set_mode((800,600))

#Variable that will keep track of the index of what slot the player is
#selecting
selectedSlot = None

#Function that checks whether there is collision or not
def collision(x,y,x2,y2,w):
if x + w > x2 > x and y+w > y2 > y:
return True
else:
return False

#Slot Class
class slotClass:
def __init__(self,x,y):
self.x = x
self.y = y

def draw(self, win):
#Draws the slots using the x and y values
pygame.draw.rect(win, (255, 0, 0), (self.x, self.y, 50, 50))

#Uses a function to test for collision with the mouse's x and y values
if collision(self.x, self.y, mx, my, 66):
global selectedSlot
pygame.draw.rect(win, (128, 0, 0), (self.x, self.y, 50, 50))

#This will set an integer value to a varaible, dependent on what the index of the element the player selecting is
selectedSlot = slotArray.index(self)

#Problem with code:
#When the following 2 lines are uncommmented, the variable selectedSlot is set to "None", regardless of whether there is collision or not
else:
selectedSlot = None

#Slot array
slotArray = []
#Slot information
slotCount = 9


#Populates the slotArray with the desired slotCount
while len(slotArray) != slotCount:
slotArray.append(slotClass(100+len(slotArray)*70,50))

#main loop
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

#Mouse x and y value to set to the vars mx and my
mx,my = pygame.mouse.get_pos()
win.fill((0,0,0))


#For every element in the slotArray, the function draw is called from the slotClass
selectedSlot = None
for i in slotArray:
i.draw(win)
if selectedSlot is not None:
break

print(selectedSlot)

pygame.display.update()
pygame.quit()

问题是您将绘制项目和选择它们合并到一个函数中(非常糟糕)。所以现在当循环被破坏时,其余的盒子就不会被绘制!!

关于Python:创建基于槽位的库存系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55149541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com