/
ParaD
/
Server
Обзор
Документация
Войти
/
ParaD
/
Server
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
frontend_function.py
93 строки
6 KB
Rayfaga
first_commit
01 мар 2025, 20:02
01 мар 2025, 20:02
c75de2b
Код
Авторство
О чём код?
import pygame def function_selection(screen, sel_fun): button_spacing = 20 screen_info = pygame.display.Info() screen_width, screen_height = screen_info.current_w, screen_info.current_h pygame.display.set_caption("Работа с компановкой вагона") background_color = (220, 220, 220) screen.fill(background_color) font = pygame.font.SysFont("Arial", 32, bold=True) button_color = (56, 87, 35) button_hover_color = (76, 107, 55) text_color = (255, 255, 255) button_width, button_height = 8 * screen_width // 10, 60 otvet = '' robot_window = pygame.display.set_mode((screen_width, screen_height)) robot_window.fill(background_color) title_text = font.render("Выберите роботов, которым работать по этой компановке", True, (0, 0, 0)) robot_window.blit(title_text, (screen_width // 2 - title_text.get_width() // 2, 20)) try: with open("./variable_txt/count_robots.txt", "r", encoding="utf-8") as file: robot_count = int(file.read().strip()) except (FileNotFoundError, ValueError): robot_count = 0 robot_buttons = [] selected_robots = set() y_offset = 100 checkbox_size = 30 checkbox_color = (0, 0, 0) for i in range(1, robot_count + 1): button_rect = pygame.Rect(screen_width // 4, y_offset, button_width // 2, button_height) checkbox_rect = pygame.Rect(button_rect.right + 10, y_offset + button_height // 2 - checkbox_size // 2, checkbox_size, checkbox_size) pygame.draw.rect(robot_window, button_color, button_rect, border_radius=15) pygame.draw.rect(robot_window, (255, 255, 255), checkbox_rect) pygame.draw.rect(robot_window, checkbox_color, checkbox_rect, 2) text_surface = font.render(str(i), True, text_color) robot_window.blit(text_surface, (button_rect.centerx - text_surface.get_width() // 2, button_rect.centery - text_surface.get_height() // 2)) robot_buttons.append((button_rect, checkbox_rect, i)) y_offset += button_height + button_spacing accept_button = pygame.Rect(30, screen_height - 100, button_width // 2, button_height) cancel_button = pygame.Rect(30 + 3 * screen_width // 4 - button_width // 4, screen_height - 100, button_width // 2, button_height) pygame.draw.rect(robot_window, button_color, accept_button, border_radius=15) pygame.draw.rect(robot_window, button_color, cancel_button, border_radius=15) accept_text = font.render("Принять", True, text_color) cancel_text = font.render("Отмена", True, text_color) robot_window.blit(accept_text, (30 + accept_text.get_width() // 2, accept_button.centery - accept_text.get_height() // 2)) robot_window.blit(cancel_text, (cancel_button.centerx - cancel_text.get_width() // 2, cancel_button.centery - cancel_text.get_height() // 2)) pygame.display.flip() running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() return otvet elif event.type == pygame.MOUSEBUTTONDOWN: for button_rect, checkbox_rect, robot_id in robot_buttons: if button_rect.collidepoint(event.pos): if robot_id in selected_robots: selected_robots.remove(robot_id) else: selected_robots.add(robot_id) if checkbox_rect.collidepoint(event.pos): if robot_id in selected_robots: selected_robots.remove(robot_id) else: selected_robots.add(robot_id) if accept_button.collidepoint(event.pos): with open("./variable_txt/from_comp.txt", "w") as f: f.write(f"{sorted(selected_robots)}_FUNCTION_{sel_fun}") running = False elif cancel_button.collidepoint(event.pos): running = False robot_window.fill(background_color) robot_window.blit(title_text, (screen_width // 2 - title_text.get_width() // 2, 20)) y_offset = 100 for button_rect, checkbox_rect, robot_id in robot_buttons: pygame.draw.rect(robot_window, button_color, button_rect, border_radius=15) pygame.draw.rect(robot_window, (255, 255, 255), checkbox_rect) pygame.draw.rect(robot_window, checkbox_color, checkbox_rect, 2) if robot_id in selected_robots: pygame.draw.line(robot_window, checkbox_color, (checkbox_rect.left + 5, checkbox_rect.centery), (checkbox_rect.right - 5, checkbox_rect.centery), 3) text_surface = font.render(str(robot_id), True, text_color) robot_window.blit(text_surface, (button_rect.centerx - text_surface.get_width() // 2, button_rect.centery - text_surface.get_height() // 2)) y_offset += button_height + button_spacing pygame.draw.rect(robot_window, button_color, accept_button, border_radius=15) pygame.draw.rect(robot_window, button_color, cancel_button, border_radius=15) robot_window.blit(accept_text, (accept_button.centerx - accept_text.get_width() // 2, accept_button.centery - accept_text.get_height() // 2)) robot_window.blit(cancel_text, (cancel_button.centerx - cancel_text.get_width() // 2, cancel_button.centery - cancel_text.get_height() // 2)) pygame.display.flip() if __name__ == "__main__": pygame.init() function_selection(pygame.display.set_mode((pygame.display.Info().current_w, pygame.display.Info().current_h)), '1')