Python Forum
[pygame] Inventory items not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[pygame] Inventory items not working
#12
(May-27-2019, 03:35 AM)SheeppOSU Wrote: I have a time.sleep after some of the buttons to make sure it doesn't get clicked twice. How would you suggest I make sure it isn't clicked twice

Your button class is improperly coded in button.optClick. You need to check collision/clicks in the event loop.

for event in pygame.event.get():
    if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
        function()
I would read this tutorial thoroughly. Your collision detection for the button is also overly complicated than it needs to be and can be reduced.

    def get_event(self, event):
        if event.type == pg.MOUSEBUTTONDOWN and event.button == 1: #if event is a mouse button 1 press
            if self.rect.collidepoint(pg.mouse.get_pos()): # if mouse is inside button 
                self.command()
(May-27-2019, 05:17 AM)SheeppOSU Wrote: I feel as if I have done everything you suggested (except of course the time.sleep problem) and I still have 923 lines of code, of course about 200 of those lines are probably no code and spreading dictionaries over multiple lines. The total lines of code if I compacted everything would be about 700 - 750
I just compacted everything in a copy of the game and found that my guess was close. The total lines in the end were 779.
You need to read this. There should only be a single call, not calls all over the code. Even though its relating to text game, it can still be applied to GUI programming.

Your loading images currently are not what i meant. Somethine along the lines of...

images = {}
for f in os.listdir(IMAGEPATH):
    image = pygame.image.load(f)
    filename = f #strip off .png and path here to only get filename /user/Destination Pics/DiamondSword.png -> DiamondSword
    images.update({filename:image})

...
#use image
blit(images['DiamondSword'], images['DiamondSword'].rect)
This is what i mean by reducing your code. Your loading of images could be reduced from 80 lines to just 5 lines. The same with your other locations. They are not really resolved. you just condensed them a little making them actually harder to read.

Everywhere you have 2 or more copies of a line can be reduced. That is the rule of thumb for programming. That is a tall tale sign of bad coding.

Quote:
    def character(self):
        if not self.dead:
            if self.direct == 'Left':
                self.weapon = gD.blit(self.weapType[3], (self.rect[0] - round(self.width*.8), self.rect[1] + round(self.height/2)))
                self.armor = gD.blit(self.color[3], (self.rect[0], self.rect[1]))
                self.weaponPos = (self.rect[0] - round(self.width*.8), self.rect[1] + round(self.height/2))
            elif self.direct == 'Right':
                self.weapon = gD.blit(self.weapType[1], (self.rect[0] + round(self.width*.8), self.rect[1] + round(self.height/2)))
                self.armor = gD.blit(self.color[1], (self.rect[0], self.rect[1]))
                self.weaponPos = (self.rect[0] + round(self.width*.8), self.rect[1] + round(self.height/2))
            elif self.direct == 'Up':
                self.weapon = gD.blit(self.weapType[0], (self.rect[0] + round(self.width/2), self.rect[1] - round(self.height*.8)))
                self.armor = gD.blit(self.color[0], (self.rect[0], self.rect[1]))
                self.weaponPos = (self.rect[0] + round(self.width/2), self.rect[1] - round(self.height*.8))
            elif self.direct == 'Down':
                self.weapon = gD.blit(self.weapType[2], (self.rect[0] + round(self.width/2), self.rect[1] + round(self.height*.8)))
                self.armor = gD.blit(self.color[2], (self.rect[0], self.rect[1]))
