-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgauge.py
92 lines (74 loc) · 2.98 KB
/
gauge.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
""" Gauge panel """
import pygame
import random
from enum import Enum
class Gague_Type(Enum):
UP = 1
DOWN = 2
LEFT = 3
RIGHT = 4
RADIAL = 5
class Gauge:
"""A gauges"""
def __init__(self, x_pos, y_pos, width, height, max_value, type: Gague_Type, displaySurface):
self.rendering = True
self._display_surf = displaySurface
self.x_pos = x_pos
self.y_pos = y_pos
self.width = width
self.height = height
self.bar_width = self.width - (self.width / 10)
self.bar_max_height = self.height - 10
self.bar_x_pos = (self.width - self.bar_width) / 2
self.bar_y_pos = (self.height - self.bar_max_height) / 2
self.max_value = max_value
self.current_value = 0
self.start_value = 0
self.target_value = self.current_value
self.move_time = 2
self.type = type
self.now = 0
def on_loop(self, update_time):
""" Update loop """
self.now += update_time
if self.current_value == self.target_value:
self.target_reached()
else:
if self.now > self.move_time:
self.target_reached()
else:
time_offset = self.now / self.move_time
self.current_value = self.start_value + ((self.target_value - self.start_value) * time_offset)
def target_reached(self):
self.now = 0
self.current_value = self.target_value
self.start_value = self.target_value
self.target_value = random.randrange(0, self.max_value)
def on_render(self):
""" Render event """
background = pygame.Surface((self.width, self.height), pygame.HWSURFACE)
background.fill((128, 128, 128))
height = self.bar_max_height * (self.current_value / self.max_value)
if self.type == Gague_Type.RADIAL:
pygame.draw.arc(background, (20, 20, 20), (self.bar_x_pos, self.bar_y_pos, self.bar_width, self.bar_max_height), 0.0, 1.0, int(self.bar_width/10))
else :
bar = pygame.Surface((self.bar_width, height), pygame.HWSURFACE)
bar.fill((20, 20, 20))
background.blit(bar, (self.bar_x_pos, self.bar_y_pos))
match self.type:
case Gague_Type.UP:
background = pygame.transform.rotate(background, 180)
case Gague_Type.DOWN:
background = pygame.transform.rotate(background, 0)
case Gague_Type.LEFT:
background = pygame.transform.rotate(background, 270)
case Gague_Type.RIGHT:
background = pygame.transform.rotate(background, 90)
self._display_surf.blit(background, (self.x_pos, self.y_pos))
def key_press(self, key):
""" user input test """
#if key == pygame.K_SPACE:
def set_current_value(self, value):
self.now = 0
self.start_value = self.current_value
self.target_value = value