forked from MyreMylar/pygame_gui_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
status_bars.py
155 lines (119 loc) · 5.47 KB
/
status_bars.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import pygame
import pygame_gui
from pygame_gui.core import ObjectID
class HappySprite(pygame.sprite.Sprite):
def __init__(self, *groups: pygame.sprite.AbstractGroup):
super().__init__(*groups)
self.image = pygame.image.load('data/images/test_emoji.png')
self.position = pygame.Vector2(200.0, 300.0)
self.rect = self.image.get_rect()
self.rect.topleft = (200, 300)
self.max_health = 100
self.current_health = 75
self.max_mana = 100
self.current_mana = 30
self.max_stamina = 100.0
self.current_stamina = 100.0
self.speed = 100.0
self.moving_left = False
self.moving_right = False
self.moving_up = False
self.moving_down = False
self.stam_recharge_tick = 0.05
self.stam_recharge_acc = 0.0
def get_health_percentage(self) -> float:
return self.current_health/self.max_health
def get_mana_percentage(self) -> float:
return self.current_mana/self.max_mana
def get_stamina_percentage(self) -> float:
return self.current_stamina/self.max_stamina
def update(self, time_delta_secs: float) -> None:
if self.moving_left:
self.position.x -= self.speed * time_delta_secs
self.current_stamina -= 0.4
if self.moving_right:
self.position.x += self.speed * time_delta_secs
self.current_stamina -= 0.4
if self.moving_up:
self.position.y -= self.speed * time_delta_secs
self.current_stamina -= 0.4
if self.moving_down:
self.position.y += self.speed * time_delta_secs
self.current_stamina -= 0.4
self.current_stamina = max(self.current_stamina, 0)
if self.current_stamina < self.max_stamina:
self.stam_recharge_acc += time_delta_secs
if self.stam_recharge_acc >= self.stam_recharge_tick:
self.current_stamina += 1
self.stam_recharge_acc = 0.0
self.current_stamina = min(self.current_stamina, self.max_stamina)
self.rect.topleft = (int(self.position.x), int(self.position.y))
pygame.init()
pygame.display.set_caption('Status Bars')
window_surface = pygame.display.set_mode((800, 600))
manager = pygame_gui.UIManager((800, 600), 'data/themes/status_bar_theme.json')
background = pygame.Surface((800, 600))
background.fill(manager.ui_theme.get_colour('dark_bg'))
sprite_list = pygame.sprite.Group()
happy_sprite = HappySprite(sprite_list)
progress_bar = pygame_gui.elements.UIStatusBar(pygame.Rect((100, 100), (200, 30)),
manager,
None,
object_id=ObjectID('#progress_bar'))
health_bar = pygame_gui.elements.UIStatusBar(pygame.Rect((0, 30), (50, 6)),
manager,
sprite=happy_sprite,
percent_method=happy_sprite.get_health_percentage,
object_id=ObjectID(
'#health_bar', '@player_status_bars'))
mana_bar = pygame_gui.elements.UIStatusBar(pygame.Rect((0, 40), (50, 6)),
manager,
sprite=happy_sprite,
percent_method=happy_sprite.get_mana_percentage,
object_id=ObjectID(
'#mana_bar', '@player_status_bars'))
stamina_bar = pygame_gui.elements.UIStatusBar(pygame.Rect((0, 50), (50, 6)),
manager,
sprite=happy_sprite,
percent_method=happy_sprite.get_stamina_percentage,
object_id=ObjectID(
'#stamina_bar', '@player_status_bars'))
progress = 0
time_acc = 0
clock = pygame.time.Clock()
is_running = True
while is_running:
time_delta = clock.tick(60)/1000.0
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
happy_sprite.moving_left = True
if event.key == pygame.K_RIGHT:
happy_sprite.moving_right = True
if event.key == pygame.K_UP:
happy_sprite.moving_up = True
if event.key == pygame.K_DOWN:
happy_sprite.moving_down = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
happy_sprite.moving_left = False
if event.key == pygame.K_RIGHT:
happy_sprite.moving_right = False
if event.key == pygame.K_UP:
happy_sprite.moving_up = False
if event.key == pygame.K_DOWN:
happy_sprite.moving_down = False
manager.process_events(event)
sprite_list.update(time_delta)
manager.update(time_delta)
time_acc += time_delta
progress = (time_acc/10.0)
if progress > 1.0:
time_acc = 0.0
progress_bar.percent_full = progress
window_surface.blit(background, (0, 0))
sprite_list.draw(window_surface)
manager.draw_ui(window_surface)
pygame.display.update()