(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 - 750You 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.
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.
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.heightif 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:as well as obviously still using self.x and self.y instead of pygame rects.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
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: