我正在尝试使用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内的draw函数中调用了该函数。我还有一个名为slotSelected的变量,该变量跟踪播放器将鼠标悬停/悬停在其上的插槽的索引。
我遇到的问题是,每当玩家将鼠标悬停在某个插槽上然后停止将鼠标悬停在任何插槽上时,我设置的插槽索引仍然是玩家刚刚所在的插槽的索引。当我添加else语句以检查是否与插槽没有冲突,并将var slotSelected设置为类似None(例如,以表明玩家没有与任何插槽发生碰撞)时,var slotSelected会一直设置为None ,无论是否有碰撞。我有一个打印语句,用于打印slotSelected的值。您会注意到,它会打印播放器与之碰撞的插槽的索引。但是,当您取消注释slotClass中包含的else语句时,var slotSelected将始终设置为None。
有关如何解决此问题的任何建议?
答案 0 :(得分:1)
也许您也可以尝试测试与背景的另一次碰撞。我不是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
因此,当鼠标位于游戏窗口中但不在项目插槽中时,您可以将其分配为None。
编辑
我弄清楚了为什么它如此回应。你有几行:
for i in slotArray:
i.draw(win)
将转到插槽0,看到已选择->设置selectedSlot = 0
,然后转到插槽1,看到未选择->设置selectedSlot = None
覆盖您先前设定的值。如果selectedSlot不为None,则需要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()
问题是您将绘制项目并选择它们组合为一个功能(非常糟糕)。因此,当循环中断时,其余框均未绘制!