Hey can you add a highscore scoreboard to this where the highscore leaderboard is stored in Data.txt
Assistant
Absolutely, I can help you add a highscore scoreboard to your Python game and store the leaderboard in a file called Data.txt. However, I'll need to see your current game code to integrate the scoreboard properly.
Could you please provide your current game code? Once you share it, I can help you implement the highscore functionality!
User
Assistant
import random
import time
import os
This gives the print a typewriter effect.
def typewriter_print(text, delay=0.03):
for char in text:
print(char, end='', flush=True)
time.sleep(delay)
print()
difficulty = input("Choose difficulty (easy, medium, hard): ").lower()
while difficulty not in words_list:
typewriter_print("❌ Invalid choice.")
difficulty = input("Choose difficulty (easy, medium, hard): ").lower()
return random.choice(words_list[difficulty])
Shows the rules to the player.
def show_rules():
typewriter_print("\n📜 Game Rules:")
typewriter_print("1️⃣ Guess the secret word one letter at a time.")
typewriter_print("2️⃣ You have 10 incorrect attempts.")
typewriter_print("3️⃣ Good luck!\n")
Got it! I'll extend your code to include a highscore leaderboard stored in Data.txt. The highscores will be based on how many tries are left when the player wins (or the score you decide to assign).
Here’s the plan:
When the player wins, ask for their name.
Save their score (e.g., number of tries left) into Data.txt.
On the main menu or after the game, show the top 5 highscores in descending order.
If Data.txt does not exist yet, create one.
I'll add the necessary functions and calls incrementally with your existing code. Since your code is incomplete (cut off at classic_game), I'll provide the extensions and example how to integrate:
Add these helper functions near the top:
python
def load_highscores(filename="Data.txt"):
highscores = []
if os.path.exists(filename):
with open(filename, "r") as f:
for line in f:
parts = line.strip().split(",")
if len(parts) == 2:
name, score = parts
try:
score = int(score)
highscores.append((name, score))
except ValueError:
pass
return highscores
def save_highscores(highscores, filename="Data.txt"):
with open(filename, "w") as f:
for name, score in highscores:
f.write(f"{name},{score}\n")
def add_highscore(name, score, filename="Data.txt"):
highscores = load_highscores(filename)
highscores.append((name, score))
highscores.sort(key=lambda x: x[1], reverse=True) # Sort descending by score
highscores = highscores[:5] # Keep top 5
save_highscores(highscores, filename)
def show_leaderboard():
print("\n🏆 Highscore Leaderboard 🏆")
highscores = load_highscores()
if not highscores:
print("No highscores yet, be the first!\n")
return
for i, (name, score) in enumerate(highscores, 1):
print(f"{i}. {name} - {score} tries left")
print()
Then, at the end of your successful game (inside classic_game where player wins), add:
python
def classic_game(secret_word):
tries = 10
guessed_letters = set()
correct_letters = set(secret_word)
while tries > 0:
print("\nSecret word: ", end="")
display_word = [letter if letter in guessed_letters else '_' for letter in secret_word]
print(" ".join(display_word))
print(f"Tries left: {tries}")
guess = input("Guess a letter: ").lower()
if len(guess) != 1 or not guess.isalpha():
print("Please guess a single letter.")
continue
if guess in guessed_letters:
print("You already guessed that letter.")
continue
guessed_letters.add(guess)
if guess in correct_letters:
print("Good guess!")
if correct_letters.issubset(guessed_letters):
print(f"\n🎉 Congratulations! You guessed the word: {secret_word}")
print(f"You had {tries} tries left.")
# Add to highscores
name = input("Enter your name for the leaderboard: ").strip()
if name:
add_highscore(name, tries)
else:
print("No name entered. Score not added.")
# Show leaderboard
show_leaderboard()
break
else:
print("Wrong guess.")
tries -= 1
bomb_stages(tries)
else:
print(f"\n💥 Boom! You lost. The word was: {secret_word}\n")
Finally, at the start, after showing the title screen and welcome message, you might want to show the leaderboard:
This keeps the highscore engine pretty simple — the score is the number of tries left when the player wins, and the leaderboard stores the top 5 largest tries left.
If you want me to integrate this with other game modes or extend functionality, just let me know!