Allow name input, change timer format

master
En Yi 2018-07-19 18:17:04 +08:00
parent 2d6ebfad2a
commit 13213414ad
4 changed files with 83 additions and 39 deletions

View File

@ -1,6 +1,19 @@
DIFFICULTIES = ['Very Easy', 'Easy', 'Medium', 'Hard', 'Insane'] DIFFICULTIES = ['Very Easy', 'Easy', 'Medium', 'Hard', 'Insane']
def generate_highscore_file(file):
with open(file, 'w') as f:
for i in range(5):
names = []
times = []
for j, name in enumerate('ABCDE'):
names.append(name*(i+1))
times.append('{:02d}:00:0'.format((j+1)*(i+1)))
info = [','.join([name, time]) for name, time in zip(names, times)]
f.write('\n'.join(info))
if not i == 4:
f.write('\n---\n')
def read_highscore_file(file): def read_highscore_file(file):
with open(file, 'r') as f: with open(file, 'r') as f:
file_data = f.read() file_data = f.read()
@ -12,8 +25,7 @@ def read_highscore_file(file):
info = {} info = {}
placing_info = line.split(',') placing_info = line.split(',')
info['name'] = placing_info[0] info['name'] = placing_info[0]
time = int(placing_info[1]) info['time'] = placing_info[1]
info['time'] = "{:02d}:{:02d}.{:1d}".format(int(time / 600), int(time / 10) % 60, time % 10)
diff_list.append(info) diff_list.append(info)
highscore_list[diff] = diff_list highscore_list[diff] = diff_list
@ -23,7 +35,7 @@ def read_highscore_file(file):
def write_highscore_file(file, data): def write_highscore_file(file, data):
with open(file, 'w') as f: with open(file, 'w') as f:
for diff in DIFFICULTIES: for diff in DIFFICULTIES:
info = [','.join([placing_info['name'], str(placing_info['time'])]) for placing_info in data[diff]] info = [','.join([placing_info['name'], placing_info['time']]) for placing_info in data[diff]]
f.write('\n'.join(info)) f.write('\n'.join(info))
if not diff == DIFFICULTIES[-1]: if not diff == DIFFICULTIES[-1]:
f.write('\n---\n') f.write('\n---\n')
@ -49,6 +61,7 @@ def check_ranking(data, difficulty, name, time):
if __name__ == "__main__": if __name__ == "__main__":
score = read_highscore_file("./highscore.txt") #score = read_highscore_file("./highscore.txt")
replace_placing(score, DIFFICULTIES[2], 'abcv', 12345) #replace_placing(score, DIFFICULTIES[2], 'abcv', 12345)
write_highscore_file("./new_highscore.txt", score) #write_highscore_file("./new_highscore.txt", score)
generate_highscore_file("./highscore.txt")

View File

@ -1,29 +1,29 @@
A,140 A,01:00:0
B,1400 B,02:00:0
C,1500 C,03:00:0
D,19290 D,04:00:0
E,40000 E,05:00:0
--- ---
AA,140 AA,02:00:0
BB,1400 BB,04:00:0
CC,1500 CC,06:00:0
DD,19290 DD,08:00:0
EE,40000 EE,10:00:0
--- ---
AAA,140 AAA,03:00:0
BBB,1400 BBB,06:00:0
CCC,1500 CCC,09:00:0
DDD,19290 DDD,12:00:0
EEE,40000 EEE,15:00:0
--- ---
AAAA,140 AAAA,04:00:0
BBBB,1400 BBBB,08:00:0
CCCC,1500 CCCC,12:00:0
DDDD,19290 DDDD,16:00:0
EEEE,40000 EEEE,20:00:0
--- ---
AAAAA,9000 AAAAA,05:00:0
BBBBB,10000 BBBBB,10:00:0
CCCCC,20000 CCCCC,15:00:0
DDDDD,40000 DDDDD,20:00:0
EEEEE,70000 EEEEE,25:00:0

View File

@ -62,7 +62,7 @@ class TimerDisplayer(QGraphicsWidget):
painter.setPen(self.box_pen) painter.setPen(self.box_pen)
painter.drawRect(box) painter.drawRect(box)
painter.drawText(box, Qt.AlignCenter, painter.drawText(box, Qt.AlignCenter,
"{:02d}:{:02d}.{:1d}".format(int(self.atenth_seconds/600), "{:02d}:{:02d}:{:1d}".format(int(self.atenth_seconds/600),
int(self.atenth_seconds/10) % 60, int(self.atenth_seconds/10) % 60,
self.atenth_seconds % 10)) self.atenth_seconds % 10))

View File

