1
+ import os
1
2
import arcade
2
3
import random
4
+ import shelve
3
5
from enum import Enum
4
6
5
7
SCREEN_WIDTH = 800
@@ -93,6 +95,8 @@ def __init__(self, width, height):
93
95
super ().__init__ (width , height , title = "Space Typer" )
94
96
arcade .set_background_color ((5 , 2 , 27 ))
95
97
98
+ self .high_score = int ()
99
+
96
100
self .score = int ()
97
101
self .lives = int ()
98
102
self .state = None
@@ -119,18 +123,21 @@ def setup(self):
119
123
120
124
def draw_game_over (self ):
121
125
arcade .draw_text ("Game Over" ,
122
- SCREEN_WIDTH / 2 , (SCREEN_HEIGHT / 2 ) + 54 ,
126
+ SCREEN_WIDTH / 2 , (SCREEN_HEIGHT / 2 ) + 68 ,
123
127
arcade .color .WHITE , 54 ,
124
128
align = "center" , anchor_x = "center" , anchor_y = "center"
125
129
)
126
130
127
131
arcade .draw_text ("Press SPACE to restart" ,
128
- SCREEN_WIDTH / 2 , (SCREEN_HEIGHT / 2 ) - 24 ,
132
+ SCREEN_WIDTH / 2 , (SCREEN_HEIGHT / 2 ),
129
133
arcade .color .WHITE , 24 ,
130
134
align = "center" , anchor_x = "center" , anchor_y = "center"
131
135
)
132
136
133
- arcade .draw_text (f"Score : { self .score } " , 10 , SCREEN_HEIGHT - 10 , arcade .color .WHITE , 15 , anchor_x = "left" , anchor_y = "top" )
137
+ arcade .draw_text (f"Current score : { self .score } " , 15 , 15 ,arcade .color .WHITE , 14 ,)
138
+ arcade .draw_text (f"High score : { self .high_score } " , SCREEN_WIDTH - 15 , 15 , arcade .color .WHITE , 14 ,
139
+ align = "right" , anchor_x = "right" , anchor_y = "baseline"
140
+ )
134
141
135
142
def draw_game (self ):
136
143
for word in self .word_list :
@@ -197,6 +204,16 @@ def update(self, delta_time):
197
204
self .create_word ()
198
205
199
206
if self .lives <= 0 :
207
+ path = os .path .join (os .path .expanduser ("~" ), ".space-typer" )
208
+ score_file = shelve .open (path )
209
+ new_high_score = int ()
210
+ if score_file .get ("high_score" ) == None :
211
+ new_high_score = self .score
212
+ else :
213
+ new_high_score = max ([self .score , score_file ["high_score" ]])
214
+ score_file ["high_score" ] = new_high_score
215
+ self .high_score = new_high_score
216
+
200
217
self .state = GameStates .GAME_OVER
201
218
202
219
def on_key_press (self , key , modifiers ):
0 commit comments