teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

@unique

2018/01/18 16:43

投稿

umyu
umyu

スコア5846

answer CHANGED
@@ -9,11 +9,12 @@
9
9
  import sys
10
10
  import time
11
11
  import math
12
- from enum import Enum
12
+ from enum import Enum, unique
13
13
  import pygame
14
14
  from pygame.locals import *
15
15
 
16
16
 
17
+ @unique
17
18
  class GameState(Enum):
18
19
  START = 1
19
20
  COUNT_UP = 2

1

math.fabsを使ったソートに変更

2018/01/18 16:43

投稿

umyu
umyu

スコア5846

answer CHANGED
@@ -8,6 +8,7 @@
8
8
  # -*- coding: UTF-8 -*
9
9
  import sys
10
10
  import time
11
+ import math
11
12
  from enum import Enum
12
13
  import pygame
13
14
  from pygame.locals import *
@@ -34,17 +35,17 @@
34
35
  sys.exit()
35
36
 
36
37
 
37
- def display_high_score(screen, sysfont, scores, score):
38
+ def display_high_score(screen, sysfont, high_scores, score=None):
38
39
  # ヘッダー行
39
40
  text = sysfont.render("### High Score ####", False, (0, 0, 0))
40
41
  screen.blit(text, (20, 0))
41
42
  # 各scoreの表示
42
- for i, score_data in enumerate(scores):
43
+ for i, scores in enumerate(high_scores):
43
44
  index = i + 1
44
45
  marker = ' ' * 3
45
- if score_data == score:
46
+ if scores == score:
46
47
  marker = '->'
47
- text = "{0}{1}:{2}".format(marker, index, score_data)
48
+ text = "{0}{1}:{2}".format(marker, index, scores)
48
49
  text_render = sysfont.render(text, False, (0, 0, 0))
49
50
  screen.blit(text_render, (20, 30 * index))
50
51
 
@@ -65,7 +66,7 @@
65
66
  screen.blit(click_start, STATUS_BAR)
66
67
  pygame.display.update() # 画面更新
67
68
  game_state = GameState.START
68
- scores = []
69
+ high_scores = []
69
70
  while True:
70
71
  for event in pygame.event.get():
71
72
  # 終了用のイベント処理
@@ -84,15 +85,16 @@
84
85
  elif game_state == GameState.COUNT_UP:
85
86
  # 5sec
86
87
  t2 = time.perf_counter()-t1-5
87
- score = str(round(t2, 4))
88
+ score = round(t2, 4)
88
89
  dif = sysfont.render("difference:{0}".format(score), False, (0,0,0))
89
90
  screen.blit(dif, STATUS_BAR)
90
- scores.append(score)
91
+ high_scores.append(score)
92
+ # math.fabsを使って0からの相対的なソートを行う
91
- scores.sort()
93
+ high_scores.sort(key=math.fabs)
92
- if len(scores) > 5:
94
+ if len(high_scores) > 5:
93
- scores.pop() #最大5件に。
95
+ high_scores.pop() #最大5件に。
94
96
  #ハイスコアの表示
95
- display_high_score(screen, sysfont, scores, score)
97
+ display_high_score(screen, sysfont, high_scores, score)
96
98
 
97
99
  game_state = GameState.RESULT #ゲームの状態を「リザルト」に変更
98
100
  elif game_state == GameState.RESULT: