回答編集履歴

2

@unique

2018/01/18 16:43

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -20,7 +20,7 @@
20
20
 
21
21
  import math
22
22
 
23
- from enum import Enum
23
+ from enum import Enum, unique
24
24
 
25
25
  import pygame
26
26
 
@@ -30,6 +30,8 @@
30
30
 
31
31
 
32
32
 
33
+ @unique
34
+
33
35
  class GameState(Enum):
34
36
 
35
37
  START = 1

1

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

2018/01/18 16:43

投稿

umyu
umyu

スコア5846

test CHANGED
@@ -18,6 +18,8 @@
18
18
 
19
19
  import time
20
20
 
21
+ import math
22
+
21
23
  from enum import Enum
22
24
 
23
25
  import pygame
@@ -70,7 +72,7 @@
70
72
 
71
73
 
72
74
 
73
- def display_high_score(screen, sysfont, scores, score):
75
+ def display_high_score(screen, sysfont, high_scores, score=None):
74
76
 
75
77
  # ヘッダー行
76
78
 
@@ -80,17 +82,17 @@
80
82
 
81
83
  # 各scoreの表示
82
84
 
83
- for i, score_data in enumerate(scores):
85
+ for i, scores in enumerate(high_scores):
84
86
 
85
87
  index = i + 1
86
88
 
87
89
  marker = ' ' * 3
88
90
 
89
- if score_data == score:
91
+ if scores == score:
90
92
 
91
93
  marker = '->'
92
94
 
93
- text = "{0}{1}:{2}".format(marker, index, score_data)
95
+ text = "{0}{1}:{2}".format(marker, index, scores)
94
96
 
95
97
  text_render = sysfont.render(text, False, (0, 0, 0))
96
98
 
@@ -132,7 +134,7 @@
132
134
 
133
135
  game_state = GameState.START
134
136
 
135
- scores = []
137
+ high_scores = []
136
138
 
137
139
  while True:
138
140
 
@@ -170,23 +172,25 @@
170
172
 
171
173
  t2 = time.perf_counter()-t1-5
172
174
 
173
- score = str(round(t2, 4))
175
+ score = round(t2, 4)
174
176
 
175
177
  dif = sysfont.render("difference:{0}".format(score), False, (0,0,0))
176
178
 
177
179
  screen.blit(dif, STATUS_BAR)
178
180
 
179
- scores.append(score)
181
+ high_scores.append(score)
182
+
180
-
183
+ # math.fabsを使って0からの相対的なソートを行う
184
+
181
- scores.sort()
185
+ high_scores.sort(key=math.fabs)
182
-
186
+
183
- if len(scores) > 5:
187
+ if len(high_scores) > 5:
184
-
188
+
185
- scores.pop() #最大5件に。
189
+ high_scores.pop() #最大5件に。
186
190
 
187
191
  #ハイスコアの表示
188
192
 
189
- display_high_score(screen, sysfont, scores, score)
193
+ display_high_score(screen, sysfont, high_scores, score)
190
194
 
191
195
 
192
196