CPU loud during python pygame run

I am just going to show the while loop for the pygame run process, it’s a big file.
Wondering if anyone can spot why my CPU is running loud, (sounds like fan kicking in) whenever I run it?
It has a delay to when the fan starts, and when it stops. I tried adding a sleep to maybe pause whatever is running so hard, but to no avail. Can anyone give me some insight?

def run_draw(self):
        red = 0
        green = 0
        blue = 0
        on = False
        off = False
        new = False
        color_pick = False
        color = (red, green, blue)
        screen.fill(globals.background_color)
        picker_window = False
        new_window = None
        while self.running:
            #sleep(1) <---- no circles drawn to screen
            #sleep(0.1)  <---- slight lag, circles being draw, CPU still Whirring during run
            animation_timer.tick(60)
            screen.fill(globals.background_color)
            self.center_surface.draw_surface()
            pg.draw.rect(screen, globals.menu_bar_color, (0, 0, globals.WIDTH, globals.HEIGHT//20), 0)
            pg.draw.rect(screen, globals.menu_bar_color, (0, globals.HEIGHT - globals.HEIGHT//20, globals.WIDTH, globals.HEIGHT//20), 0)
            for rects in self.rect_list:
                rects.place_rect()
            for circs in self.drawn_circs:
                circs.add_circle()
            for window in self.windows:
                window.run(red, green, blue)
            mousedown = False
            events = pg.event.get()
            for event in events:
                if event.type == pg.QUIT:
                    self.running = False
                    pg.quit()
                    exit(0)
                if event.type == pg.MOUSEBUTTONDOWN:
                    position = pg.mouse.get_pos()
                    # cbd = Current Button Dimensions
                    # list returned = [x position, y postion, x postion + width, y position + height]
                    for rects in self.rect_list:
                        cbd = rects.get_rect_place()
                        if cbd[0] < position[0] < cbd[2]:
                            if cbd[1] < position[1] < cbd[3]:
                                #print("IN BUTTON", rects.name)
                                if rects.name == "ON":
                                    on = True
                                    off = False
                                if rects.name == "OFF":
                                    off = True
                                    on = False
                                    color_pick = False
                                    if len(self.windows) > 0:
                                        self.windows.pop()
                                if rects.name == "NEW":
                                    new = True
                                    color_pick = False
                                if rects.name == 'COLOR':
                                    on = True
                                    new = False
                                    color_pick = True
                                    off = False
                                    new_window = ColorPicker()
                                    self.windows.append(new_window)
                            else:
                                pass

            for event in events:
                if event.type == pg.MOUSEBUTTONDOWN and not off and on and new and not color_pick:

                    radius = 20
                    (x, y) = pg.mouse.get_pos()
                    # dsp = Drawn Screen Parameters
                    limit_x_left = self.dsp[0] - radius
                    limit_x_right = self.dsp[2] - radius
                    limit_y_top = self.dsp[1] - radius
                    limit_y_bottom = self.dsp[3] - radius

                    if limit_x_left < x < limit_x_right:
                        if limit_y_top < y < limit_y_bottom:

                            color = (red, green, blue)

                            circle = DrawCircle(screen, x, y, radius, color)
                            self.drawn_circs.append(circle)

                    for rects in self.rect_list:
                        cbd = rects.get_rect_place()
                        if cbd[0] < position[0] < cbd[2]:
                            if cbd[1] < position[1] < cbd[3]:
                                #print("IN BUTTON", rects.name)
                                if rects.name == "ON":
                                    on = True
                                    off = False
                                if rects.name == "OFF":
                                    off = True
                                    on = False
                                    color_pick = False
                                    if len(self.windows) > 0:
                                        self.windows.pop()
                                if rects.name == "NEW":
                                    new = True
                                    color_pick = False
                                if rects.name == 'COLOR':
                                    on = True
                                    new = False
                                    color_pick = True
                                    off = False
                                    new_window = ColorPicker()
                                    self.windows.append(new_window)
                            else:
                                pass

                if event.type == pg.MOUSEBUTTONDOWN and on and color_pick and not off and not new:

                    (gx, gy, gdx, gdy) = new_window.get_button_place("plus green")
                    (g2x, g2y, g2dx, g2dy) = new_window.get_button_place("minus green")
                    (bx, by, bdx, bdy) = new_window.get_button_place("plus blue")
                    (b2x, b2y, b2dx, b2dy) = new_window.get_button_place("minus blue")
                    (rx, ry, rdx, rdy) = new_window.get_button_place("plus red")
                    (r2x, r2y, r2dx, r2dy) = new_window.get_button_place("minus red")
                    (shx, shy, shdx, shdy) = new_window.get_button_place("plus shade")
                    (sh2x, sh2y, sh2dx, sh2dy) = new_window.get_button_place("minus shade")
                    position = pg.mouse.get_pos()
                    if gx < position[0] < gdx and gy < position[1] < gdy:
                        if 0 <= green <= 250:
                            green += 5
                    elif g2x < position[0] < g2dx and g2y < position[1] < g2dy:
                        if 255 >= green >= 5:
                            green -= 5
                    elif bx < position[0] < bdx and by < position[1] < bdy:
                        if 0 <= blue <= 250:
                            blue += 5
                    elif b2x < position[0] < b2dx and b2y < position[1] < b2dy:
                        if 255 >= blue >= 5:
                            blue -= 5
                    elif rx < position[0] < rdx and ry < position[1] < rdy:
                        if 0 <= red <= 250:
                            red += 5
                    elif r2x < position[0] < r2dx and r2y < position[1] < r2dy:
                        if 255 >= red >= 5:
                            red -= 5
                    elif shx < position[0] < shdx and shy < position[1] < shdy:
                        # I tried increments of 10, shade difference was much more dramatic
                        if 0 <= red <= 250:
                            red += 5
                        if 0 <= green <= 250:
                            green += 5
                        if 0 <= blue <= 250:
                            blue += 5
                    elif sh2x < position[0] < sh2dx and sh2y < position[1] < sh2dy:
                        if 255 >= red >= 5:
                            red -= 5
                        if 255 >= green >= 5:
                            green -= 5
                        if 255 >= blue >= 5:
                            blue -= 5

                    else:
                        print("-----color picker event else clause----")
                        for rects in self.rect_list:
                            cbd = rects.get_rect_place()
                            if cbd[0] < position[0] < cbd[2]:
                                if cbd[1] < position[1] < cbd[3]:
                                    #print("IN BUTTON", rects.name)
                                    if rects.name == "ON":
                                        on = True
                                        off = False
                                    if rects.name == "OFF":
                                        # I have to double click to get color picker off... need fixed
                                        off = True
                                        on = False
                                        color_pick = False
                                        if len(self.windows) > 0:
                                            self.windows.pop()
                                    if rects.name == "NEW":
                                        new = True
                                        color_pick = False
                                    if rects.name == 'COLOR':
                                        on = True
                                        new = False
                                        color_pick = True
                                        off = False
                                        new_window = ColorPicker()
                                        self.windows.append(new_window)
                                else:
                                    pass

            if on:
                item = self.object_list[0]
                item.draw_circle()

            if off:
                item = self.object_list[1]
                item.draw_circle()
            #print(" object_list length ==========", len(self.object_list))
            pg.display.flip()

Whew! That’s a lot of loops inside loops. So, when you hear the fans going off that’s because you’re hammering the CPU so the fans have to turn on to cool it down. That means you’re probably doing a lot of extra processing that you don’t need to do. I’m going to guess that this while loop is going fast and then running a bunch of loops inside loops so it’s doing a lot of extra work.

What you should do is get into the profiling stuff I taught you in LMorePTHW. It’s where I show you how to use cProfile to figure out what’s taking the most time, and then how to trim it down. Watch the videos.

I’d also say that you’d have an easier time speeding this up if you did some work to refactor this so that it was a while loop that called a bunch of functions, rather than this deeply nested structure you have here. But, just take some time to run this and profile it to see what you can do quick for now.

I didn’t think of that, awesome thanks!
The pygame while loop needs to be there, but I didn’t think of having the event checks separate. More to research!

fixed and working much better! Sometimes the answers are just really on the bottom shelf when I keep looking at the top, Thankyou Zed!


class DrawIt(object):
    """ class container for defining pygame screen run and iteraction """
    def __init__(self):
        self.engine = StateEngine()
        self.running = True
    def run_draw(self):
        on = False
        off = False
        new = False
        color_pick = False        
        screen.fill(globals.background_color)
        while self.running:
       
            animation_timer.tick(60)
            screen.fill(globals.background_color)
            self.engine.screen_update()
            events = pg.event.get()
            for event in events:
                if event.type == pg.QUIT:
                    self.running = False
                    pg.quit()
                    exit(0)
                if event.type == pg.MOUSEBUTTONDOWN:
                    position = pg.mouse.get_pos()
                    on, off, new, color_pick = self.engine.update_buttons(position, on, off, new, color_pick)
                    self.engine.update_draws(on, off, new, color_pick)
                    self.engine.check_color_picker(position, on, off, new, color_pick)

            if on:
                item = self.engine.object_list[0]
                item.draw_circle()

            if off:
                item = self.engine.object_list[1]
                item.draw_circle()
            #print(" object_list length ==========", len(self.object_list))
            pg.display.flip()


drawing = DrawIt()
drawing.run_draw()

Oh yeah! Way smaller and simpler.