Python Forum
[pyagame] Run code more smoothly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[pyagame] Run code more smoothly
#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


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