В этом уроке мы будем строить цветовой сборщик в Pygame, используя библиотеку Хумана:
Представляя Хумана
Хуман это библиотека, которая упрощает кодирование Pygame.
Установить это просто:
pip install Hooman
Скелет Хумана выглядит следующим образом:
from hooman import Hooman import pygame window_width, window_height = 500, 500 hapi = Hooman(window_width, window_height) while hapi.is_running: hapi.flip_display() hapi.event_loop() pygame.quit()
Построение сборщика
Нам нужны два элемента Hooman:
Ползунок и текстовый элемент.
Слайдер выглядит следующим образом:
hapi.slider (x, y, ширина, высота, варианты)
где варианты – словарь
Документы говорят нам:
optional parameters background_color - the background color of the slider slider_width - the width of slider, by default it is the same as the height slider_color - the color of the slider starting_value - the starting value of the slider, by default it is in the middle of the range value_range - the range that the slider ranges between, by default it is between 0 and 1 Methods update() - this updates the slider and draws it on screen, this should be called every frame value() - this returns the current value of the slider set_value(value) - given a integer or float, this sets the value and moves the slider
Перед петлей Whice мы установили 3 ползунки:
slider_options = {"value_range": [0, 255]} r_slider = hapi.slider(50, 300, 400, 10, slider_options) g_slider = hapi.slider(50, 330, 400, 10, slider_options) b_slider = hapi.slider(50, 360, 400, 10, slider_options)
где каждый имеет диапазон от 0 до 255
Затем мы установили фон на значения слайдера. Прежде чем фоновые документы скажут:
.background used to set background color of screen hapi.background((100, 100, 100)) for r g b hapi.background(100) same as hapi.background((100, 100, 100))
Приведенный ниже фрагмент соответствует нашей цели:
while hapi.is_running: bg_col = (r_slider.value(), g_slider.value(), b_slider.value()) hapi.background(bg_col)
Цвета устанавливаются в виде кортежей из 3 значений, а именно красный, синий, зеленый
Затем после обновления ползунков:
r_slider.update() g_slider.update() b_slider.update()
Затем мы отображаем значения с помощью .Text
.Text (строка, x, y)
hapi.fill(hapi.color["black"]) r_text = "r:{}".format(r_slider.value()) hapi.text(r_text, 50, 280) g_text = "g:{}".format(g_slider.value()) hapi.text(g_text, 50, 310) b_text = "b:{}".format(b_slider.value()) hapi.text(b_text, 50, 340)
Полный фрагмент:
from hooman import Hooman import pygame window_width, window_height = 500, 500 hapi = Hooman(window_width, window_height) slider_options = {"value_range": [0, 255]} r_slider = hapi.slider(50, 300, 400, 10, slider_options) g_slider = hapi.slider(50, 330, 400, 10, slider_options) b_slider = hapi.slider(50, 360, 400, 10, slider_options) while hapi.is_running: bg_col = (r_slider.value(), g_slider.value(), b_slider.value()) hapi.background(bg_col) r_slider.update() g_slider.update() b_slider.update() hapi.fill(hapi.color["black"]) r_text = "r:{}".format(r_slider.value()) hapi.text(r_text, 50, 280) g_text = "g:{}".format(g_slider.value()) hapi.text(g_text, 50, 310) b_text = "b:{}".format(b_slider.value()) hapi.text(b_text, 50, 340) hapi.flip_display() hapi.event_loop() pygame.quit()
Оригинал: “https://dev.to/abdurrahmaanj/building-a-color-picker-in-pygame-using-hooman-307m”