```
from lgs_classes import *

clock = pygame.time.Clock()
back = (80, 80, 80)
mw = pygame.display.set_mode((500, 500), pygame.RESIZABLE)

def moder(act):
    global objects, wires, wire_dict
    if context_mode_list == "add_object":
        if act.startswith("gate_"):
            objects.append(Gate(mw, old_mouse_x - 53, old_mouse_y - 53, act.split("gate_", 1)[-1]))
        elif act.startswith("btn_"):
            objects.append(Button(mw, old_mouse_x - 42, old_mouse_y - 42, act.split("btn_", 1)[-1]))
        elif act == "light":
            objects.append(Lamp(mw, old_mouse_x - 37, old_mouse_y - 37))
        elif act == "timer":
            objects.append(Timer(mw, old_mouse_x - 51, old_mouse_y - 51))
        elif act == "speaker":
            objects.append(Speaker(mw, old_mouse_x - 51, old_mouse_y - 51))
            
    elif context_mode_list == "context_menu":
        if act[1] == "delete":
            for wire in wires[:]:
                if wire.get_input() == act[2] or wire.end_object == act[2]:
                    wires.remove(wire)
                    wire_key = (wire.get_input(), wire.end_object)
                    if wire_key in wire_dict:
                        del wire_dict[wire_key]
            objects.remove(act[2])
            sounds_dict["delete_object"].play()
            for obj in objects:
                for key in obj.connects_dict:
                    if obj.connects_dict[key] == act[2]:
                        obj.connects_dict[key] = None
                        
        elif act[1].startswith("change_color_"):
            act[2].change_color(act[1].split("change_color_", 1)[-1])

        elif act[1].startswith("timer_"):
            act[2].delay = max(0, act[2].delay + float(act[1].split("timer_", 1)[-1]))

        elif act[1].startswith("speaker_"):
            act[2].freq = max(200, min(act[2].freq + int(act[1].split("speaker_", 1)[-1]), 2000))
            act[2].update_freq


def context_menu(m_names, x, y, work_mode="add_object", long=200):
    height = 10
    for i in m_names:
        height += 20
    if y + height >= mw.get_size()[1]:
        y -= height
    if x + long >= mw.get_size()[0]:
        x -= long
    
    if work_mode == "add_object":
        global context_list
        context_list = []
        offset = 10
        for i in range(len(m_names)):
            l = Label(mw, long, 18, x, y + offset, (46,46,46))
            context_list.append(l)
            offset += 20
        for i in range(len(m_names)):
            context_list[i].set_text(m_names[i], 30, (255,255,255))
            
    elif work_mode == "context_menu":
        context_list = []
        offset = 10
        for i in range(len(context_opt_list)):
            l = Label(mw, long, 18, x, y + offset, (46,46,46))
            context_list.append(l)
            offset += 20
        for i in range(len(context_opt_list)):
            context_list[i].set_text(context_opt_list[i][0], 30, (255,255,255))
    

connecting = False

wire_dict = {}
wires = []

context_list = []
context_opt_list = []
context_mode_list = None

drawing = Drawing(mw)

gui_grab = Gui(mw, -10, -5, "gui_grab", "gui_grab_pressed", 40, (0, -5))
gui_connect = Gui(mw, 70, -5, "gui_connect", "gui_connect_pressed", 40, (-5, -5))

connector = Connector(mw, 100, 100, "connector", 25, (-30, -30))
connector_wire = Wire(None, connector)

dragging = connector

objects = []

offset_x = 0
offset_y = 0

old_x = 0
old_y = 0

old_mouse_x = 0
old_mouse_y = 0

s = False

guing = False

running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            running = False
            break

        if guing:
            for i, obj in enumerate(context_list):
                if obj.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]):
                    obj.change_color((255, 128, 0))
                    if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                        moder(context_opt_list[i])
                else:
                    obj.change_color((46, 46, 46))

        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1 and gui_grab.collidepoint(event.pos[0], event.pos[1]):
                gui_grab.press()
            if event.button == 1 and gui_connect.collidepoint(event.pos[0], event.pos[1]):
                gui_connect.press()
            if event.button == 3:
                old_mouse_x, old_mouse_y = event.pos
                s = True
                for object_ in objects:
                    if object_.collidepoint(event.pos[0], event.pos[1]):
                        s = False
                        break
                if s:
                    if guing:
                        context_opt_list = []
                        context_list = []
                        guing = False
                    else:
                        context_mode_list = "add_object"
                        context_opt_list = ["btn_button",
                                          "btn_toggle",
                                          "light",
                                          "timer",
                                          "speaker",
                                          "gate_and",
                                          "gate_or",
                                          "gate_xor",
                                          "gate_nand",
                                          "gate_nor",
                                          "gate_xnor",
                                          "gate_not"]
                        context_menu(list(objects_names.values()), old_mouse_x, old_mouse_y, long=160)
                        guing = True
                        
            elif guing:
                context_opt_list = []
                context_list = []
                guing = False

            if not guing:
                for object_ in objects:
                    if event.button == 3 and object_.collidepoint(event.pos[0], event.pos[1]):
                        if guing:
                            context_opt_list = []
                            context_list = []
                            guing = False
                        else:
                            context_mode_list = "context_menu"
                            if object_.get_type() == "light":
                                context_opt_list = [(words["delete"], "delete", object_),
                                                  (words["change_color_white"], "change_color_white", object_),
                                                  (words["change_color_red"], "change_color_red", object_),
                                                  (words["change_color_yellow"], "change_color_yellow", object_),
                                                  (words["change_color_green"], "change_color_green", object_),
                                                  (words["change_color_blue"], "change_color_blue", object_)]                                
                                context_menu(context_opt_list, old_mouse_x, old_mouse_y, "context_menu", 180)
                                
                            elif object_.get_type() == "timer":
                                context_opt_list = [(words["delete"], "delete", object_),
                                                  (words["time_add_1_sec"], "timer_1.0", object_),
                                                  (words["time_add_100_ms"], "timer_0.1", object_),
                                                  (words["time_add_10_ms"], "timer_0.01", object_),
                                                  (words["time_subtract_1_sec"], "timer_-1.0", object_),
                                                  (words["time_subtract_100_ms"], "timer_-0.1", object_),
                                                  (words["time_subtract_10_ms"], "timer_-0.01", object_)]                        
                                context_menu(context_opt_list, old_mouse_x, old_mouse_y, "context_menu", 180)

                            elif object_.get_type() == "speaker":
                                context_opt_list = [(words["delete"], "delete", object_),
                                                  (words["speaker_add"], "speaker_200", object_),
                                                  (words["speaker_subtract"], "speaker_-200", object_)]                        
                                context_menu(context_opt_list, old_mouse_x, old_mouse_y, "context_menu", 210)

                            else:
                                context_opt_list = [(words["delete"], "delete", object_)]
                                context_menu(context_opt_list, old_mouse_x, old_mouse_y, "context_menu", 120)

                            context_mode_list = "context_menu"
                            guing = True
   
                        break

            if not guing:
                for object_ in objects:
                    if event.button == 1 and object_.collidepoint(event.pos[0], event.pos[1]):
                        if gui_grab.togglemode and not gui_connect.togglemode:
                            if object_.get_type() in ["gate", "button", "light", "timer", "speaker"]:
                                sounds_dict["take"].play()
                                dragging = object_
                                offset_x = dragging.x - event.pos[0]
                                offset_y = dragging.y - event.pos[1]
                                
                                objects.remove(dragging)
                                objects.append(dragging)

                        if event.button == 1 and object_.collidepoint(event.pos[0], event.pos[1]) and not gui_connect.togglemode:
                            if object_.get_type() == "button":
                                if object_.button_mode == "button":
                                    object_.press("down")
                                elif object_.button_mode == "toggle":
                                    object_.press(None)
                                    break
                                
                        dragging = object_
                        if gui_connect.togglemode:
                            dragging = object_
                            connector_wire.start_object = dragging
                            connector.x = pygame.mouse.get_pos()[0] - 8
                            connector.y = pygame.mouse.get_pos()[1] - 8
                            connecting = not dragging.get_type() in ["light", "speaker"]

        elif event.type == pygame.MOUSEBUTTONUP and dragging:
            if event.button == 1:
                for object_ in objects:
                    if object_.get_type() == "button" and object_.button_mode == "button" and object_.togglemode:
                        object_.press("up")

                for object_ in objects:
                    if object_.collidepoint(event.pos[0], event.pos[1]) and object_ != dragging:
                        if not (object_.get_type() in ["button"]
                            and not dragging.get_type() in ["light", "speaker"]) and gui_connect.togglemode:
                            wire_key = (dragging, object_)
                            if wire_key in wire_dict:
                                existing_wire = wire_dict[wire_key]
                                wires.remove(existing_wire)
                                del wire_dict[wire_key]

                                for key in object_.connects_dict:
                                    if object_.connects_dict[key] == dragging:
                                        object_.connects_dict[key] = None
                                        break  
                                sounds_dict["disconnecting"].play()
                                
                            else:
                                connected = False
                                for n in range(2):
                                    if f"input{n + 1}" in object_.connects_dict:
                                        if object_.connects_dict[f"input{n + 1}"] is None:
                                            new_wire = Wire(dragging, object_)
                                            wires.append(new_wire)
                                            wire_dict[wire_key] = new_wire

                                            object_.connects_dict[f"input{n + 1}"] = dragging
                                            sounds_dict["connecting"].play()
                                            connected = True
                                            break

                dragging = None
                connecting = False

        if event.type == pygame.MOUSEMOTION and dragging and gui_grab.togglemode and not gui_connect.togglemode:
            dragging.x = event.pos[0] + offset_x
            dragging.y = event.pos[1] + offset_y

    if not running:
        break

    #   ---------     Drawing:    ---------

    mw.fill(back)

    for wire in wires:
        if wire.get_input().get_signal():
            wire.draw(drawing, (228, 101, 0))
        else:
            wire.draw(drawing, (228, 228, 228))

    if connecting:
        if connector_wire.get_input().get_signal():
            connector_wire.draw(drawing, (228, 101, 0))
        else:
            connector_wire.draw(drawing, (228, 228, 228))

    if connecting:
        connector.x = pygame.mouse.get_pos()[0] - 8
        connector.y = pygame.mouse.get_pos()[1] - 8
        connector.draw(drawing)

    for object_ in objects:
        object_.draw(drawing)

    gui_grab.draw(drawing)
    gui_connect.draw(drawing)

    for act in context_list:
        act.draw(drawing)

    pygame.display.update()
    clock.tick(60)
```

