Skip to content

Commit bd93c3e

Browse files
committed
Implemented high score system
1 parent 0694a37 commit bd93c3e

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

space-typer.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import os
12
import arcade
23
import random
4+
import shelve
35
from enum import Enum
46

57
SCREEN_WIDTH = 800
@@ -93,6 +95,8 @@ def __init__(self, width, height):
9395
super().__init__(width, height, title="Space Typer")
9496
arcade.set_background_color((5, 2, 27))
9597

98+
self.high_score = int()
99+
96100
self.score = int()
97101
self.lives = int()
98102
self.state = None
@@ -119,18 +123,21 @@ def setup(self):
119123

120124
def draw_game_over(self):
121125
arcade.draw_text("Game Over",
122-
SCREEN_WIDTH / 2, (SCREEN_HEIGHT / 2) + 54,
126+
SCREEN_WIDTH / 2, (SCREEN_HEIGHT / 2) + 68,
123127
arcade.color.WHITE, 54,
124128
align="center", anchor_x="center", anchor_y="center"
125129
)
126130

127131
arcade.draw_text("Press SPACE to restart",
128-
SCREEN_WIDTH / 2, (SCREEN_HEIGHT / 2) - 24,
132+
SCREEN_WIDTH / 2, (SCREEN_HEIGHT / 2),
129133
arcade.color.WHITE, 24,
130134
align="center", anchor_x="center", anchor_y="center"
131135
)
132136

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+
)
134141

135142
def draw_game(self):
136143
for word in self.word_list:
@@ -197,6 +204,16 @@ def update(self, delta_time):
197204
self.create_word()
198205

199206
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+
200217
self.state = GameStates.GAME_OVER
201218

202219
def on_key_press(self, key, modifiers):

0 commit comments

Comments
 (0)