forked from quasar098/midi-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scorekeeper.py
107 lines (89 loc) · 4.24 KB
/
scorekeeper.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
from utils import *
import pygame
from hiticon import HitIcon, HitLevel
class Scorekeeper:
def __init__(self, world):
self.world = world
self.unhit_notes: list[float] = []
self.hit_icons: list[HitIcon] = []
self.hp = 100
self.shown_hp = 0
@property
def life_bar_rect(self):
return pygame.Rect((10, 10, int(Config.SCREEN_WIDTH / 2 - 30), 30))
def draw(self, screen: pygame.Surface, current_time: float, misses: int):
self.hp = max(0, min(self.hp, 100))
bg_color = get_colors().get("hp_bar_background", pygame.Color(34, 51, 59))
border_color = get_colors().get("hp_bar_border", pygame.Color(10, 9, 8))
fill_colors = get_colors().get("hp_bar_fill", (
pygame.Color(156, 198, 155), pygame.Color(189, 228, 168), pygame.Color(215, 242, 186)
))
screen.fill(border_color, self.life_bar_rect)
inner = self.life_bar_rect.inflate(-8, -8)
screen.fill(bg_color, inner)
bar_width = inner.width / len(fill_colors)
bar_hp_repr = 100 / len(fill_colors)
leftover = self.shown_hp
for _ in range(len(fill_colors)):
chunk_rect = inner.copy()
chunk_rect.x = inner.x + bar_width * _
width = min(max(leftover, 0), int(bar_hp_repr)+1)/bar_hp_repr*bar_width
leftover -= bar_hp_repr
chunk_rect.width = width
screen.fill(fill_colors[_], chunk_rect)
if current_time > 0:
self.hp -= Config.hp_drain_rate*Config.dt
hp_show_damping = 10
self.shown_hp = self.shown_hp*(1-hp_show_damping*Config.dt)+self.hp*(hp_show_damping*Config.dt)
# remove unhit notes
to_remove = []
for timestamp in self.unhit_notes:
if timestamp+0.12 < current_time and not self.world.square.died: # 120ms late is missed note
self.hp -= 6
self.hit_icons.append(HitIcon(HitLevel.miss, self.world.square.pos))
to_remove.append(timestamp)
misses = misses if misses is not None else 0
misses += 1
for t_remove in to_remove:
self.unhit_notes.remove(t_remove)
return misses
def do_keypress(self, current_time: float, misses: int):
# negative closest means hit before, positive means hit after
for timestamp in self.unhit_notes:
offset = current_time-timestamp
if not self.world.square.died:
if offset > 0.06: # 60ms late - 120ms late
self.hit_icons.append(HitIcon(HitLevel.late, self.world.square.pos.copy()))
self.hp -= 1
misses += 1
elif offset > -0.06: # 60ms early - 60ms late
self.hit_icons.append(HitIcon(HitLevel.perfect, self.world.square.pos.copy()))
self.hp += 3
elif offset > -0.09: # 60ms early - 90ms early
self.hit_icons.append(HitIcon(HitLevel.good, self.world.square.pos.copy()))
self.hp -= 1
elif offset > -0.12: # 90ms early - 120ms early
self.hit_icons.append(HitIcon(HitLevel.early, self.world.square.pos.copy()))
self.hp -= 2
misses += 1
else:
return misses
self.unhit_notes.remove(timestamp)
return misses
def should_hit(self, current_time: float, blacklist: list):
# negative closest means hit before, positive means hit after
for timestamp in self.unhit_notes:
offset = current_time-timestamp
if timestamp in blacklist: continue
if offset > 0.06: # 60ms late - 120ms late
pass
elif offset > -0.06: # 60ms early - 60ms late
blacklist.append(timestamp)
return True, blacklist
elif offset > -0.09: # 60ms early - 90ms early
pass
elif offset > -0.12: # 90ms early - 120ms early
pass
else:
return False, blacklist
return False, blacklist