With PyGameUI, all of this can be created with just a few lines of code!
Back to topStart by downloading the PyGameUI python file:
Click me to download PyGameUIThan place the PyGameUI file in the same folder as your project files.
Your project directory should now look like this:
project_folder/
└───pygameui.py
└───main.py
To use PyGameUI in a project, the projects needs to have at least a basic pygame script with PyGameUI imported as any other library:
import pygame, sys
import pygameui
pygame.init()
win = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
while True:
win.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.flip()
clock.tick(60)
With this done you are ready to get started using PyGameUI!
Adding a text element only requires 1 line of code:
The two arguments are position (x, y) and content
example_text = pygameui.Text((200, 200), "My cool text")
Drawing the text only requires 1 line of code too:
example_text.draw(win)
Final code:
import pygame, sys, pygameui
pygame.init()
win = pygame.display.set_mode((500, 500))
# Create text element
example_text = pygameui.Text((200, 200), "My cool text")
clock = pygame.time.Clock()
while True:
win.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Draw text
example_text.draw(win)
pygame.display.flip()
clock.tick(60)
Result:
In addition to the required parameters, we can pass in optional ones. This allows even more customizing!
The text element has so far four optional parameters: font, fontSize, color and centerMode.
Argument | Explanation | Default |
---|---|---|
color | Sets the color of the text | (255, 255, 255) |
font_name | Sets the font for the text | freesansbold |
font_size | Sets the size of the font (To see available fonts use 'pygame.font.get_fonts()') | 20 |
center_mode | If True the textrect's center is at the provided position, if not is the textrect's topleft used | True |
To edit an optional argument just add another parameter when creating the element.
Example:
# Adding one parameter
example_text = pygameui.Text((200, 200), "My cool text", (255, 255, 200), fontName = "elephant")
# Adding multiple parameters
example_text_2 = pygameui.Text((200, 200), "My cool not centermoded text", (255, 255, 100), fontSize = 50, centerMode = False)
Final code:
import pygame, sys, pygameui
pygame.init()
win = pygame.display.set_mode((500, 500))
# Adding one parameter
example_text = pygameui.Text((200, 200), "My cool text", (255, 255, 200), font_name = "elephant")
# Adding multiple parameters
example_text_2 = pygameui.Text((200, 200), "My cool not centermoded text", (255, 255, 100), font_size = 50, center_mode = False)
clock = pygame.time.Clock()
while True:
win.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
example_text.draw(win)
example_text_2.draw(win)
pygame.display.flip()
clock.tick(60)
Result:
Notice how the both text elements have the same position but still are placed at different locations.
This is because the larger one has centerMode set to false.
A general element can be anything from just a decorative rectangle to an image or button.
They are created in the same way as text elements but only requires a position:
example_element = pygameui.Element((300, 300))
General elements can also contain text or images:
# Creating an element with text
element_with_text = pygameui.Element((300, 300), "My cool element")
# Creating an element with an image
my_amaizing_image = pygame.image.load("my_amaizing_image.png")
element_with_image = pygameui.Element((300, 300), content=my_amaizing_image)
General elements are drawn in the same way as text elements:
example_element.draw(win)
Final code:
import pygame, sys, pygameui
pygame.init()
win = pygame.display.set_mode((500, 500))
example_element = pygameui.Element((300, 300))
clock = pygame.time.Clock()
while True:
win.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
example_element.draw(win)
pygame.display.flip()
clock.tick(60)
Result:
General elements are customized in the same way as text elements, but have a lot more posible parameters.
Argument | Explanation | Default |
---|---|---|
content | What the element should contain. Can either be a pygame image, a string or left as nothing. | None |
center_mode | If True the elements center is placed on the provided position, if not the elements topleft is placed on the provided position. | True |
center_text | Same as centerMode but only for the potensial text. | True |
font_name | Sets the font for the potensial text. (To see available fonts use 'pygame.font.get_fonts()') | freesansbold.ttf |
font_size | Sets the font size for the potensial text. | 50 |
text_color | Sets the text color for the potensial text. | (231, 111, 81) |
rect_width | Sets the width of the rectangle or image. | 200 |
rect_height | Sets the height for the rectangle or image. | 75 |
rect_color | Sets a color for the rect. | (233, 196, 106) |
rect_border_radius | How round the rects corners should be. The higher the number, the rounder the corner. | 10 |
To add an input element just follow the same process as the other elements:
The only required argument is the position.
example_input = pygameui.Input((200, 200))
Input elements are drawn in the same way as any other element:
example_input.draw(win)
Final code:
import pygame, sys
import pygameui
pygame.init()
win = pygame.display.set_mode((500, 500))
# Creating an input
example_input = pygameui.Input((200, 200))
clock = pygame.time.Clock()
while True:
win.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Drawing the input
example_input.draw(win)
# Updating the display
pygame.display.flip()
clock.tick(60)
Result:
As you probatly have notices our input element looks nice and all that, but lacks any functionality...
We first have to store the events from the pygame.event.get(). To do this swap this code:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
With this code:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
Now we can add functionality to the input element. To do this we need to add a few lines of code.
First we have to create a list of the clickable elements in our project. This list tells PyGameUI when
it should change the cursor to a hand. (Indicating that the element is clickable)
Right now our project only includes one clickable element so our list can be created like this:
clickable_elements = [example_input]
Now we can add the functionality to the input element. This is done by just adding a line of code to the main loop:
example_input.work(events, clickable_elements)
Final code:
import pygame, sys
import pygameui
pygame.init()
win = pygame.display.set_mode((500, 500))
# Creating an input
example_input = pygameui.Input((200, 200))
# Creating a list of all clickable elements
clickable_elements = [example_input]
clock = pygame.time.Clock()
while True:
win.fill((0, 0, 0))
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Give the input funcionality
example_input.work(events, clickable_elements)
# Drawing the input
example_input.draw(win)
# Updating the display
pygame.display.flip()
clock.tick(60)
Result:
Now just click on the input to activate it, write to your heart's content, and click the input or outside of it to deactivate.
Now that we have a working input element, we can obtain the text from it. This is done by calling the getValue() method on the input element.
input_text = example_input.getValue()
print(input_text)
Final code:
import pygame, sys
import pygameui
pygame.init()
win = pygame.display.set_mode((500, 500))
# Creating an input
example_input = pygameui.Input((200, 200))
# Creating a list of all clickable elements
clickable_elements = [example_input]
clock = pygame.time.Clock()
while True:
win.fill((0, 0, 0))
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Give the input funcionality
example_input.work(events, clickable_elements)
# Obtaining the input value and printing it
input_text = example_input.getValue()
print("You have written: " + input_text)
# Drawing the input
example_input.draw(win)
# Updating the display
pygame.display.flip()
clock.tick(60)
Input elements are customized in the same way as the other elements.
Argument | Explanation | Default |
---|---|---|
font_name | The font name to be used for the text in the input element. (To see available fonts use 'pygame.font.get_fonts()') | "freesansbold.ttf" |
font_size | The font size to be used for the text in the input element. | 30 |
example_content | The example content to be displayed in the input element when empty and not active. | "Click me to input!" |
prefilled_content | The prefilled content to be displayed in the input element. | "" |
character_limit | The maximum number of characters allowed in the input element. | 100 |
normal_text_color | The text color for the input element when it is not active. | (231, 111, 81) |
example_text_color | The text color for the example content in the input element. | (100, 100, 100) |
rect_width | The width of the rectangular area of the input element. | 200 |
rect_height | The height of the rectangular area of the input element. | 50 |
rect_color_active | The color of the input elements rectangular border when it is active. | (233, 196, 106) |
rect_color_passive | The color of the input elements rectangular border when it is not active. | (200, 200, 200) |
rect_border_radius | The border radius of the rectangular area of the input element. | 1 |
rect_border_width | The border width of the rectangular area of the input element. | 5 |
center_mode | If True the elements center is placed on the provided position, if not the elements topleft is placed on the provided position. | True |
Now that you have learned the basics of PyGameUI, you can start giving your project even more functionality or bling!
(Recommended) Learn how to create buttons and give them functionality → (Advanced) Learn how to bring your UI to life by automaticly moving elements around →