Skip to content

Commit 042a1cb

Browse files
committed
Fixed some more docs and added some examples
1 parent e831c08 commit 042a1cb

File tree

10 files changed

+590
-32
lines changed

10 files changed

+590
-32
lines changed
-9 Bytes
Binary file not shown.

assets/gifs/add_pygameui.gif

116 KB
Loading

assets/imgs/avatar.jpeg

178 KB
Loading

docs/components/button.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,3 @@ centered: bool = False
5151
- `font_size`: Size of the text font
5252
- `font_family`: Font family for the text
5353
- `centered`: If True, the button is centered on the provided position
54-
55-
### Button States
56-
- Normal: Default appearance
57-
- Hover: When mouse is over the button
58-
- Click: When button is being pressed

docs/getting-started.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Getting Started with PygameUI
22

33
## Installation
4+
45
Ensure you have Python and PyGame installed.
56

67
Start by downloading the PyGameUI python file from the [releases page](https://github.com/trymbf/pygameui/releases).
@@ -33,9 +34,36 @@ while running:
3334
if event.type == pygame.QUIT:
3435
running = False
3536

36-
# Update and draw
37+
# Reset screen
3738
screen.fill(background_color)
39+
40+
# Update element, moves, checks actions etc
3841
ui_element.update()
42+
43+
# Draw element on screen
3944
ui_element.draw(screen)
45+
46+
# Update pygame display
4047
pygame.display.flip()
4148
```
49+
50+
To add more UI elements, simply create more instances of the `Element` class or its subclasses (like `Button`, `Text`, etc.) and follow the same pattern.
51+
52+
## Example create text
53+
First create a text object:
54+
```python
55+
my_text = pygameui.Text(
56+
text="Hello World",
57+
position=(100, 100),
58+
font_size=30,
59+
color=(255, 255, 255)
60+
)
61+
```
62+
63+
And then in the main loop, you would call:
64+
```python
65+
my_text.update()
66+
my_text.draw(screen)
67+
```
68+
69+
And that's it! You now have a text object on your screen. You can customize the text by changing its properties like `font_size`, `color`, and `position`. [See the Text class documentation for more details.](components/text.md)

examples/dashboard.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""
2+
Dashboard UI using pygameui
3+
This code creates a simple dashboard UI using the pygameui library.
4+
It includes a user profile section, a main content area with statistics,
5+
and a notification area.
6+
"""
7+
8+
import pygame
9+
import pygameui
10+
11+
pygame.init()
12+
screen = pygame.display.set_mode((800, 600))
13+
pygame.display.set_caption("PyGameUI Dashboard")
14+
15+
# Define colors
16+
DARK_BG = (30, 30, 40)
17+
LIGHT_BG = (45, 45, 60)
18+
ACCENT = (86, 140, 235)
19+
ACCENT_HOVER = (120, 170, 255)
20+
TEXT_COLOR = (240, 240, 245)
21+
ERROR_COLOR = (235, 86, 86)
22+
SUCCESS_COLOR = (86, 235, 120)
23+
24+
# Create UI elements
25+
header = pygameui.Text((400, 50), "Dashboard", TEXT_COLOR, font_size=40, centered=True)
26+
27+
# User profile section - using Element instead of Panel which doesn't exist
28+
profile_bg = pygameui.Element((20, 100), 250, 480, LIGHT_BG, border_radius=10)
29+
# Use proper Image parameters according to implementation
30+
try:
31+
avatar = pygameui.Image((145, 180), "assets/imgs/avatar.jpeg",
32+
width=150, height=150, centered=True)
33+
except Exception as e:
34+
# Fallback if image can't be loaded
35+
pass
36+
37+
username_display = pygameui.Text((145, 280), "trymbf", TEXT_COLOR, font_size=24, centered=True)
38+
status = pygameui.Text((145, 310), "Online", SUCCESS_COLOR, font_size=18, centered=True)
39+
logout_btn = pygameui.Button((145, 530), 200, 40, 4, "Logout", ACCENT, ACCENT_HOVER, text_color=TEXT_COLOR, centered=True)
40+
41+
# Main content area - using Element instead of Panel
42+
content_bg = pygameui.Element((290, 100), 490, 480, LIGHT_BG, border_radius=10)
43+
welcome_text = pygameui.Text((535, 140), "Welcome back!", TEXT_COLOR, font_size=28, centered=True)
44+
45+
# Stats section - remove ProgressBar and use Element instead
46+
stats_title = pygameui.Text((535, 200), "Your Statistics", TEXT_COLOR, font_size=22, centered=True)
47+
stat1_bg = pygameui.Element((535, 250), 400, 30, (100, 100, 120), centered=True)
48+
stat1_fill = pygameui.Element((535 - (400-300)/2, 250), 300, 30, ACCENT, centered=True) # 75% filled
49+
stat1_label = pygameui.Text((535, 280), "Projects Completed: 75%", TEXT_COLOR, font_size=16, centered=True)
50+
stat2_bg = pygameui.Element((535, 330), 400, 30, (100, 100, 120), centered=True)
51+
stat2_fill = pygameui.Element((535 - (400-160)/2, 330), 160, 30, ACCENT, centered=True) # 40% filled
52+
stat2_label = pygameui.Text((535, 360), "Tasks Finished: 40%", TEXT_COLOR, font_size=16, centered=True)
53+
54+
# Notification area
55+
notif_title = pygameui.Text((535, 410), "Recent Notifications", TEXT_COLOR, font_size=22, centered=True)
56+
notif1 = pygameui.Text((535, 450), "New message from Alex", TEXT_COLOR, font_size=16, centered=True)
57+
notif2 = pygameui.Text((535, 480), "Project deadline tomorrow", ERROR_COLOR, font_size=16, centered=True)
58+
clear_notif_btn = pygameui.Button((535, 530), 200, 40, 4, "Clear All", ACCENT, ACCENT_HOVER, text_color=TEXT_COLOR, centered=True)
59+
60+
# Put all elements in a list for easy updating and drawing
61+
elements = [
62+
header, profile_bg, username_display, status, logout_btn,
63+
content_bg, welcome_text, stats_title, stat1_bg, stat1_fill, stat1_label,
64+
stat2_bg, stat2_fill, stat2_label, notif_title, notif1, notif2, clear_notif_btn
65+
]
66+
67+
# Try to add avatar if it was loaded
68+
try:
69+
if avatar:
70+
elements.append(avatar)
71+
except:
72+
pass
73+
74+
run = True
75+
while run:
76+
events = pygame.event.get()
77+
78+
# Handle events
79+
for event in events:
80+
if event.type == pygame.QUIT:
81+
run = False
82+
83+
if logout_btn.was_clicked():
84+
status.change_text("Logging out...")
85+
status.change_text_color(ERROR_COLOR)
86+
87+
if clear_notif_btn.was_clicked():
88+
notif1.change_text("No new notifications")
89+
notif2.change_text("")
90+
91+
# Update elements - the update method doesn't take parameters
92+
for element in elements:
93+
element.update()
94+
95+
# Draw everything
96+
screen.fill(DARK_BG)
97+
98+
for element in elements:
99+
element.draw(screen)
100+
101+
# Update screen
102+
pygame.display.flip()

examples/game_ui.py

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
import pygame
2+
import sys
3+
import pygameui
4+
import random
5+
6+
# Initialize pygame
7+
pygame.init()
8+
screen = pygame.display.set_mode((800, 600))
9+
pygame.display.set_caption("PyGameUI Game Interface Example")
10+
11+
# Define colors
12+
BG_COLOR = (15, 20, 30)
13+
PANEL_COLOR = (30, 35, 45)
14+
DARK_PANEL = (25, 25, 35)
15+
ACCENT_COLOR = (86, 140, 235)
16+
ACCENT_HOVER = (120, 170, 255)
17+
TEXT_COLOR = (230, 230, 240)
18+
HEALTH_COLOR = (235, 86, 86)
19+
MANA_COLOR = (86, 110, 235)
20+
XP_COLOR = (86, 235, 120)
21+
GOLD_COLOR = (235, 200, 86)
22+
23+
# Game state
24+
health = 75
25+
max_health = 100
26+
mana = 50
27+
max_mana = 100
28+
experience = 35
29+
max_experience = 100
30+
gold = 1250
31+
player_name = "Hero"
32+
level = 5
33+
34+
# Create UI elements
35+
# Main game panel (simulating game window)
36+
game_panel = pygameui.Element((400, 250), 780, 390, DARK_PANEL, border_radius=10, centered=True)
37+
game_title = pygameui.Text((400, 250), "GAME AREA", (100, 100, 130), font_size=72, centered=True)
38+
39+
# Top bar - player info
40+
top_panel = pygameui.Element((400, 20), 780, 50, PANEL_COLOR, border_radius=8, centered=True)
41+
player_name_text = pygameui.Text((70, 20), player_name, TEXT_COLOR, font_size=22, centered=True)
42+
level_text = pygameui.Text((140, 20), f"Level {level}", GOLD_COLOR, font_size=20, centered=True)
43+
gold_text = pygameui.Text((700, 20), f"Gold: {gold}", GOLD_COLOR, font_size=20, centered=True)
44+
45+
# Bottom bar - health, mana, experience
46+
bottom_panel = pygameui.Element((400, 570), 780, 70, PANEL_COLOR, border_radius=8, centered=True)
47+
48+
# Health bar
49+
health_label = pygameui.Text((70, 550), "HP", TEXT_COLOR, font_size=18, centered=True)
50+
health_bg = pygameui.Element((200, 550), 200, 20, (60, 40, 40), centered=True)
51+
health_bar = pygameui.Element((200 - (200-(health/max_health*200))/2, 550), health/max_health*200, 20, HEALTH_COLOR, centered=True)
52+
health_text = pygameui.Text((200, 570), f"{health}/{max_health}", TEXT_COLOR, font_size=14, centered=True)
53+
54+
# Mana bar
55+
mana_label = pygameui.Text((350, 550), "MP", TEXT_COLOR, font_size=18, centered=True)
56+
mana_bg = pygameui.Element((480, 550), 200, 20, (40, 40, 60), centered=True)
57+
mana_bar = pygameui.Element((480 - (200-(mana/max_mana*200))/2, 550), mana/max_mana*200, 20, MANA_COLOR, centered=True)
58+
mana_text = pygameui.Text((480, 570), f"{mana}/{max_mana}", TEXT_COLOR, font_size=14, centered=True)
59+
60+
# Experience bar
61+
xp_bg = pygameui.Element((400, 595), 600, 15, (40, 60, 40), centered=True)
62+
xp_bar = pygameui.Element((400 - (600-(experience/max_experience*600))/2, 595), experience/max_experience*600, 15, XP_COLOR, centered=True)
63+
xp_text = pygameui.Text((730, 595), f"XP: {experience}/{max_experience}", TEXT_COLOR, font_size=14, centered=True)
64+
65+
# Action buttons
66+
attack_btn = pygameui.Button((690, 550), 120, 40, 5, "Attack", HEALTH_COLOR, (255, 100, 100), text_color=TEXT_COLOR, centered=True)
67+
spell_btn = pygameui.Button((690, 500), 120, 40, 5, "Spell", MANA_COLOR, (100, 140, 255), text_color=TEXT_COLOR, centered=True)
68+
item_btn = pygameui.Button((560, 550), 120, 40, 5, "Item", GOLD_COLOR, (255, 220, 100), text_color=TEXT_COLOR, centered=True)
69+
run_btn = pygameui.Button((560, 500), 120, 40, 5, "Run", (150, 150, 150), (200, 200, 200), text_color=TEXT_COLOR, centered=True)
70+
71+
# Action log
72+
log_panel = pygameui.Element((110, 500), 200, 140, DARK_PANEL, border_radius=5, centered=True)
73+
log_title = pygameui.Text((110, 445), "Combat Log", TEXT_COLOR, font_size=16, centered=True)
74+
log_entry1 = pygameui.Text((110, 470), "Battle started", TEXT_COLOR, font_size=12, centered=True)
75+
log_entry2 = pygameui.Text((110, 490), "Enemy appeared", TEXT_COLOR, font_size=12, centered=True)
76+
log_entry3 = pygameui.Text((110, 510), "Waiting for action...", TEXT_COLOR, font_size=12, centered=True)
77+
78+
# Group all elements
79+
elements = [
80+
game_panel, game_title,
81+
top_panel, player_name_text, level_text, gold_text,
82+
bottom_panel,
83+
health_label, health_bg, health_bar, health_text,
84+
mana_label, mana_bg, mana_bar, mana_text,
85+
xp_bg, xp_bar, xp_text,
86+
attack_btn, spell_btn, item_btn, run_btn,
87+
log_panel, log_title, log_entry1, log_entry2, log_entry3
88+
]
89+
90+
# Action functions
91+
def perform_attack():
92+
global health, mana, gold, experience
93+
94+
# Update combat log
95+
log_entry3.change_text(log_entry2.content)
96+
log_entry2.change_text(log_entry1.content)
97+
log_entry1.change_text("You attack! Damage dealt!")
98+
99+
# Simulate battle effects
100+
health -= random.randint(5, 15)
101+
health = max(0, health)
102+
mana += random.randint(5, 10)
103+
mana = min(mana, max_mana)
104+
experience += random.randint(3, 8)
105+
gold += random.randint(10, 30)
106+
107+
# Update UI
108+
update_stats()
109+
110+
def cast_spell():
111+
global health, mana, gold, experience
112+
113+
if mana < 15:
114+
log_entry3.change_text(log_entry2.content)
115+
log_entry2.change_text(log_entry1.content)
116+
log_entry1.change_text("Not enough mana!")
117+
return
118+
119+
# Update combat log
120+
log_entry3.change_text(log_entry2.content)
121+
log_entry2.change_text(log_entry1.content)
122+
log_entry1.change_text("You cast a spell! Critical hit!")
123+
124+
# Simulate battle effects
125+
health += random.randint(5, 15)
126+
health = min(health, max_health)
127+
mana -= random.randint(10, 20)
128+
mana = max(0, mana)
129+
experience += random.randint(5, 12)
130+
gold += random.randint(15, 40)
131+
132+
# Update UI
133+
update_stats()
134+
135+
def use_item():
136+
global health, mana, gold
137+
138+
if gold < 50:
139+
log_entry3.change_text(log_entry2.content)
140+
log_entry2.change_text(log_entry1.content)
141+
log_entry1.change_text("Not enough gold!")
142+
return
143+
144+
# Update combat log
145+
log_entry3.change_text(log_entry2.content)
146+
log_entry2.change_text(log_entry1.content)
147+
log_entry1.change_text("You used a health potion!")
148+
149+
# Simulate item use
150+
health += random.randint(20, 35)
151+
health = min(health, max_health)
152+
gold -= 50
153+
154+
# Update UI
155+
update_stats()
156+
157+
def attempt_run():
158+
# Update combat log
159+
log_entry3.change_text(log_entry2.content)
160+
log_entry2.change_text(log_entry1.content)
161+
162+
if random.random() > 0.5:
163+
log_entry1.change_text("You escaped successfully!")
164+
else:
165+
log_entry1.change_text("Failed to escape!")
166+
# Take damage for trying to run
167+
global health
168+
health -= random.randint(10, 20)
169+
health = max(0, health)
170+
update_stats()
171+
172+
def update_stats():
173+
# Update text elements
174+
health_text.change_text(f"{health}/{max_health}")
175+
mana_text.change_text(f"{mana}/{max_mana}")
176+
xp_text.change_text(f"XP: {experience}/{max_experience}")
177+
gold_text.change_text(f"Gold: {gold}")
178+
179+
# Update bar elements - adjust width and position
180+
health_bar.rect.width = (health/max_health) * 200
181+
health_bar.rect.centerx = 200 - (200-(health/max_health*200))/2
182+
183+
mana_bar.rect.width = (mana/max_mana) * 200
184+
mana_bar.rect.centerx = 480 - (200-(mana/max_mana*200))/2
185+
186+
xp_bar.rect.width = (experience/max_experience) * 600
187+
xp_bar.rect.centerx = 400 - (600-(experience/max_experience*600))/2
188+
189+
# Main game loop
190+
running = True
191+
while running:
192+
events = pygame.event.get()
193+
194+
# Handle events
195+
for event in events:
196+
if event.type == pygame.QUIT:
197+
running = False
198+
199+
# Handle button clicks
200+
if event.type == pygame.MOUSEBUTTONDOWN:
201+
if attack_btn.is_hovered():
202+
perform_attack()
203+
elif spell_btn.is_hovered():
204+
cast_spell()
205+
elif item_btn.is_hovered():
206+
use_item()
207+
elif run_btn.is_hovered():
208+
attempt_run()
209+
210+
# Update all elements
211+
for element in elements:
212+
element.update()
213+
214+
# Draw everything
215+
screen.fill(BG_COLOR)
216+
for element in elements:
217+
element.draw(screen)
218+
219+
# Update display
220+
pygame.display.flip()
221+
222+
pygame.quit()
223+
sys.exit()

0 commit comments

Comments
 (0)