From 13213414ad77cb9fc482a50714ef984c9e6870c0 Mon Sep 17 00:00:00 2001 From: En Yi Date: Thu, 19 Jul 2018 18:17:04 +0800 Subject: [PATCH] Allow name input, change timer format --- general/highscore.py | 25 +++++++++++---- general/highscore.txt | 50 ++++++++++++++--------------- graphic_components/menu_graphics.py | 2 +- graphic_components/scoreboard.py | 45 ++++++++++++++++++++++---- 4 files changed, 83 insertions(+), 39 deletions(-) diff --git a/general/highscore.py b/general/highscore.py index 18b28dc..5c973de 100644 --- a/general/highscore.py +++ b/general/highscore.py @@ -1,6 +1,19 @@ 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): with open(file, 'r') as f: file_data = f.read() @@ -12,8 +25,7 @@ def read_highscore_file(file): info = {} placing_info = line.split(',') info['name'] = placing_info[0] - time = int(placing_info[1]) - info['time'] = "{:02d}:{:02d}.{:1d}".format(int(time / 600), int(time / 10) % 60, time % 10) + info['time'] = placing_info[1] diff_list.append(info) highscore_list[diff] = diff_list @@ -23,7 +35,7 @@ def read_highscore_file(file): def write_highscore_file(file, data): with open(file, 'w') as f: 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)) if not diff == DIFFICULTIES[-1]: f.write('\n---\n') @@ -49,6 +61,7 @@ def check_ranking(data, difficulty, name, time): if __name__ == "__main__": - score = read_highscore_file("./highscore.txt") - replace_placing(score, DIFFICULTIES[2], 'abcv', 12345) - write_highscore_file("./new_highscore.txt", score) \ No newline at end of file + #score = read_highscore_file("./highscore.txt") + #replace_placing(score, DIFFICULTIES[2], 'abcv', 12345) + #write_highscore_file("./new_highscore.txt", score) + generate_highscore_file("./highscore.txt") \ No newline at end of file diff --git a/general/highscore.txt b/general/highscore.txt index fac4bff..7082a29 100644 --- a/general/highscore.txt +++ b/general/highscore.txt @@ -1,29 +1,29 @@ -A,140 -B,1400 -C,1500 -D,19290 -E,40000 +A,01:00:0 +B,02:00:0 +C,03:00:0 +D,04:00:0 +E,05:00:0 --- -AA,140 -BB,1400 -CC,1500 -DD,19290 -EE,40000 +AA,02:00:0 +BB,04:00:0 +CC,06:00:0 +DD,08:00:0 +EE,10:00:0 --- -AAA,140 -BBB,1400 -CCC,1500 -DDD,19290 -EEE,40000 +AAA,03:00:0 +BBB,06:00:0 +CCC,09:00:0 +DDD,12:00:0 +EEE,15:00:0 --- -AAAA,140 -BBBB,1400 -CCCC,1500 -DDDD,19290 -EEEE,40000 +AAAA,04:00:0 +BBBB,08:00:0 +CCCC,12:00:0 +DDDD,16:00:0 +EEEE,20:00:0 --- -AAAAA,9000 -BBBBB,10000 -CCCCC,20000 -DDDDD,40000 -EEEEE,70000 \ No newline at end of file +AAAAA,05:00:0 +BBBBB,10:00:0 +CCCCC,15:00:0 +DDDDD,20:00:0 +EEEEE,25:00:0 \ No newline at end of file diff --git a/graphic_components/menu_graphics.py b/graphic_components/menu_graphics.py index 8db04f9..0bb6bd1 100644 --- a/graphic_components/menu_graphics.py +++ b/graphic_components/menu_graphics.py @@ -62,7 +62,7 @@ class TimerDisplayer(QGraphicsWidget): painter.setPen(self.box_pen) painter.drawRect(box) 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, self.atenth_seconds % 10)) diff --git a/graphic_components/scoreboard.py b/graphic_components/scoreboard.py index 9ebc64c..1971a42 100644 --- a/graphic_components/scoreboard.py +++ b/graphic_components/scoreboard.py @@ -20,13 +20,16 @@ class HighScoreBoard(QWidget): def __init__(self, width, height): super().__init__() + self.final_time = "00:10:00" + self.current_difficulty = hs.DIFFICULTIES[1] self.layout = QVBoxLayout(self) self.layout.setAlignment(Qt.AlignCenter) self.diff_switch = DifficultySwitch() self.layout.addLayout(self.diff_switch) self.score_grid = ScoreGrid() self.layout.addLayout(self.score_grid) - self.layout.addWidget(NameInput()) + self.name_input = NameInput() + self.layout.addWidget(self.name_input) self.setFixedSize(width, height) @@ -36,6 +39,8 @@ class HighScoreBoard(QWidget): """) 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): self.score_grid.replace_scores(difficulty) @@ -44,6 +49,8 @@ class HighScoreBoard(QWidget): if self.isVisible(): 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): difficultySelected = pyqtSignal(str) @@ -67,6 +74,7 @@ class DifficultySwitch(QHBoxLayout): self.shift_direction = FORWARD self.show_pos = self.max_length + self.next_pos = self.max_length self.timer = QTimer(self) self.timer.setInterval(20) self.timer.timeout.connect(self.shift_pos) @@ -90,20 +98,30 @@ class DifficultySwitch(QHBoxLayout): def shift_difficulty(self, direction): if not self.timer.isActive(): self.shift_direction = direction + self.next_pos = self.circular_value(self.next_pos + direction * self.max_length) 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): - self.show_pos += self.shift_direction - if self.show_pos == (len(hs.DIFFICULTIES)+1) * self.max_length: - 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.show_pos = self.circular_value(self.show_pos + self.shift_direction) + if self.show_pos == self.next_pos: self.timer.stop() 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): + scoreUpdate = pyqtSignal(str) def __init__(self): super().__init__() @@ -139,8 +157,13 @@ class ScoreGrid(QGridLayout): self.animated_labels[2*i].replace_text(scores[i]['name']) 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): + nameReceived = pyqtSignal(str) def __init__(self): super().__init__() @@ -151,6 +174,14 @@ class NameInput(QWidget): self.name_input = QLineEdit(self) 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):