musicgame.py:
import pygame
import random
import json
import hashlib
def hsh(text):
return hashlib.md5(text.encode('utf-8')).hexdigest()
pygame.init()
pygame.mixer.init()
pygame.display.set_caption('Typing Practice')
pygame.display.set_icon(pygame.image.load('s/ico.png'))
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
JUDGELINE_POS = SCREEN_HEIGHT - 50
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()
realTimeSinceStartUp = 0.0
waitTime = 3
autoplay = False
keys='sdjk'
speed = 1000
offset = 0
HIDDEN_CHART='''
4:580,4:1426,2:1848,1:1848,4:2271,4:3116,4:3961,4:4806,4:5651,2:6073,4:6496,2:6919,4:4383,4:5228,4:2693,4:3538,2:3961,2:4383,1:5651,2:5651,3:5651,1:6073,3:6073,4:6073,1:6496,2:6496,3:6496,1:6919,3:6919,4:6919,1:7341,1:7764,1:8186,1:8609,1:9031,3:9031,1:9454,3:9454,1:9876,1:10299,1:10721,1:11144,1:11566,1:11989,1:12411,2:12411,3:12411,4:12411,4:12834,3:12834,2:12834,1:12834,1:13257,2:13257,3:13257,4:13257,4:13679,3:13679,2:13679,1:13679,4:14102,4:14524,4:14947,4:15369,4:15792,4:16214,4:16637,4:17059,2:17059,2:17482,4:17482,4:17904,4:18327,4:18750,1:19172,2:19172,3:19172,4:19172,1:19595,2:19595,3:19595,4:19595,1:20017,2:20017,3:20017,4:20017,1:20440,2:20440,3:20440,4:20440,4:1003,4:1848,1:20862,1:21285,1:21707,1:22130,1:22552,3:22552,1:22975,3:22975,1:23397,3:23397,1:23820,3:23820,1:24242,1:24665,1:25088,1:25510,1:25933,2:25933,3:25933,4:25933,4:27623,1:28045,3:27623,4:28045,1:28257,1:28468,4:28468,2:28679,4:28890,3:28890,2:28890,1:28890,1:29102,2:29102,3:29102,4:29102,1:29313,1:29735,1:30158,1:30580,2:30580,3:30580,1:31003,2:31003,3:31003,1:31426,1:31848,1:32271,3:32271,1:32693,3:32693,4:32693,4:32271,1:33116,1:33538,1:33961,1:34383,2:34383,3:34383,4:34383,1:34806,2:34806,3:34806,4:34806,1:35228,3:35228,4:35228,2:35228,1:35651,3:35651,4:35651,2:35651,4:36073,4:36496,4:36919,4:37341,4:37764,2:37764,2:38186,4:38186,2:38609,4:38609,2:39031,4:39031,4:39454,4:39876,4:40299,4:40721,1:41144,2:41144,3:41144,4:41144,1:41355,2:41355,4:41355,3:41355,1:41566,2:41566,3:41566,4:41566,1:41778,2:41778,3:41778,4:41778,1:41989,2:41989,3:41989,4:41989,1:42200,2:42200,3:42200,4:42200,2:42411,1:42411,3:42411,4:42411,2:37341,2:39454,4:42834,4:43257,4:43679,1:44102,4:44102,2:44102,2:44524,1:44524,4:44524,1:44947,2:44947,4:44947,4:45369,4:45792,4:46214,2:46637,4:46637,4:47059,2:46214,4:47482,4:47904,3:47904,2:47904,1:47904,1:48327,2:48327,3:48327,4:48327,1:48750,2:48750,3:48750,4:48750,1:49172,2:49172,4:49172,3:49172,1:49595,1:50017,1:50440,1:50862,1:51285,3:51285,1:51707,3:51707,1:52130,3:52130,1:52552,3:52552,1:52975,1:53397,1:53820,1:54242,1:54665,2:54665,3:54665,4:54665,4:55088,3:55088,2:55088,1:55088,1:55510,2:55510,3:55510,4:55510,4:55933,3:55933,2:55933,1:55933,1:27623,2:27623,2:28045,3:28045,1:27834,2:27834,3:27834,4:27834,2:28257,2:28468,3:28468,3:28257,4:28257,1:28679,3:28679,4:28679,4:56355,4:56778,4:57200,4:57623,1:57623,2:57623,1:58045,2:58045,4:58045,1:58468,2:58468,4:58468,4:58890,4:59313,4:59735,2:59735,1:59735,2:60158,1:60158,4:60158,4:60580,2:60580,1:60580,4:61003,4:61426,1:61848,2:61848,3:61848,4:61848,1:62271,3:62271,4:62271,2:62271,1:62693,2:62693,3:62693,4:62693,1:63116,1:63538,1:63961,1:64383,3:64383,1:64806,3:64806,1:65228,3:65228,1:65651,3:65651,1:66073,3:66073,1:66496,1:66919,1:67341,1:67764,1:68186,2:68186,3:68186,4:68186,1:68609,2:68609,3:68609,4:68609,1:69031,2:69031,4:69031,3:69031,1:69242,1:69454,2:69454,2:69242,3:69454,3:69242,4:69454,4:69242,4:69876,4:70299,4:70088,4:70510,2:70721,4:70721,4:71144,4:70933,2:70933,4:71566,4:71989,4:71778,4:72200,4:72411,2:72623,4:72623,3:72623,2:72834,3:72834,4:72834,1:74102,3:74102,1:74313,1:74524,3:74524,3:74313,1:74947,1:75158,1:75369,1:75580,1:75792,1:76003,4:76003,1:76214,3:76214,4:76214,4:71355,2:71144,2:71355,2:73045,3:73045,4:73045,4:73257,4:73468,4:73679,4:73890,4:74102,2:74102,2:74313,4:74313,2:74524,4:74524,1:74735,2:74735,3:74735,4:74735,1:2271,2:2271,1:2693,2:2693,1:3961,1:4383,3:8609,4:10721,3:10721,3:11144,4:11144,2:14102,1:14102,1:14524,2:14524,2:14947,1:14947,2:16637,2:17904,2:18327,2:18750,2:36073,1:36073,1:36496,2:36496,1:36919,2:36919,1:37341,1:37764,1:38186,1:38609,1:39031,1:39454,2:71566,2:74947,3:74947,4:74947,2:75158,3:75158,4:75158,2:75369,3:75369,4:75369,2:75580,3:75580,4:75580,2:75792,3:75792,4:75792,2:76003,3:76003,2:76214,1:580,2:580,3:580
'''
try:
dat=json.load(open('config.txt','r'))
keys, speed, offset = dat['keys'], float(dat['speed'])*1000, float(dat['offset'])
except Exception as e:
print(e)
json.dump({'keys':'sdjk','speed':1.0,'offset':0},open('config.txt','w'))
PLUSTIME=0.040
PERFECTTIME=PLUSTIME*2
GOODTIME=PERFECTTIME*2
DELAYS=0
DONENOTES=0
LAST_DELAY=0
JUDGECOUNTERS=[0,0,0,0]
slot_judgelist=[]
cheating=False
DIFF_LIST=[
('s/0.txt', 's/music.ogg', 'Easy', 254, '"THX4TESTINGHAHA"'),
('s/1.txt', 's/music.ogg', 'Normal', 529, '"TRYSTHHARDERPLZ"'),
('s/2.txt', 's/music.ogg', 'Hard', 983, '"GO2HANDLESTREAM"'),
('s/3.txt', 's/music.ogg', 'Master', 1589, '"WOWSTREAMEXPERT"'),
('s/4.txt', 's/music.ogg', 'Legend', 2828, '"MASTERORCHEATER"'),
]
HASHES=[
'6c812fb0e75b3bccbe80ea93cb1ecd67',
'009e730a33df9c9e649962568cafb5aa',
'1d2fd7b5318c4ee43453924fc89f8067',
'9d9a3e0c7b28d332286374b1ac1b2e62',
'06cc4f759f1f62b1270ea488626c034f',
'',
]
def Text(x, y, text, font_size=24, color=(255, 255, 255)):
font = pygame.font.Font(None, font_size)
text_surface = font.render(text, True, color)
screen.blit(text_surface, (x, y))
KP = []
class GameObject:
def __init__(self):
self.awaken = False
self.judged = False
self.lTime = 0
self.enabled = True
self.position = [0, 0]
self.size_x = 100
self.size_y = 5
def Update(self, deltaTime):
pass
def OnEnable(self):
pass
def GetColor(self):
return (255,255,255)
class Note(GameObject):
def GetColor(self):
return (255-128*(self.pos%2),255,255*(self.pos%2))
def SetData(self, pos, time):
self.pos = pos
self.time = time
self.position = [pos * 100 - 250 + SCREEN_WIDTH / 2 - self.size_x / 2, - (time + waitTime) * speed + JUDGELINE_POS]
def Update(self, deltaTime):
rt=realTimeSinceStartUp-waitTime
if autoplay and rt>=self.time and not self.judged:
self.OnJudge()
if rt>self.time-GOODTIME and not self.awaken:
self.awaken=True
slot_judgelist.append(self)
if rt>self.time+GOODTIME and not self.judged:
self.judged=True
slot_judgelist.remove(self)
Destroy(self)
self.OnMiss()
self.position[1] = JUDGELINE_POS - (self.time-rt) * speed
def OnJudge(self):
global DELAYS
global DONENOTES
global LAST_DELAY
global combo
global lastJudgeColor
global lastJudgeType
self.judged=True
slot_judgelist.remove(self)
Destroy(self)
DONENOTES+=1
rt=realTimeSinceStartUp-waitTime
offset=self.time-rt
combo+=1
LAST_DELAY=offset
DELAYS+=offset
if abs(offset)<=PLUSTIME:
JUDGECOUNTERS[0]+=1
lastJudgeType='BEST'
lastJudgeColor=(255,192,0)
elif abs(offset)<=PERFECTTIME:
JUDGECOUNTERS[1]+=1
lastJudgeType='COOL'
lastJudgeColor=(255,255,0)
else:
JUDGECOUNTERS[2]+=1
lastJudgeType='GOOD'
lastJudgeColor=(128,255,128)
def OnMiss(self):
global combo
global lastJudgeColor
global lastJudgeType
lastJudgeType='MISS'
lastJudgeColor=(64,0,0)
combo = 0
JUDGECOUNTERS[3]+=1
class Line(GameObject):
def GetColor(self):
global KP
global keys
now=realTimeSinceStartUp
if keys[self.pos-1] in KP:
self.lTime=now
return (128+127-int(127*min((now-self.lTime),0.5)/0.5),255-int(255*min((now-self.lTime),0.5)/0.5),255-int(255*min((now-self.lTime),0.5)/0.5))
def SetData(self, pos):
self.pos = pos
self.position = [pos * 100 - 250 + SCREEN_WIDTH / 2 - self.size_x / 2, JUDGELINE_POS]
note_prefab = Note()
line_prefab = Line()
gameObjects = []
destroyed = 0
noteCount = 0
def Destroy(gameObj):
global destroyed
destroyed+=1
gameObjects.remove(gameObj)
def Instantiate(gameObj):
new_obj = gameObj.__class__()
new_obj.OnEnable()
gameObjects.append(new_obj)
return new_obj
sound=None
inner=False
def LoadAssets(asset):
global FLAG
global HASHES
global cheating
global inner
global note_prefab
global noteCount
global sound
chart = HIDDEN_CHART if asset[0]=='inner' else open(asset[0],'r').read()
if hsh(chart)!=HASHES[selectingIdx%len(DIFF_LIST)]:
cheating=True
chart = chart.split(',')
pygame.mixer.music.load(asset[1])
sound=pygame.mixer.Sound(asset[1])
for info in chart:
noteCount+=1
tok=info.split(':')
Instantiate(note_prefab).SetData(int(tok[0]), int(tok[1])*0.001+offset)
if noteCount!=asset[3]:
cheating=True
if asset[0]=='inner':
inner=True
cheating=False
FLAG=asset[4]
combo = 0
lines=[]
lastJudgeType=''
lastJudgeColor=(0,0,0)
playing=False
def GetAcc():
global JUDGECOUNTERS
if sum(JUDGECOUNTERS)==0:
return 0
return (JUDGECOUNTERS[0]+JUDGECOUNTERS[1]*0.75+JUDGECOUNTERS[2]*0.5)/sum(JUDGECOUNTERS)
def MainUpdate():
global playing
if not playing and realTimeSinceStartUp-waitTime>=0:
playing=True
pygame.mixer.music.play(0)
if not playing and realTimeSinceStartUp-waitTime<-3:
Text(SCREEN_WIDTH / 4, 50, f'Practice: S D J K', 60, (64,64,64))
else:
if noteCount==destroyed or realTimeSinceStartUp-waitTime > sound.get_length():
if cheating:
Text(SCREEN_WIDTH / 2 - 200, 50, f'Invalid chart', 60, (64,64,0))
elif GetAcc()>=0.95 or inner:
Text(SCREEN_WIDTH / 2 - 200, 50, FLAG, 60, (64,64,64))
else:
Text(SCREEN_WIDTH / 2 - 200, 50, f'Sorry, but you failed', 60, (64,0,0))
else:
st=str(combo)
Text(SCREEN_WIDTH / 2 - 12.5 * len(st), 50, st, 60, (64,64,64))
Text(SCREEN_WIDTH / 2 - 35, 100, lastJudgeType, 48, lastJudgeColor)
Text(SCREEN_WIDTH / 2 - 35, 150, ('EARLY' if LAST_DELAY>0 else 'LATE')+': '+str(int(LAST_DELAY*1000))+'ms', 20, (32,32,64) if LAST_DELAY>0 else (64,32,32))
Text(50, SCREEN_HEIGHT / 2, f'{realTimeSinceStartUp-waitTime:.3f}')
Text(50, SCREEN_HEIGHT / 2 + 30, f'{GetAcc()*100:.3f}%')
Text(50, SCREEN_HEIGHT / 2 + 60, f'Delay-Avg: {int(DELAYS/(1 if DONENOTES==0 else DONENOTES)*1000)}ms')
for i in range(len(lines)):
line=lines[i]
Text(line.position[0] + line.size_x / 2,JUDGELINE_POS+20,keys[i].upper(),36,line.GetColor())
key=keys[i]
def OnHit(hit):
for idx in range(len(keys)):
key=keys[idx]
if hit==key:
for slot in slot_judgelist:
if slot.pos-1==idx:
slot.OnJudge()
break
for i in range(1, 5):
l=Instantiate(line_prefab)
lines.append(l)
l.SetData(i)
running = True
selectedChallenge=False
selectingIdx=0
keySeq=''
tco=0
tt=''
while running and not selectedChallenge:
deltaTime = clock.tick(200) / 1000.0
KP=[]
if keySeq=='RDURRDRRUUDDLRLR':
keySeq+='#'
realTimeSinceStartUp = 0.0
LoadAssets(('inner','s/music.mp3','',552, 'Slow down and look'))
selectedChallenge=True
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key==pygame.K_DOWN:
tco+=1
if tco<17:
tt+='↓'+(' ' if tco%4==0 else '')
keySeq+='D'
selectingIdx+=1
pygame.display.set_caption(tt)
elif event.key==pygame.K_UP:
tco+=1
if tco<17:
tt+='↑'+(' ' if tco%4==0 else '')
keySeq+='U'
selectingIdx+=len(DIFF_LIST)-1
pygame.display.set_caption(tt)
elif event.key==pygame.K_LEFT:
tco+=1
if tco<17:
tt+='←'+(' ' if tco%4==0 else '')
keySeq+='L'
pygame.display.set_caption(tt)
elif event.key==pygame.K_RIGHT:
tco+=1
if tco<17:
tt+='→'+(' ' if tco%4==0 else '')
keySeq+='R'
pygame.display.set_caption(tt)
elif event.key==13:
realTimeSinceStartUp = 0.0
LoadAssets(DIFF_LIST[selectingIdx%len(DIFF_LIST)])
selectedChallenge=True
pygame.display.set_caption('Typing Practice')
screen.fill((0, 0, 0))
for i in range(len(DIFF_LIST)):
diff=DIFF_LIST[i]
prefix='>' if selectingIdx%len(DIFF_LIST)==i else ' '
col=(255,255,0) if selectingIdx%len(DIFF_LIST)==i else (255,255,255)
Text(200, (i-len(DIFF_LIST)/2)*50+SCREEN_HEIGHT/2, prefix+diff[2], 30,col)
pygame.display.flip()
while running:
deltaTime = clock.tick(200) / 1000.0
realTimeSinceStartUp += deltaTime
KP=[]
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
KP.append(event.unicode)
OnHit(event.unicode)
for obj in gameObjects:
if obj.enabled:
obj.Update(deltaTime)
screen.fill((0, 0, 0))
MainUpdate()
for obj in gameObjects:
if obj.enabled:
pygame.draw.rect(screen, obj.GetColor(), (*obj.position, obj.size_x, obj.size_y))
pygame.display.flip()
pygame.quit()