-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.py
164 lines (139 loc) · 4.95 KB
/
score.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
156
157
158
159
160
161
162
163
164
from st3m.ui.view import BaseView, ViewManager, ViewTransitionBlend
from st3m.input import InputState
from ctx import Context
from st3m.goose import Optional
import st3m.run
import math
import random
import sys_display
try:
import media
except ImportError:
pass
if __name__ == '__main__':
import sys
sys.path.append('/flash/apps/PetalHero')
from . import flower
from . import utils
from .midireader import difficulties
class ScoreView(BaseView):
def __init__(self, app, song, data, streak, badnotes, difficulty):
super().__init__()
self.app = app
self.song = song
self.data = data
self.difficulty = difficulty
self.flower = flower.Flower(0)
self.time = 0
self.streak = streak
self.badnotes = badnotes
self.played = False
if not self.data:
self.accuracy = 0.42
else:
events = data.track.getAllEvents()
self.accuracy = sum(map(lambda x: max(0.0, x.played * 1.0 - x.missed * 0.5), events)) / (len(events) + badnotes)
self.stars = int(5.0 * (self.accuracy + 0.05))
self.highscore = self.song.recordScore(difficulty, self.accuracy, streak, badnotes)
self.timeout = 5 if self.highscore else 0
def draw(self, ctx: Context) -> None:
#utils.background(ctx)
utils.clear(ctx)
ctx.save()
ctx.translate(-90, 1)
ctx.scale(0.325, 0.325)
for i in range(5):
if i < self.stars:
utils.fire_gradient(ctx)
else:
ctx.gray(0.15)
self.flower.draw(ctx)
ctx.translate(45 / 0.325, 0)
ctx.restore()
ctx.save()
ctx.font = "Camp Font 3"
ctx.text_align = ctx.CENTER
ctx.text_baseline = ctx.MIDDLE
ctx.font_size = 16
ctx.rgba(0.2, 0.2, 0.2, 0.69)
w = ctx.text_width(self.difficulty.text) + 10
ctx.round_rectangle(-w / 2, -8, w, 18, 8)
ctx.fill()
ctx.gray(0.8)
ctx.move_to(0, 2)
ctx.text(self.difficulty.text)
ctx.restore()
"""
ctx.rectangle(-120, 65, 240, 55)
ctx.fill()
"""
utils.fire_gradient(ctx)
ctx.font = "Camp Font 1"
ctx.font_size = 38
ctx.text_align = ctx.CENTER
ctx.text_baseline = ctx.MIDDLE
ctx.move_to (0, -78)
ctx.text("SONG")
ctx.move_to (0, -42)
ctx.text("COMPLETE")
ctx.gray(1.0)
ctx.font = "Camp Font 3"
ctx.font_size = 18
ctx.move_to (0,37)
ctx.text(f"Accuracy: {int(self.accuracy * 100)}%")
ctx.move_to(0,58)
ctx.text(f"Longest streak: {self.streak}")
ctx.font = "Camp Font 3"
ctx.text_align = ctx.CENTER
ctx.text_baseline = ctx.MIDDLE
alpha = min(self.timeout, 0.5) / 0.5
if alpha > 0:
ctx.global_alpha = alpha
utils.fire_gradient(ctx)
ctx.font_size = 18
ctx.move_to(0, 85 - math.sin(self.time * 4) * 2)
ctx.text("New highscore!")
if alpha < 1:
ctx.global_alpha = 1.0 - alpha
ctx.gray(0.5)
ctx.font_size = 16
ctx.move_to(0, 84 - math.sin(self.time * 4) * 2)
ctx.text("Press the button...")
def think(self, ins: InputState, delta_ms: int) -> None:
super().think(ins, delta_ms)
media.think(delta_ms)
utils.blm_timeout(self, delta_ms)
if self.time > 0:
self.timeout -= delta_ms / 1000
if self.timeout < 0:
self.timeout = 0
self.time += delta_ms / 1000
if self.time > 1.5 and not self.played:
self.played = True
taunt = None
if self.accuracy == 0:
taunt = "jurgen1"
elif self.accuracy >= 0.99:
taunt = "myhero"
elif self.stars in [0, 1]:
taunt = random.choice(["jurgen2", "jurgen3", "jurgen4", "jurgen5"])
elif self.stars == 5:
taunt = random.choice(["perfect1", "perfect2", "perfect3"])
if self.app and taunt:
media.load(self.app.path + "/sounds/" + taunt + ".mp3")
if not self.is_active():
return
if self.input.buttons.app.middle.pressed:
self.vm.pop(ViewTransitionBlend())
def on_enter(self, vm: Optional[ViewManager]) -> None:
super().on_enter(vm)
if self.app:
self.app.after_score = True
utils.emit("score", {"name": self.song.name, "artist": self.song.artist, "difficulty": self.difficulty.text, "accuracy": self.accuracy, "streak": self.streak, "stars": self.stars, "path": self.song.dirName})
def on_exit(self):
if self.app:
utils.play_go(self.app)
return True
if __name__ == '__main__':
view = ScoreView(None, None, 420, difficulties[2])
st3m.run.run_view(view)