Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- try:
- rawSongs = open("songs.txt").readlines()
- rawUsers = open("users.txt").readlines()
- rawHighscores = open("highscores.txt").readlines()
- except:
- print("At least one file is missing! Make sure songs.txt, users.txt and highscores.txt are all present in the working directory before running this program.")
- raise SystemExit
- songs = []
- for i in rawSongs:
- fields = i.split(",")
- title = fields[0]
- artist = fields[1].rstrip("\n")
- songs.append([title, artist])
- users = []
- for i in rawUsers:
- fields = i.split(",")
- username = fields[0]
- password = fields[1].rstrip("\n")
- users.append([username, password])
- highscores = []
- for i in rawHighscores:
- fields = i.split(",")
- score = int(fields[0])
- name = fields[1].rstrip("\n")
- highscores.append([score, name])
- # User validation
- userDetails = None
- while True:
- userInput = input("Please enter your username:\n")
- for i in users:
- if i[0] == userInput:
- userDetails = i
- break
- else:
- print("Username not found!")
- continue
- break
- while True:
- passInput = input("Please enter your password:\n")
- if passInput == userDetails[1]:
- print("Successfully logged in as {}!".format(userDetails[0]))
- break
- else:
- print("Incorrect password!")
- # Main game loop
- usedSongs = []
- curSong = None
- allSongsUsed = False
- userScore = 0
- while True:
- while True:
- temp = random.choice(songs)
- if temp not in usedSongs:
- usedSongs.append(temp)
- curSong = temp
- break
- elif len(usedSongs) == len(songs):
- print("All songs used!")
- allSongsUsed = True
- break
- if allSongsUsed:
- break
- initials = []
- for i in curSong[0].split():
- initials.append(i[0] + ("_" * (len(i) - 1)))
- scoreAdd = 3
- while True:
- print("Song: {} by {}".format(" ".join(initials), curSong[1]))
- guess = input("Please enter your guess at the song title:\n")
- if guess.upper() == curSong[0].upper():
- print("Correct!")
- userScore += scoreAdd
- print("Your score is now {}".format(userScore))
- break
- else:
- if scoreAdd == 3:
- print("Incorrect, please try again")
- scoreAdd = 1
- else:
- print("Game over! The correct answer was {}".format(curSong[0]))
- scoreAdd = 0
- print("Your score: {}".format(userScore))
- break
- if scoreAdd == 0:
- break
- highscores.append([userScore, userDetails[0]])
- sortedHighscores = sorted(highscores, key = lambda x: x[0], reverse = True)
- print("Highscores:")
- if len(sortedHighscores) >= 5:
- for i in sortedHighscores[0:5]:
- print("{} by {}".format(*i))
- else:
- for i in sortedHighscores:
- print("{} by {}".format(*i))
- newRawHighscores = []
- for i in sortedHighscores:
- formatted = [str(i[0]), i[1]]
- new = ",".join(formatted)
- newRawHighscores.append(new)
- newToWrite = "\n".join(newRawHighscores)
- newHighscores = open("highscores.txt", "w")
- newHighscores.write(newToWrite)
- newHighscores.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement