|
| 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