Python Forum
[pygame] Inventory items not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[pygame] Inventory items not working
#15
(May-27-2019, 07:18 PM)SheeppOSU Wrote: The button was only detecting clicks at certain times though so I separated it to give it a bigger range of detection. It still won't work though.
That is not the proper fix. That means there is an issue, and expanding the collision with your mouse range only worsens the problem.

Quote:if event.type == pygame.MOUSEBUTTONDOWN and clicked[0] == 1 and pygame.Rect((200, 650, 400, 100)).collidepoint(pygame.mouse.get_pos()):
Get rid of the last and condition for the rect. Its too much in one check.

It should be more like this.
btn = Button(rect=(200, 650, 400, 100))
...
if event.type == pygame.MOUSEBUTTONDOWN and clicked[0] == 1:
    if btn.rect.collidepoint(pygame.mouse.get_pos()):
        #code
Here btn is the Button object of your class. btn.rect is the rect you gave it when initializing the button object.

and this
pygame.Rect((200, 650, 400, 100)).collidepoint(pygame.mouse.get_pos())
is not an efficient way to check collision. You are creating an entire new rect when doing this. You want to reuse the button rect from its class.

Quote:
                if PassWord == UserData[name]['password']:
                    Logging = False
                else:            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)
                    pygame.draw.rect(Screen, (0, 0, 255), (0, 350, 800, 50))
                    SD.message_display(Screen, 'Invalid Password', 40, (255, 0, 0), 400, 450)
            else:
                pygame.draw.rect(Screen, (0, 0, 255), (0, 350, 800, 50))
                SD.message_display(Screen, 'Invalid Username', 40, (255, 0, 0), 400, 450)
The pygame.draw.rect lines should be saved. Its a horrible process when you have them littering your entire code. Why are you drawing new rects when your button class already has a draw method? You should have be reusing the your Button class.

Quote:
if self.rect[0] + self.rect[2] > mouse[0] > self.rect[0] and self.rect[1] + self.rect[3] > mouse[1] > self.rect[1]:
Your collision detection for your button is over-complicated. Simplify it by just doing
if self.rect.collidepoint(pygame.mouse.get_pos()):
Quote:
    def optClick(self, gD, ShadowColor, ButColor, command=None, command2=None):
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        if self.rect[0] + self.rect[2] > mouse[0] > self.rect[0] and self.rect[1] + self.rect[3] > mouse[1] > self.rect[1]:
            pygame.draw.rect(gD, ShadowColor, (self.rect))
            gD.blit(self.textSurf, self.textRect)
            if click[0] == 1:
                    if command != None:
                        command()
                    if command2 != None:
                        command2()
            else:
                pygame.draw.rect(gD, ButColor, (self.rect))
                gD.blit(self.textSurf, self.textRect)
In your button class...You should use a toggle variable to switch to and from ShadowColor to ButColor. Then assign that to a general color variable and blit that color. Here is a function that checks for mouse hover over the button. Notice that there is no drawing here. Just below that method is the draw method. In it you can see that color gets blit no matter what. But color here is assigned a different color as hover color or clicked color based on other actions. Here it is sent to draw (another method because its a rounded rect instead of a pure rectangle). Otherwise it was just blit right there as color. Here is the actual drawing of the button. Notice there is no logic here, it just draws color as the logic has been established beforehand.


Quote:
                    if command != None:
You only need if command:. Command is either None or it is not. You never need to check for against None unless you are comparing it to empty or None. So you can just do if command: or if not command: for the opposite. you also need to use if and elif. if and if will check both conditions. you only want it to check one and then the other...not both. Otherwise you could result in bugs.

Quote:
self.rect = (950, self.rect[1], self.rect[2], self.rect[3])
this is more confusing. you should just call self.rect.x or self.rect.y instead of self.rect[0]] etc.

Quote:
            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)

Aside from being redundant, what is the point of checking for if num < 3? As there is nothing else compared.
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, 09:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding an inventory and a combat system to a text based adventure game detkitten 2 6,930 Dec-17-2019, 03:40 AM
Last Post: detkitten
  [pygame] Inventory problems. Weapons equipped to wrong slot SheeppOSU 6 4,076 May-07-2019, 02:46 AM
Last Post: SheeppOSU
  [pygame] Blitting armor and weapons with inventory SheeppOSU 3 2,920 Apr-29-2019, 02:31 AM
Last Post: SheeppOSU
  [pygame] Equiping inventory slots with invisible buttons SheeppOSU 6 4,777 Apr-26-2019, 08:45 PM
Last Post: SheeppOSU
  [pyGame] Key Event not Working !... JamieVanCadsand 7 20,732 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