1 year ago
#387689
Mushroom
If statement isn't working in pygame, and my inventory isn't working because of it
I'm currently working on a game in pygame, and the first thing that I'm making is an inventory. I have a class for items, and a class for the slots that the items can go in:
class Item(pygame.sprite.Sprite):
def __init__(self,img,pos,item,itemgroup,slotgroup,maingroup):
super().__init__(itemgroup, maingroup)
self.image = img
self.rect = self.image.get_rect()
self.rect.center = pos
self.clicked = False
self.slotgroup = slotgroup
self.item = item
self.itemgroup = itemgroup
self.Type = 'item'
self.slot = 0
def update(self):
self.getdrag()
def getdrag(self):
pos = pygame.mouse.get_pos()
if self.rect.collidepoint(pos):
if pygame.mouse.get_pressed()[0] == 1:
go_ahead = True
for item in self.itemgroup:
if item.clicked == True:
go_ahead = False
if go_ahead:
self.clicked=True
if pygame.mouse.get_pressed()[0] == 0 and self.clicked == True:
self.clicked = False
self.drag(self.clicked)
if self.clicked == True:
self.drag(self.clicked)
def drag(self,still_clicked):
if still_clicked:
self.rect.center = pygame.mouse.get_pos()
if not still_clicked:
self.spritex = []
self.spritey = []
self.spritexx = []
self.spriteyy = []
self.spritexxx = []
self.spriteyyy = []
for sprite in self.slotgroup:
if sprite.item == self.item:
if sprite.used == False:
self.spritex.append(sprite.rect.centerx)
self.spritey.append(sprite.rect.centery)
if sprite.item == 'empty':
if sprite.used == False:
self.spritex.append(sprite.rect.centerx)
self.spritey.append(sprite.rect.centery)
for item in self.spritex:
self.spritexx.append(self.rect.centerx - item)
self.spritexxx.append(abs(self.rect.centerx - item))
for item in self.spritey:
self.spriteyy.append(self.rect.centery - item)
self.spriteyyy.append(abs(self.rect.centery - item))
self.rect.centerx -= self.spritexx[self.spritexxx.index(min(self.spritexxx))]
self.rect.centery -= self.spriteyy[self.spriteyyy.index(min(self.spriteyyy))]
for sprite in self.slotgroup:
if pygame.Rect.colliderect(self.rect,sprite.rect):
sprite.used = True
sprite.anyused = True
self.slot = sprite
class Slots(pygame.sprite.Sprite):
def __init__(self,img,pos,item,slotgroup,itemgroup,maingroup,number):
super().__init__(slotgroup,maingroup)
self.image = img
self.used = False
self.anyused = False
self.rect = self.image.get_rect()
self.rect.center = pos
self.item = item
self.Type = 'slot'
self.itemgroup = itemgroup
self.number = number
What should happen is 1,it waits until you click an item, drag it, and let got, 2, it finds the location of each of the slots that the item could go in, 3, it finds the location of the closest one, 4, it moves the item to that location.
The goal is to limit 1 item to each slot, but instead you can stack items on the same slot, which breaks the inventory.
The issue is that it ignores this if statement,
for sprite in self.slotgroup:
if sprite.item == self.item:
if sprite.used == False:
self.spritex.append(sprite.rect.centerx)
self.spritey.append(sprite.rect.centery)
if sprite.item == 'empty':
>>>>if sprite.used == False:
self.spritex.append(sprite.rect.centerx)
self.spritey.append(sprite.rect.centery)
In the main file, I have something that outlines each item slot that has used == True, but I can still put items in those outlined slots.
I've tried using different variables for if the sprite.item is empty or the same as the item, but it doesn't work. I have tried putting if sprite.item == self.item and if sprite.item == 'empty' together, but that didn't help either.
Any help with this would be greatly appreciated.
python
pygame
0 Answers
Your Answer