In this tutorial we’ll be building a color picker in PyGame using the Hooman library:
Introducing Hooman
Hooman is a library which simplifies PyGame coding.
To install it’s just:
pip install Hoomanpip install Hoomanpip install Hooman
The Hooman skeleton is as follows:
<span>from</span> <span>hooman</span> <span>import</span> <span>Hooman</span><span>import</span> <span>pygame</span><span>window_width</span><span>,</span> <span>window_height</span> <span>=</span> <span>500</span><span>,</span> <span>500</span><span>hapi</span> <span>=</span> <span>Hooman</span><span>(</span><span>window_width</span><span>,</span> <span>window_height</span><span>)</span><span>while</span> <span>hapi</span><span>.</span><span>is_running</span><span>:</span><span>hapi</span><span>.</span><span>flip_display</span><span>()</span><span>hapi</span><span>.</span><span>event_loop</span><span>()</span><span>pygame</span><span>.</span><span>quit</span><span>()</span><span>from</span> <span>hooman</span> <span>import</span> <span>Hooman</span> <span>import</span> <span>pygame</span> <span>window_width</span><span>,</span> <span>window_height</span> <span>=</span> <span>500</span><span>,</span> <span>500</span> <span>hapi</span> <span>=</span> <span>Hooman</span><span>(</span><span>window_width</span><span>,</span> <span>window_height</span><span>)</span> <span>while</span> <span>hapi</span><span>.</span><span>is_running</span><span>:</span> <span>hapi</span><span>.</span><span>flip_display</span><span>()</span> <span>hapi</span><span>.</span><span>event_loop</span><span>()</span> <span>pygame</span><span>.</span><span>quit</span><span>()</span>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()
Building the picker
We need two Hooman elements:
The Slider and the text element.
The slider is as follows:
hapi.slider(x, y, width, height, options)
where options is a dictionary
The docs tell us:
optional parametersbackground_color - the background color of the sliderslider_width - the width of slider, by default it is the same as the heightslider_color - the color of the sliderstarting_value - the starting value of the slider, by default it is in the middle of the rangevalue_range - the range that the slider ranges between, by default it is between 0 and 1Methodsupdate() - this updates the slider and draws it on screen, this should be called every framevalue() - this returns the current value of the sliderset_value(value) - given a integer or float, this sets the value and moves the slideroptional 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 slideroptional 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
Before the while loop we set 3 sliders:
<span>slider_options</span> <span>=</span> <span>{</span><span>"value_range"</span><span>:</span> <span>[</span><span>0</span><span>,</span> <span>255</span><span>]}</span><span>r_slider</span> <span>=</span> <span>hapi</span><span>.</span><span>slider</span><span>(</span><span>50</span><span>,</span> <span>300</span><span>,</span> <span>400</span><span>,</span> <span>10</span><span>,</span> <span>slider_options</span><span>)</span><span>g_slider</span> <span>=</span> <span>hapi</span><span>.</span><span>slider</span><span>(</span><span>50</span><span>,</span> <span>330</span><span>,</span> <span>400</span><span>,</span> <span>10</span><span>,</span> <span>slider_options</span><span>)</span><span>b_slider</span> <span>=</span> <span>hapi</span><span>.</span><span>slider</span><span>(</span><span>50</span><span>,</span> <span>360</span><span>,</span> <span>400</span><span>,</span> <span>10</span><span>,</span> <span>slider_options</span><span>)</span><span>slider_options</span> <span>=</span> <span>{</span><span>"value_range"</span><span>:</span> <span>[</span><span>0</span><span>,</span> <span>255</span><span>]}</span> <span>r_slider</span> <span>=</span> <span>hapi</span><span>.</span><span>slider</span><span>(</span><span>50</span><span>,</span> <span>300</span><span>,</span> <span>400</span><span>,</span> <span>10</span><span>,</span> <span>slider_options</span><span>)</span> <span>g_slider</span> <span>=</span> <span>hapi</span><span>.</span><span>slider</span><span>(</span><span>50</span><span>,</span> <span>330</span><span>,</span> <span>400</span><span>,</span> <span>10</span><span>,</span> <span>slider_options</span><span>)</span> <span>b_slider</span> <span>=</span> <span>hapi</span><span>.</span><span>slider</span><span>(</span><span>50</span><span>,</span> <span>360</span><span>,</span> <span>400</span><span>,</span> <span>10</span><span>,</span> <span>slider_options</span><span>)</span>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)
where each has a range of 0 to 255
Then we set the background to the slider values. Before the background docs tell:
.backgroundused to set background color of screenhapi.background((100, 100, 100)) for r g bhapi.background(100) same as hapi.background((100, 100, 100)).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)).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))
The below snippet suits our purpose:
<span>while</span> <span>hapi</span><span>.</span><span>is_running</span><span>:</span><span>bg_col</span> <span>=</span> <span>(</span><span>r_slider</span><span>.</span><span>value</span><span>(),</span> <span>g_slider</span><span>.</span><span>value</span><span>(),</span> <span>b_slider</span><span>.</span><span>value</span><span>())</span><span>hapi</span><span>.</span><span>background</span><span>(</span><span>bg_col</span><span>)</span><span>while</span> <span>hapi</span><span>.</span><span>is_running</span><span>:</span> <span>bg_col</span> <span>=</span> <span>(</span><span>r_slider</span><span>.</span><span>value</span><span>(),</span> <span>g_slider</span><span>.</span><span>value</span><span>(),</span> <span>b_slider</span><span>.</span><span>value</span><span>())</span> <span>hapi</span><span>.</span><span>background</span><span>(</span><span>bg_col</span><span>)</span>while hapi.is_running: bg_col = (r_slider.value(), g_slider.value(), b_slider.value()) hapi.background(bg_col)
colors are set as tuples of 3 values viz red, blue, green
Then after we update the sliders:
<span>r_slider</span><span>.</span><span>update</span><span>()</span><span>g_slider</span><span>.</span><span>update</span><span>()</span><span>b_slider</span><span>.</span><span>update</span><span>()</span><span>r_slider</span><span>.</span><span>update</span><span>()</span> <span>g_slider</span><span>.</span><span>update</span><span>()</span> <span>b_slider</span><span>.</span><span>update</span><span>()</span>r_slider.update() g_slider.update() b_slider.update()
Then we display the values using .text
.text(the string, x, y)
<span>hapi</span><span>.</span><span>fill</span><span>(</span><span>hapi</span><span>.</span><span>color</span><span>[</span><span>"black"</span><span>])</span><span>r_text</span> <span>=</span> <span>"r:{}"</span><span>.</span><span>format</span><span>(</span><span>r_slider</span><span>.</span><span>value</span><span>())</span><span>hapi</span><span>.</span><span>text</span><span>(</span><span>r_text</span><span>,</span> <span>50</span><span>,</span> <span>280</span><span>)</span><span>g_text</span> <span>=</span> <span>"g:{}"</span><span>.</span><span>format</span><span>(</span><span>g_slider</span><span>.</span><span>value</span><span>())</span><span>hapi</span><span>.</span><span>text</span><span>(</span><span>g_text</span><span>,</span> <span>50</span><span>,</span> <span>310</span><span>)</span><span>b_text</span> <span>=</span> <span>"b:{}"</span><span>.</span><span>format</span><span>(</span><span>b_slider</span><span>.</span><span>value</span><span>())</span><span>hapi</span><span>.</span><span>text</span><span>(</span><span>b_text</span><span>,</span> <span>50</span><span>,</span> <span>340</span><span>)</span><span>hapi</span><span>.</span><span>fill</span><span>(</span><span>hapi</span><span>.</span><span>color</span><span>[</span><span>"black"</span><span>])</span> <span>r_text</span> <span>=</span> <span>"r:{}"</span><span>.</span><span>format</span><span>(</span><span>r_slider</span><span>.</span><span>value</span><span>())</span> <span>hapi</span><span>.</span><span>text</span><span>(</span><span>r_text</span><span>,</span> <span>50</span><span>,</span> <span>280</span><span>)</span> <span>g_text</span> <span>=</span> <span>"g:{}"</span><span>.</span><span>format</span><span>(</span><span>g_slider</span><span>.</span><span>value</span><span>())</span> <span>hapi</span><span>.</span><span>text</span><span>(</span><span>g_text</span><span>,</span> <span>50</span><span>,</span> <span>310</span><span>)</span> <span>b_text</span> <span>=</span> <span>"b:{}"</span><span>.</span><span>format</span><span>(</span><span>b_slider</span><span>.</span><span>value</span><span>())</span> <span>hapi</span><span>.</span><span>text</span><span>(</span><span>b_text</span><span>,</span> <span>50</span><span>,</span> <span>340</span><span>)</span>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)
The complete snippet:
<span>from</span> <span>hooman</span> <span>import</span> <span>Hooman</span><span>import</span> <span>pygame</span><span>window_width</span><span>,</span> <span>window_height</span> <span>=</span> <span>500</span><span>,</span> <span>500</span><span>hapi</span> <span>=</span> <span>Hooman</span><span>(</span><span>window_width</span><span>,</span> <span>window_height</span><span>)</span><span>slider_options</span> <span>=</span> <span>{</span><span>"value_range"</span><span>:</span> <span>[</span><span>0</span><span>,</span> <span>255</span><span>]}</span><span>r_slider</span> <span>=</span> <span>hapi</span><span>.</span><span>slider</span><span>(</span><span>50</span><span>,</span> <span>300</span><span>,</span> <span>400</span><span>,</span> <span>10</span><span>,</span> <span>slider_options</span><span>)</span><span>g_slider</span> <span>=</span> <span>hapi</span><span>.</span><span>slider</span><span>(</span><span>50</span><span>,</span> <span>330</span><span>,</span> <span>400</span><span>,</span> <span>10</span><span>,</span> <span>slider_options</span><span>)</span><span>b_slider</span> <span>=</span> <span>hapi</span><span>.</span><span>slider</span><span>(</span><span>50</span><span>,</span> <span>360</span><span>,</span> <span>400</span><span>,</span> <span>10</span><span>,</span> <span>slider_options</span><span>)</span><span>while</span> <span>hapi</span><span>.</span><span>is_running</span><span>:</span><span>bg_col</span> <span>=</span> <span>(</span><span>r_slider</span><span>.</span><span>value</span><span>(),</span> <span>g_slider</span><span>.</span><span>value</span><span>(),</span> <span>b_slider</span><span>.</span><span>value</span><span>())</span><span>hapi</span><span>.</span><span>background</span><span>(</span><span>bg_col</span><span>)</span><span>r_slider</span><span>.</span><span>update</span><span>()</span><span>g_slider</span><span>.</span><span>update</span><span>()</span><span>b_slider</span><span>.</span><span>update</span><span>()</span><span>hapi</span><span>.</span><span>fill</span><span>(</span><span>hapi</span><span>.</span><span>color</span><span>[</span><span>"black"</span><span>])</span><span>r_text</span> <span>=</span> <span>"r:{}"</span><span>.</span><span>format</span><span>(</span><span>r_slider</span><span>.</span><span>value</span><span>())</span><span>hapi</span><span>.</span><span>text</span><span>(</span><span>r_text</span><span>,</span> <span>50</span><span>,</span> <span>280</span><span>)</span><span>g_text</span> <span>=</span> <span>"g:{}"</span><span>.</span><span>format</span><span>(</span><span>g_slider</span><span>.</span><span>value</span><span>())</span><span>hapi</span><span>.</span><span>text</span><span>(</span><span>g_text</span><span>,</span> <span>50</span><span>,</span> <span>310</span><span>)</span><span>b_text</span> <span>=</span> <span>"b:{}"</span><span>.</span><span>format</span><span>(</span><span>b_slider</span><span>.</span><span>value</span><span>())</span><span>hapi</span><span>.</span><span>text</span><span>(</span><span>b_text</span><span>,</span> <span>50</span><span>,</span> <span>340</span><span>)</span><span>hapi</span><span>.</span><span>flip_display</span><span>()</span><span>hapi</span><span>.</span><span>event_loop</span><span>()</span><span>pygame</span><span>.</span><span>quit</span><span>()</span><span>from</span> <span>hooman</span> <span>import</span> <span>Hooman</span> <span>import</span> <span>pygame</span> <span>window_width</span><span>,</span> <span>window_height</span> <span>=</span> <span>500</span><span>,</span> <span>500</span> <span>hapi</span> <span>=</span> <span>Hooman</span><span>(</span><span>window_width</span><span>,</span> <span>window_height</span><span>)</span> <span>slider_options</span> <span>=</span> <span>{</span><span>"value_range"</span><span>:</span> <span>[</span><span>0</span><span>,</span> <span>255</span><span>]}</span> <span>r_slider</span> <span>=</span> <span>hapi</span><span>.</span><span>slider</span><span>(</span><span>50</span><span>,</span> <span>300</span><span>,</span> <span>400</span><span>,</span> <span>10</span><span>,</span> <span>slider_options</span><span>)</span> <span>g_slider</span> <span>=</span> <span>hapi</span><span>.</span><span>slider</span><span>(</span><span>50</span><span>,</span> <span>330</span><span>,</span> <span>400</span><span>,</span> <span>10</span><span>,</span> <span>slider_options</span><span>)</span> <span>b_slider</span> <span>=</span> <span>hapi</span><span>.</span><span>slider</span><span>(</span><span>50</span><span>,</span> <span>360</span><span>,</span> <span>400</span><span>,</span> <span>10</span><span>,</span> <span>slider_options</span><span>)</span> <span>while</span> <span>hapi</span><span>.</span><span>is_running</span><span>:</span> <span>bg_col</span> <span>=</span> <span>(</span><span>r_slider</span><span>.</span><span>value</span><span>(),</span> <span>g_slider</span><span>.</span><span>value</span><span>(),</span> <span>b_slider</span><span>.</span><span>value</span><span>())</span> <span>hapi</span><span>.</span><span>background</span><span>(</span><span>bg_col</span><span>)</span> <span>r_slider</span><span>.</span><span>update</span><span>()</span> <span>g_slider</span><span>.</span><span>update</span><span>()</span> <span>b_slider</span><span>.</span><span>update</span><span>()</span> <span>hapi</span><span>.</span><span>fill</span><span>(</span><span>hapi</span><span>.</span><span>color</span><span>[</span><span>"black"</span><span>])</span> <span>r_text</span> <span>=</span> <span>"r:{}"</span><span>.</span><span>format</span><span>(</span><span>r_slider</span><span>.</span><span>value</span><span>())</span> <span>hapi</span><span>.</span><span>text</span><span>(</span><span>r_text</span><span>,</span> <span>50</span><span>,</span> <span>280</span><span>)</span> <span>g_text</span> <span>=</span> <span>"g:{}"</span><span>.</span><span>format</span><span>(</span><span>g_slider</span><span>.</span><span>value</span><span>())</span> <span>hapi</span><span>.</span><span>text</span><span>(</span><span>g_text</span><span>,</span> <span>50</span><span>,</span> <span>310</span><span>)</span> <span>b_text</span> <span>=</span> <span>"b:{}"</span><span>.</span><span>format</span><span>(</span><span>b_slider</span><span>.</span><span>value</span><span>())</span> <span>hapi</span><span>.</span><span>text</span><span>(</span><span>b_text</span><span>,</span> <span>50</span><span>,</span> <span>340</span><span>)</span> <span>hapi</span><span>.</span><span>flip_display</span><span>()</span> <span>hapi</span><span>.</span><span>event_loop</span><span>()</span> <span>pygame</span><span>.</span><span>quit</span><span>()</span>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()
暂无评论内容