@ -20,13 +20,16 @@ class HighScoreBoard(QWidget):
def __init__(self, width, height): def __init__(self, width, height):
super().__init__() super().__init__()
self.final_time = "00:10:00"
self.current_difficulty = hs.DIFFICULTIES[1]
self.layout = QVBoxLayout(self) self.layout = QVBoxLayout(self)
self.layout.setAlignment(Qt.AlignCenter) self.layout.setAlignment(Qt.AlignCenter)
self.diff_switch = DifficultySwitch() self.diff_switch = DifficultySwitch()
self.layout.addLayout(self.diff_switch) self.layout.addLayout(self.diff_switch)
self.score_grid = ScoreGrid() self.score_grid = ScoreGrid()
self.layout.addLayout(self.score_grid) self.layout.addLayout(self.score_grid)
self.layout.addWidget(NameInput()) self.name_input = NameInput()
self.layout.addWidget(self.name_input)
self.setFixedSize(width, height) self.setFixedSize(width, height)
@ -36,6 +39,8 @@ class HighScoreBoard(QWidget):
""") """)
self.diff_switch.difficultySelected.connect(self.change_score_board) self.diff_switch.difficultySelected.connect(self.change_score_board)
self.name_input.nameReceived.connect(self.set_score)
self.score_grid.scoreUpdate.connect(self.diff_switch.go_to_difficulty)
def change_score_board(self, difficulty): def change_score_board(self, difficulty):
self.score_grid.replace_scores(difficulty) self.score_grid.replace_scores(difficulty)
@ -44,6 +49,8 @@ class HighScoreBoard(QWidget):
if self.isVisible(): if self.isVisible():
self.score_grid.show_score_info(toggle) self.score_grid.show_score_info(toggle)
def set_score(self, name):
self.score_grid.set_highscore(self.current_difficulty, name, self.final_time)
class DifficultySwitch(QHBoxLayout): class DifficultySwitch(QHBoxLayout):
difficultySelected = pyqtSignal(str) difficultySelected = pyqtSignal(str)
@ -67,6 +74,7 @@ class DifficultySwitch(QHBoxLayout):
self.shift_direction = FORWARD self.shift_direction = FORWARD
self.show_pos = self.max_length self.show_pos = self.max_length
self.next_pos = self.max_length
self.timer = QTimer(self) self.timer = QTimer(self)
self.timer.setInterval(20) self.timer.setInterval(20)
self.timer.timeout.connect(self.shift_pos) self.timer.timeout.connect(self.shift_pos)
@ -90,20 +98,30 @@ class DifficultySwitch(QHBoxLayout):
def shift_difficulty(self, direction): def shift_difficulty(self, direction):
if not self.timer.isActive(): if not self.timer.isActive():
self.shift_direction = direction self.shift_direction = direction
self.next_pos = self.circular_value(self.next_pos + direction * self.max_length)
self.timer.start() self.timer.start()
def go_to_difficulty(self, difficulty):
pos = (hs.DIFFICULTIES.index(difficulty) + 1) * self.max_length
self.show_pos = pos
self.next_pos = pos
def shift_pos(self): def shift_pos(self):
self.show_pos += self.shift_direction self.show_pos = self.circular_value(self.show_pos + self.shift_direction)
if self.show_pos == (len(hs.DIFFICULTIES)+1) * self.max_length: if self.show_pos == self.next_pos:
self.show_pos = self.max_length
elif self.show_pos == 0:
self.show_pos = len(hs.DIFFICULTIES) * self.max_length
if self.show_pos % 9 == 0:
self.timer.stop() self.timer.stop()
self.difficultySelected.emit(self.difficulty_display.text().strip(' ')) self.difficultySelected.emit(self.difficulty_display.text().strip(' '))
def circular_value(self, value):
if value == (len(hs.DIFFICULTIES)+1) * self.max_length:
value = self.max_length
elif value == 0:
value = len(hs.DIFFICULTIES) * self.max_length
return value
class ScoreGrid(QGridLayout): class ScoreGrid(QGridLayout):
scoreUpdate = pyqtSignal(str)
def __init__(self): def __init__(self):
super().__init__() super().__init__()
@ -139,8 +157,13 @@ class ScoreGrid(QGridLayout):
self.animated_labels[2*i].replace_text(scores[i]['name']) self.animated_labels[2*i].replace_text(scores[i]['name'])
self.animated_labels[2*i+1].replace_text(scores[i]['time']) self.animated_labels[2*i+1].replace_text(scores[i]['time'])
def set_highscore(self, difficulty, name, time):
hs.replace_placing(self.highscore_list, difficulty, name, time)
self.replace_scores(difficulty)
self.scoreUpdate.emit(difficulty)
class NameInput(QWidget): class NameInput(QWidget):
nameReceived = pyqtSignal(str)
def __init__(self): def __init__(self):
super().__init__() super().__init__()
@ -151,6 +174,14 @@ class NameInput(QWidget):
self.name_input = QLineEdit(self) self.name_input = QLineEdit(self)
self.layout.addWidget(self.name_input) self.layout.addWidget(self.name_input)
self.name_input.returnPressed.connect(self.receive_name_input)
def receive_name_input(self):
print(self.name_input.text().strip(' '))
name = self.name_input.text().strip(' ')
if name:
self.nameReceived.emit(name)
print('name sent')
class AnimatedLabel(QLabel): class AnimatedLabel(QLabel):