This is small implementation of basic UI components to speed up building pygame projects.
Python 3.5+
- Pygame
pip install pygame
You can simply create a button like so
Button((X, Y, WIDTH, HEIGHT), (PARENT_X, PARENT_Y), COLOR, ALPHA, TEXT)
and then to draw it Button.draw(SURFACE)
.
Obligatory initial variables:
(X, Y, WIDTH, HEIGHT)
- also called asplacement
in code, a tuple representing button position and size. NOTE: Given cordinates are relative to the surface that you are drawing it on. If you pass a surface and then blit this surface onto the main screen it will be drawn on cordinates on the first surface not the main one, ex. if you set Button position to (10, 0, W, H) draw it on a surface and then blit this surface onto a main screen at (100, 100), button absulute position will be (110, 100)(PARENT_X, PARENT_Y)
- a tuple(X, Y)
, because my library support adding subobjects, to properly determine if user has clicked on an object you have to pass ojects parent absolute postion, if you are drawing the button on the main screen just pass(0, 0)
, otherwise you have to pass the absolute position of the parent. Considering previous example you would pass(10, 0)
COLOR
- Color of a button in RGB standard, you can also use myColors()
class instead of typing it by yourselfALPHA
- Transparency of the button, 255 is completly drawn and 0 is invisibleTEXT
- Text that will be shown on the button, you can always chenge it laterButton.text=""
Optional arguments:
font=
- If you want to choose different font, default ismonospace
font_size=
- Font size, default is10
font_color=
- Font color, default is blackclick_function=
If you want to execute a simple function after the button is clicked, although it is not recommended for complex functions, I will show a better way of handling button clicks.
Methods:
draw(surface)
- Execute it when drawing a button, it will be drawn on passed surfaceclicked(pos)
- ReturnsTrue
orFalse
if button is clicked for given position. Execute this function in your main event loop and passmouse_pos
to check if user has clicked the button.pos
has to be a tuple like(X, Y)
. It will also execute a functionclick_function=
that you have passed when creating a button. Check if it returnsTrue
and then do whatever you want to do after user has pressed a button. NOTE: If your button respond to clicks outside of its box there is a big chance that you have passed wrong(PARENT_X, PARENT_Y)
ormouse_pos
and it is detecting click in a wrong place
Properties:
size
- Size of a button(WIDTH, HEIGHT)
position
- Position of a button(X, Y)
text
- Text
You can simply create a checkbox like so
Checkbox((X, Y, WIDTH, HEIGHT), (PARENT_X, PARENT_Y), BOX_COLOR, INDICATOR_COLOR, ALPHA, TEXT)
and then to draw it Checkbox.draw(SURFACE)
.
Obligator initial variables:
(X, Y, WIDTH, HEIGHT)
- also called asplacement
in code, a tuple representing checkbox position and size. NOTE: Given cordinates are relative to the surface that you are drawing it on. If you pass a surface and then blit this surface onto the main screen it will be drawn on cordinates on the first surface not the main one, ex. if you set Checkbox position to (10, 0, W, H) draw it on a surface and then blit this surface onto a main screen at (100, 100), checkbox absulute position will be (110, 100)(PARENT_X, PARENT_Y)
- a tuple(X, Y)
, because my library support adding subobjects, to properly determine if user has clicked on an object you have to pass ojects parent absolute postion, if you are drawing the checkbox on the main screen just pass(0, 0)
, otherwise you have to pass the absolute position of the parent. Considering previous example you would pass(10, 0)
BOX_COLOR
- Color of a checkbox boxINDICATOR_COLOR
- Color of a circle that indicates that a checkbox is checkedALPHA
- Transparency of the checkbox, 255 is completly drawn and 0 is invisibleTEXT
- Text that will be shown on the checkbox, you can always chenge it laterCheckbox.text=""
Optional arguments:
spacing=
- How many pixels to the right a label will be rednered, default is10
font=
- If you want to choose different font, default ismonospace
font_size=
- Font size, default is10
font_color=
- Font color, default is blackclick_function=
If you want to execute a simple function after a checkbox is checked/unchecked, although it is not recommended for complex functions, I will show a better way of handling checkbox clicks.
Methods:
draw(surface)
- Execute it when drawing a checkbox, it will be drawn on passed surfaceclicked(pos)
- ReturnsTrue
orFalse
if checkbox is clicked for given position. Execute this function in your main event loop and passmouse_pos
to check if user has clicked the checkbox.pos
has to be a tuple like(X, Y)
. It will also execute a functionclick_function=
that you have passed when creating a checkbox. Check if it returnsTrue
and then do whatever you want to do after user has checked/unchecked a checkbox. NOTE: If your checkbox respond to clicks outside of its box there is a big chance that you have passed wrong(PARENT_X, PARENT_Y)
ormouse_pos
and it is detecting click in a wrong place
Properties:
checked
-True
if a checkbox is checked andFalse
if not, defaultFalse
size
- Size of a checkbox(WIDTH, HEIGHT)
position
- Position of a checkbox(X, Y)
text
- Text
You can simply create a slider like so
Slider((X, Y, WIDTH, HEIGHT), (PARENT_X, PARENT_Y), MIN_VALUE, MAX_VALUE, JUMP, DEFAULT_VALUE, SLIDER_COLOR, BAR_COLOR, SLIDER_RADIUS, ALPHA, TEXT)
and then to draw it Slider.draw(SURFACE)
.
Obligator initial variables:
(X, Y, WIDTH, HEIGHT)
- also called asplacement
in code, a tuple representing slider position and size. NOTE: Given cordinates are relative to the surface that you are drawing it on. If you pass a surface and then blit this surface onto the main screen it will be drawn on cordinates on the first surface not the main one, ex. if you set slider position to (10, 0, W, H) draw it on a surface and then blit this surface onto a main screen at (100, 100), slider absulute position will be (110, 100)(PARENT_X, PARENT_Y)
- a tuple(X, Y)
, because my library support adding subobjects, to properly determine if user has clicked on an object you have to pass ojects parent absolute postion, if you are drawing the slider on the main screen just pass(0, 0)
, otherwise you have to pass the absolute position of the parent. Considering previous example you would pass(10, 0)
MIN_VALUE
- Min value of a sliderMAX_VALUE
- Max value of a sliderJUMP
- Value that you want your slider to increment byDEFAULT_VALUE
- Starting value of your sliderSLIDER_COLOR
- Color of a circle that indicates sliderBAR_COLOR
- Main bar colorSLIDER_RADIUS
- Radius of a circle that indicates sliderALPHA
- Transparency of the slider, 255 is completly drawn and 0 is invisibleTEXT
- Text that will be shown bellow a slider, you can always chenge it laterslider.text=""
. It will be rendered asYOUR_TEXT: VALUE
Curently there is no validation for default/min/max value and jump, you have to make sure that the values that you want to increment by have at least one pixel, ex. if you create slider with width=20 and set its max value for 100, jump to 1 and min value to 0 it won't work as you expect it to because there are 100 possible values ((max_value-min_value)/jump)
and only 20 horizontal pixels. In order to make this example work you have to set jump as 5
Optional arguments:
spacing=
- How many pixels to the bottom a label will be rednered, default is10
font=
- If you want to choose different font, default ismonospace
font_size=
- Font size, default is10
font_color=
- Font color, default is blackclick_function=
If you want to execute a simple function after a slider is moved, although it is not recommended for complex functions, I will show a better way of handling slider clicks.
Methods:
draw(surface)
- Execute it when drawing a slider, it will be drawn on passed surfaceclicked(pos)
- ReturnsTrue
orFalse
if slider is clicked for given position. Execute this function in your main event loop and passmouse_pos
to check if user has clicked the slider.pos
has to be a tuple like(X, Y)
. It will also execute a functionclick_function=
that you have passed when creating a slider. Check if it returnsTrue
and then do whatever you want to do after user has moved a slider. NOTE: If your slider respond to clicks outside of its box there is a big chance that you have passed wrong(PARENT_X, PARENT_Y)
ormouse_pos
and it is detecting click in a wrong place
Properties:
value
- Value of a slidersize
- Size of a slider(WIDTH, HEIGHT)
position
- Position of a slider(X, Y)
text
- Text, it will be rendered asYOUR_TEXT: VALUE
If you wnat to get smoth slide effect instead of clicking a slider you have to keep track of pressed buttons and call Slider.clicked()
function every tick. For my recommended approach look at slider_example.py
It is invisible surface on which you can draw UI elements instead of main screen. When user resize a window all you have to do is reposition placeholder and all buttons will stay in the same position.
To create: Placeholder((X, Y, WIDTH, HEIGHT))
Obligatory initial arguments:
(X, Y, WIDTH, HEIGHT)
- also called asplacement
in code, a tuple representing slider position and size.
Methods:
add_sub_object(obj)
- Adding an UI object to a placeholderdelete_sub_object(obj)
- Deleting an UI object from a placeholderdraw(surface)
- Drawing placeholders and all its sub objects on a surface
Properties:
size
- Size of a slider(WIDTH, HEIGHT)
position
- Position of a slider(X, Y)
Can act like a placeholder or be added as a separate object to existing placeholder. Basically its a placeholder with a color and transparency.
To create: Background((X, Y, WIDTH, HEIGHT), COLOR, ALPHA)
Obligatory initial arguments:
(X, Y, WIDTH, HEIGHT)
- also called asplacement
in code, a tuple representing slider position and size.COLOR
- Color of a background in RGB standard, you can also use myColors()
class instead of typing it by yourselfALPHA
- Transparency of the background color, 255 is completly drawn and 0 is invisible. NOTE: It doens't apply to sub objects
- Better errors handling
- Not allowing setting wrong value/jump/min_value/max_value in slider
- Auto positiong sub objects for placeholders
- Aligments left/right/top/bottom
- Horizontal/Vertical alingment
- Auto detecting absolute postion for clickable elements
- More label-rendering options for slider, checkboxes
- Easier and more intuitive functions
- Cleaner objects initialization
- More default generated things like better default font size depending on the size of an object, slider radius etc.
- More colors
- More components
- Labels
- Textboxes
- Dropdown menus