Python Forum
[pyagame] Run code more smoothly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[pyagame] Run code more smoothly
#1
I made an inventory class and was going to test it, but it doesn't draw very well. Because items are dragged around, the background has to be updated as well as the inventory boxes being drawn and doing some checks. It just doesn't work. I need a way to run my code more smoothly. Thx for any suggestions.

Draw function for the inventory; this one is most open for improvement -
    def draw(self):
        for slotY in range(1, 4):
            for slotX in range(1, 4):
                slotNum = slotX + ((slotY - 1) * 3)
                item = self.items[slotNum] #Dictionary to keep track of slots and what they are holding
                x = 50 + (slotX * 55)
                y = 300 + (slotY * 55)
                if item != None and self.carrying == None: #self.carrying is the item being dragged
                    item.draw()
                    function = item.PickUp #Will change the item's slot value to None
                elif self.carrying != None:
                    if item != None:
                        item.draw()
                        function = partial(self.SwapItems, item, self.carrying) #Will switch around the slot values to switch the items
                    else:
                        function = partial(self.PutDown, self.carrying, slotNum) #Change the item's slot number to the slot clicked on
                slotBtn = BT([x, y, 52, 52], '', 16, [55, 55, 55])
                slotBtn.start(Screen, [85, 85, 85], [115, 115, 115], [function]) #Runs the button which includes drawing and checking for clicks
Draw function and CalculatePos function for item class -
    def draw(self):
        if self.slot == None: #item's slot number
            mouse = pygame.mouse.get_pos()
            pos = [mouse[0] - 25, mouse[1] - 25] #Center the thumbnail on the mouse
        else:
            pos = self.CalculatePos() #Get positions from the slot number
        Screen.blit(PD[self.weapon + 'Thumbnail'], pos)

    def CalculatePos(self):
        slot = self.slot
        y = 1
        count = 0
        while slot - 3 > count:
            count += 3
            y += 1
        x = slot - ((y - 1) * 3)
        return 50 + (x * 55), 300 + (y * 55)
The stage class's draw function looks like this, but I don't think it can be improved
    def draw(self):
        Screen.fill(self.background)
        for obst in self.obst: #There are at least 32 obstacles on each stage
            obst.draw()
Reply


Messages In This Thread
[pyagame] Run code more smoothly - by SheeppOSU - Aug-15-2019, 03:37 AM
RE: [pyagame] Run code more smoothly - by metulburr - Aug-15-2019, 10:09 AM

Forum Jump:

User Panel Messages

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