/
megaokna43
/
TestSchulteTable
Обзор
Документация
Войти
/
megaokna43
/
TestSchulteTable
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/user_input.py
75 строк
3 KB
megaokna43
mvp_test_shulte, initial Readme.md, requirements.txt
09 апр 2025, 05:33
09 апр 2025, 05:33
1208eb8
Код
Авторство
О чём код?
import pygame import time class UserInput: @staticmethod def get_user_name(): """Получает имя пользователя через графический интерфейс с мигающим курсором""" pygame.init() screen = pygame.display.set_mode((400, 200)) pygame.display.set_caption("Введите ваше имя и фамилию") font = pygame.font.SysFont('Arial', 32) input_text = "" # Переменные курсора cursor_visible = True last_cursor_toggle = time.time() cursor_blink_interval = 0.5 # seconds cursor_position = 0 # Position at end of text input_active = True while input_active: current_time = time.time() # Переключить видимость курсора для эффекта мигания if current_time - last_cursor_toggle > cursor_blink_interval: cursor_visible = not cursor_visible last_cursor_toggle = current_time screen.fill((255, 255, 255)) prompt = font.render("Введите ваше имя:", True, (0, 0, 0)) screen.blit(prompt, (20, 20)) # Нарисовать поле ввода pygame.draw.rect(screen, (200, 200, 200), (20, 70, 360, 50)) # Render input text text_surface = font.render(input_text, True, (0, 0, 0)) screen.blit(text_surface, (30, 80)) # Нарисовать мигающий курсор if cursor_visible: cursor_x = 30 + font.size(input_text[:cursor_position])[0] pygame.draw.line(screen, (0, 0, 0), (cursor_x, 80), (cursor_x, 80 + font.get_height()), 2) pygame.display.flip() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() return "Unknown" if event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: input_active = False elif event.key == pygame.K_BACKSPACE: if cursor_position > 0: input_text = input_text[:cursor_position - 1] + input_text[cursor_position:] cursor_position -= 1 elif event.key == pygame.K_LEFT: cursor_position = max(0, cursor_position - 1) elif event.key == pygame.K_RIGHT: cursor_position = min(len(input_text), cursor_position + 1) else: # Insert character at cursor position input_text = input_text[:cursor_position] + event.unicode + input_text[cursor_position:] cursor_position += 1 # Reset cursor visibility when typing cursor_visible = True last_cursor_toggle = current_time pygame.quit() return input_text if input_text else "Unknown"