Перенеси всю логику создания контекстного меню в отдельный класс. То есть, функции context_... - переносятся в этот класс, а переменные context_... - становятся локальными переменными этого класса, а не глобальными. (В конце выдай полный, обновлённый код с изменениями)

(History of the chat:
User  :
```
from lgs_classes import *

clock = pygame.time.Clock()
back = (80, 80, 80)
mw = pygame.display.set_mode((500, 500), pygame.RESIZABLE)

def moder(act):
    global objects, wires, wire_dict
    if context_mode_list == "add_object":
        if act.startswith("gate_"):
            objects.append(Gate(mw, old_mouse_x - 53, old_mouse_y - 53, act.split("gate_", 1)[-1]))
        elif act.startswith("btn_"):
            objects.append(Button(mw, old_mouse_x - 42, old_mouse_y - 42, act.split("btn_", 1)[-1]))
        elif act == "light":
            objects.append(Lamp(mw, old_mouse_x - 37, old_mouse_y - 37))
        elif act == "timer":
            objects.append(Timer(mw, old_mouse_x - 51, old_mouse_y - 51))
        elif act == "speaker":
            objects.append(Speaker(mw, old_mouse_x - 51, old_mouse_y - 51))
            
    elif context_mode_list == "context_menu":
        if act[1] == "delete":
            for wire in wires[:]:
                if wire.get_input() == act[2] or wire.end_object == act[2]:
                    wires.remove(wire)
                    wire_key = (wire.get_input(), wire.end_object)
                    if wire_key in wire_dict:
                        del wire_dict[wire_key]
            objects.remove(act[2])
            sounds_dict["delete_object"].play()
            for obj in objects:
                for key in obj.connects_dict:
                    if obj.connects_dict[key] == act[2]:
                        obj.connects_dict[key] = None
                        
        elif act[1].startswith("change_color_"):
            act[2].change_color(act[1].split("change_color_", 1)[-1])

        elif act[1].startswith("timer_"):
            act[2].delay = max(0, act[2].delay + float(act[1].split("timer_", 1)[-1]))

        elif act[1].startswith("speaker_"):
            act[2].freq = max(200, min(act[2].freq + int(act[1].split("speaker_", 1)[-1]), 2000))
            act[2].update_freq


def context_menu(m_names, x, y, work_mode="add_object", long=200):
    height = 10
    for i in m_names:
        height += 20
    if y + height >= mw.get_size()[1]:
        y -= height
    if x + long >= mw.get_size()[0]:
        x -= long
    
    if work_mode == "add_object":
        global context_list
        context_list = []
        offset = 10
        for i in range(len(m_names)):
            l = Label(mw, long, 18, x, y + offset, (46,46,46))
            context_list.append(l)
            offset += 20
        for i in range(len(m_names)):
            context_list[i].set_text(m_names[i], 30, (255,255,255))
            
    elif work_mode == "context_menu":
        context_list = []
        offset = 10
        for i in range(len(context_opt_list)):
            l = Label(mw, long, 18, x, y + offset, (46,46,46))
            context_list.append(l)
            offset += 20
        for i in range(len(context_opt_list)):
            context_list[i].set_text(context_opt_list[i][0], 30, (255,255,255))
    

connecting = False

wire_dict = {}
wires = []

context_list = []
context_opt_list = []
context_mode_list = None

drawing = Drawing(mw)

gui_grab = Gui(mw, -10, -5, "gui_grab", "gui_grab_pressed", 40, (0, -5))
gui_connect = Gui(mw, 70, -5, "gui_connect", "gui_connect_pressed", 40, (-5, -5))

connector = Connector(mw, 100, 100, "connector", 25, (-30, -30))
connector_wire = Wire(None, connector)

dragging = connector

objects = []

offset_x = 0
offset_y = 0

old_x = 0
old_y = 0

old_mouse_x = 0
old_mouse_y = 0

s = False

guing = False

running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            running = False
            break

        if guing:
            for i, obj in enumerate(context_list):
                if obj.collidepoint(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]):
                    obj.change_color((255, 128, 0))
                    if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                        moder(context_opt_list[i])
                else:
                    obj.change_color((46, 46, 46))

        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1 and gui_grab.collidepoint(event.pos[0], event.pos[1]):
                gui_grab.press()
            if event.button == 1 and gui_connect.collidepoint(event.pos[0], event.pos[1]):
                gui_connect.press()
            if event.button == 3:
                old_mouse_x, old_mouse_y = event.pos
                s = True
                for object_ in objects:
                    if object_.collidepoint(event.pos[0], event.pos[1]):
                        s = False
                        break
                if s:
                    if guing:
                        context_opt_list = []
                        context_list = []
                        guing = False
                    else:
                        context_mode_list = "add_object"
                        context_opt_list = ["btn_button",
                                          "btn_toggle",
                                          "light",
                                          "timer",
                                          "speaker",
                                          "gate_and",
                                          "gate_or",
                                          "gate_xor",
                                          "gate_nand",
                                          "gate_nor",
                                          "gate_xnor",
                                          "gate_not"]
                        context_menu(list(objects_names.values()), old_mouse_x, old_mouse_y, long=160)
                        guing = True
                        
            elif guing:
                context_opt_list = []
                context_list = []
                guing = False

            if not guing:
                for object_ in objects:
                    if event.button == 3 and object_.collidepoint(event.pos[0], event.pos[1]):
                        if guing:
                            context_opt_list = []
                            context_list = []
                            guing = False
                        else:
                            context_mode_list = "context_menu"
                            if object_.get_type() == "light":
                                context_opt_list = [(words["delete"], "delete", object_),
                                                  (words["change_color_white"], "change_color_white", object_),
                                                  (words["change_color_red"], "change_color_red", object_),
                                                  (words["change_color_yellow"], "change_color_yellow", object_),
                                                  (words["change_color_green"], "change_color_green", object_),
                                                  (words["change_color_blue"], "change_color_blue", object_)]                                
                                context_menu(context_opt_list, old_mouse_x, old_mouse_y, "context_menu", 180)
                                
                            elif object_.get_type() == "timer":
                                context_opt_list = [(words["delete"], "delete", object_),
                                                  (words["time_add_1_sec"], "timer_1.0", object_),
                                                  (words["time_add_100_ms"], "timer_0.1", object_),
                                                  (words["time_add_10_ms"], "timer_0.01", object_),
                                                  (words["time_subtract_1_sec"], "timer_-1.0", object_),
                                                  (words["time_subtract_100_ms"], "timer_-0.1", object_),
                                                  (words["time_subtract_10_ms"], "timer_-0.01", object_)]                        
                                context_menu(context_opt_list, old_mouse_x, old_mouse_y, "context_menu", 180)

                            elif object_.get_type() == "speaker":
                                context_opt_list = [(words["delete"], "delete", object_),
                                                  (words["speaker_add"], "speaker_200", object_),
                                                  (words["speaker_subtract"], "speaker_-200", object_)]                        
                                context_menu(context_opt_list, old_mouse_x, old_mouse_y, "context_menu", 210)

                            else:
                                context_opt_list = [(words["delete"], "delete", object_)]
                                context_menu(context_opt_list, old_mouse_x, old_mouse_y, "context_menu", 120)

                            context_mode_list = "context_menu"
                            guing = True
   
                        break

            if not guing:
                for object_ in objects:
                    if event.button == 1 and object_.collidepoint(event.pos[0], event.pos[1]):
                        if gui_grab.togglemode and not gui_connect.togglemode:
                            if object_.get_type() in ["gate", "button", "light", "timer", "speaker"]:
                                sounds_dict["take"].play()
                                dragging = object_
                                offset_x = dragging.x - event.pos[0]
                                offset_y = dragging.y - event.pos[1]
                                
                                objects.remove(dragging)
                                objects.append(dragging)

                        if event.button == 1 and object_.collidepoint(event.pos[0], event.pos[1]) and not gui_connect.togglemode:
                            if object_.get_type() == "button":
                                if object_.button_mode == "button":
                                    object_.press("down")
                                elif object_.button_mode == "toggle":
                                    object_.press(None)
                                    break
                                
                        dragging = object_
                        if gui_connect.togglemode:
                            dragging = object_
                            connector_wire.start_object = dragging
                            connector.x = pygame.mouse.get_pos()[0] - 8
                            connector.y = pygame.mouse.get_pos()[1] - 8
                            connecting = not dragging.get_type() in ["light", "speaker"]

        elif event.type == pygame.MOUSEBUTTONUP and dragging:
            if event.button == 1:
                for object_ in objects:
                    if object_.get_type() == "button" and object_.button_mode == "button" and object_.togglemode:
                        object_.press("up")

                for object_ in objects:
                    if object_.collidepoint(event.pos[0], event.pos[1]) and object_ != dragging:
                        if not (object_.get_type() in ["button"]
                            and not dragging.get_type() in ["light", "speaker"]) and gui_connect.togglemode:
                            wire_key = (dragging, object_)
                            if wire_key in wire_dict:
                                existing_wire = wire_dict[wire_key]
                                wires.remove(existing_wire)
                                del wire_dict[wire_key]

                                for key in object_.connects_dict:
                                    if object_.connects_dict[key] == dragging:
                                        object_.connects_dict[key] = None
                                        break  
                                sounds_dict["disconnecting"].play()
                                
                            else:
                                connected = False
                                for n in range(2):
                                    if f"input{n + 1}" in object_.connects_dict:
                                        if object_.connects_dict[f"input{n + 1}"] is None:
                                            new_wire = Wire(dragging, object_)
                                            wires.append(new_wire)
                                            wire_dict[wire_key] = new_wire

                                            object_.connects_dict[f"input{n + 1}"] = dragging
                                            sounds_dict["connecting"].play()
                                            connected = True
                                            break

                dragging = None
                connecting = False

        if event.type == pygame.MOUSEMOTION and dragging and gui_grab.togglemode and not gui_connect.togglemode:
            dragging.x = event.pos[0] + offset_x
            dragging.y = event.pos[1] + offset_y

    if not running:
        break

    #   ---------     Drawing:    ---------

    mw.fill(back)

    for wire in wires:
        if wire.get_input().get_signal():
            wire.draw(drawing, (228, 101, 0))
        else:
            wire.draw(drawing, (228, 228, 228))

    if connecting:
        if connector_wire.get_input().get_signal():
            connector_wire.draw(drawing, (228, 101, 0))
        else:
            connector_wire.draw(drawing, (228, 228, 228))

    if connecting:
        connector.x = pygame.mouse.get_pos()[0] - 8
        connector.y = pygame.mouse.get_pos()[1] - 8
        connector.draw(drawing)

    for object_ in objects:
        object_.draw(drawing)

    gui_grab.draw(drawing)
    gui_connect.draw(drawing)

    for act in context_list:
        act.draw(drawing)

    pygame.display.update()
    clock.tick(60)
```

Перенеси всю логику создания контекстного меню в отдельный класс. То есть, функции context_... - переносятся в этот класс, а переменные context_... - становятся локальными переменными этого класса, а не глобальными. (В конце выдай полный, обновлённый код с изменениями)


)