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
#2
What does "it doesnt draw very well" mean? Most of the time laggy operations in pygame result to bottlenecks in your own code. The following is probably not enough to create those issues, but indeed shows that there are bottlenecks in your code. This is not very resource intensive, but things like this sporadically throughout your code can make it laggy.

Quote:
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)
The draw methods should only do that....draw things. Not handle logic. For every object that is being drawn you are wasting resources by obtaining the mouse position for every object. This is not needed. You only need to obtain the mouse position once per frame, not per object. The mouse position will not change in the same frame, so you can pass the same mouse position down to all your objects.

The following is however resource intensive....
Your screen fill is on every draw method of that object. I can see why you need it, however it is drawing the entire background every frame and every object (regardless of whether its being moved or not). So if you have a 1000 objects on screen, you are separately drawing the background 1000 times per frame, when you only need it when drag and dropping. I would determine if an item is being dragged and only while that is occurring, fill the screen in. Also there are methods for reducing CPU usage including dirty rects and pygame.display.update to only update portions of the screen.

It would be much helpful to view your full code.

It would also be helpful to use pygame rects, instead of handling positioning of drawing. There are magic numbers everywhere.
Recommended Tutorials:
Reply


Forum Jump:

User Panel Messages

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