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
|
# -*- coding: utf-8 -*-
"""
Manages time bar : a kind of reversed progress bar.
Created on Fri Sep 20 23:19:47 2013
@author: laurent-bernabe
"""
import pygame
from pygame.locals import USEREVENT
class TimeBar(object):
"""
A kind of reversed progress bar.
"""
def __init__(self, width, height, active_color, dead_color,
lftp_edge=(0, 0)):
"""
Constructor
width : width of the bar (when it has its maximum value) => integer
height : height of the bar => integer
active_color : color for active part => Tuple of 3 integers in [0,255]
dead_color : color for the dead part => Tuple of 3 integers in [0,255]
lftp_edge : coordinate of the left-top edge => tuple of two integers
"""
self._width = width
self._height = height
self._lftp_edge = lftp_edge
self._active_color = active_color
self._dead_color = dead_color
self._is_empty = False
def start(self, max_value, step):
"""
Starts a new 'session'. Please, notice that time between two stages
(or ticks) is 10 milliseconds (so it will take max_value * step * 10
milleseconds for the bar to be empty), and that you must call
decrease() method on USEREVENT+1 event of pygame.
max_value : the start value (don't forget that it is a reversed
progress bar) => integer
step : how many units will be removed at each stage => integer
"""
self._value = max_value
self._max_value = max_value
self._step = step
self._is_empty = False
pygame.time.set_timer(USEREVENT + 1, 10)
def decrease(self):
"""
Decreases the value of the time bar, and stops its falling if there
is no more value unit.
"""
self._value -= self._step
if self._value <= 0:
self._is_empty = True
pygame.time.set_timer(USEREVENT + 1, 0)
def get_width(self):
"""
Accessor to the width (width when it has its maximum value)
=> integer
"""
return self._width
def get_height(self):
"""
Accessor to the height
=> integer
"""
return self._height
def get_edge(self):
"""
Accessor to the left-top edge.
=> tuple of two integers
"""
return self._lftp_edge
def get_active_color(self):
"""
Accessor to the active part (not elapsed) color.
=> Tuple of 3 integers.
"""
return self._active_color
def get_dead_color(self):
"""
Accessor to the dead part (elapsed) color.
=> Tuple of 3 integers
"""
return self._dead_color
def get_value(self):
"""
Accessor to the value.
Be careful ! May raise NameError, as value may not be defined yet.
=> integer
"""
try:
return self._value
except AttributeError:
raise NameError
def get_max_value(self):
"""
Accessor to the max value.
Be careful ! May raise NameError, as max value may not be defined yet.
=> integer
"""
try:
return self._max_value
except AttributeError:
raise NameError
def is_empty(self):
"""
Says whether the bar is empty.
=> boolean
"""
return self._is_empty
|