Рубрики
Без рубрики

Создание палитры цветов в PyGame с помощью Hooman

Создание палитры цветов с помощью Python

Автор оригинала: Abdur-Rahmaan Janhangeer.

В этом уроке мы будем создавать палитру цветов в PyGame, используя библиотеку Hooman:

Текст Alt

Представляем Хумана

Hooman – это библиотека, которая упрощает кодирование 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()

Построение сборщика

Нам нужны два элемента Хумана:

Ползунок и текстовый элемент.

Ползунок выглядит следующим образом:

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

Перед циклом while мы устанавливаем 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

.текст(строка, 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()