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



我正在尝试使用模块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内的绘制功能中调用该功能。我也有一个称为插槽的变量,该变量跟踪播放器正在使用/悬停的插槽的索引。

我遇到的问题是,每当玩家徘徊在插槽上,然后停止徘徊在任何插槽上时,我设置的插槽索引仍然是玩家刚刚打开的插槽的索引。当我添加其他语句以检查是否与插槽没有碰撞时,并将var插槽设置为例如note的东西(以表明播放器没有与任何插槽相撞),var slotected始终设置为,无论是否发生碰撞。我有一个打印语句,可以打印插槽的值。您会注意到它打印了玩家碰撞的插槽的索引。但是,当您输入我在slotclass中包含的其他语句时,var slotected将始终设置为无。

关于如何解决此问题的任何建议?

也许您也可以尝试测试与背景的另一碰撞。我不是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不是没有,则需要break您的循环!这是解决此问题的代码:

#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()

问题是,您已经将绘制项目绘制并将其选择为一个功能(非常糟糕)组合在一起。因此,现在,当循环被打破时,其余的盒子没有绘制!

最新更新