self.weaponPos = (self.rect[0] + round(self.width/2), self.rect[1] + round(self.height
            if plrX > self.rect[0] and plrY > self.rect[1]:
                if num < 3:
                    self.direct = random.choice(DownRight)
                self.rect = (self.rect[0] + 0.2, self.rect[1] + 0.2, self.rect[2], self.rect[3])
            elif plrX > self.rect[0] and plrY < self.rect[1]:
                if num < 3:
                    self.direct = random.choice(UpRight)
                self.rect = (self.rect[0] + 0.2, self.rect[1] - 0.2, self.rect[2], self.rect[3])
            elif plrX < self.rect[0] and plrY < self.rect[1]:
                if num < 3:
                    self.direct = random.choice(UpLeft)
                self.rect = (self.rect[0] - 0.2, self.rect[1] - 0.2, self.rect[2], self.rect[3])
            elif plrX < self.rect[0] and plrY > self.rect[1]:
                if num < 3:
                    self.direct = random.choice(DownLeft)
                self.rect = (self.rect[0] - 0.2, self.rect[1] + 0.2, self.rect[2], self.rect[3])
            elif plrX == self.rect[0] and plrY > self.rect[1]:
                if num < 3:
                    self.direct = 'Down'
                self.rect = (self.rect[0], self.rect[1] + 0.2, self.rect[2], self.rect[3])
            elif plrX == self.rect[0] and plrY < self.rect[1]:
                if num < 3:
                    self.direct = 'Up'
                self.rect = (self.rect[0] + 0.2, self.rect[1] - 0.2, self.rect[2], self.rect[3])
            elif plrY == self.rect[1] and plrX > self.rect[0]:
                if num < 3:
                    self.direct = 'Right'
                self.rect = (self.rect[0] + 0.2, self.rect[1], self.rect[2], self.rect[3])
            elif plrY == self.rect[1] and plrX < self.rect[0]:
                if num < 3:
                    self.direct = 'Left'
                self.rect = (self.rect[0] - 0.2, self.rect[1], self.rect[2], self.rect[3])
            elif plrX == self.rect[0] and plrY == self.rect[1]:
                if num < 3:
self.direct = random.choice(AnyDirect)


as well as unknown magic numbers all over the place.
Quote:
            if k < 6:
                i += 30
            else:
                k = 0
                i = 0
                self.y += 30
            slot = self.slots/6
            if self.y >= self.Y + 30*slot:
self.y = self.Y
as well as obviously still using self.x and self.y instead of pygame rects.

You have logic (SlotBtn creation and click method) in your draw method. As well as weapon changing. What does the weapon changing or buttons have anything to do with drawing? The draw method should only be blitting....no logic. This is why we have so much trouble find things.
    def draw(self, p, equips=None):
        i = 0
        k = 0
        self.opened = True
        SD.message_display(gD, 'Inventory', 50, black, self.x + 90, self.y - 50)
        for n in range(1, self.slots + 1):
            k += 1
            if n in self.availableSlots:
                gD.blit(self.thumbnails[n], (self.x + i + 1, self.y + 1))
                    
            if n in self.equips:
                SlotBtn = IBT((self.x + i, self.y, 20, 20))
                SlotBtn.optClick(command=partial(self.equipSub, n))
                pygame.draw.rect(gD, red, (self.x + i, self.y, 20, 20), 1)
                if n in self.availableSlots:
                    for weapon in weaponTuple:
                        if self.thumbnails[n] in weapon:
                            self.item['weapon'] = weapon
                            p.weapChange(weapon)
                            strItemChange(weapon)
                    for armor in armorTuple:
                        if self.thumbnails[n] in armor:
                            self.item['armor'] = armor
                            p.armorChange(armor)
                            strItemChange(armor)
            else:
                SlotBtn = IBT((self.x + i, self.y, 20, 20))
                SlotBtn.optClick(command=partial(self.equipChange, False, self.equips + (n,), n))
pygame.draw.rect(gD, black, (self.x + i, self.y, 20, 20), 1)
Recommended Tutorials:
Reply


Messages In This Thread
[pygame] Inventory items not working - by SheeppOSU - May-12-2019, 12:36 AM
RE: [pygame] Inventory items not working - by metulburr - May-27-2019, 11:04 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding an inventory and a combat system to a text based adventure game detkitten 2 7,017 Dec-17-2019, 03:40 AM
Last Post: detkitten
  [pygame] Inventory problems. Weapons equipped to wrong slot SheeppOSU 6 4,140 May-07-2019, 02:46 AM
Last Post: SheeppOSU
  [pygame] Blitting armor and weapons with inventory SheeppOSU 3 2,965 Apr-29-2019, 02:31 AM
Last Post: SheeppOSU
  [pygame] Equiping inventory slots with invisible buttons SheeppOSU 6 4,850 Apr-26-2019, 08:45 PM
Last Post: SheeppOSU
  [pyGame] Key Event not Working !... JamieVanCadsand 7 20,910 Sep-29-2017, 08:59 PM
Last Post: Mekire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020