Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #requires vlc player
- #feel free to remove elements that you don't need
- import random
- import time
- import tkinter
- from tkinter import messagebox
- import vlc
- import os
- #These import all the extras I need in the program
- global i
- #Allows to use the variable throughout all the code
- i = 0
- #This allows my while loop to work to restart the program if the user wants to
- global clear
- clear = lambda:os.system('cls')
- while i == 0:
- #Makes a loop I can return to
- def readScripts():
- #Defines the portion of code to run in Main()
- with open("songlist.txt", "r") as f:
- #Opens the .txt file to read as the variable 'f'
- songList = [line.rstrip('\n') for line in f]
- #Strips the song list down into lines
- f.close()
- #Closes the file
- with open("artistlist.txt", "r") as f:
- artistList = [line.rstrip('\n') for line in f]
- f.close()
- with open("users.txt", "r") as f:
- users = [line.rstrip('\n') for line in f]
- f.close()
- with open("pass.txt", "r") as f:
- passwords = [line.rstrip('\n') for line in f]
- f.close()
- return songList, artistList, users, passwords
- def enterDetails(users, passwords):
- #Another definition
- global enterUser
- #Another global variable
- userAlready = messagebox.askyesno("Account?","Do you already have an account?")
- #Creates a message box asking if they have got an account, returning a boolean
- if userAlready == True:
- #If the user presses 'Yes'...
- enterUser = input("Enter your username\n> ").lower()
- #Gets the username and puts it in lower case
- if enterUser in users:
- #if the user is in the users.txt document...
- enterPass = str(input("Enter your password\n> "))
- #Gets the password
- userPos = users.index(enterUser)
- #Creates a variable and stores the number of lines in users
- if enterPass in passwords:
- #if the passwrod entered is in pass.txt...
- passPos = passwords.index(enterPass)
- #indexes the passwords
- if userPos == passPos:
- #if the 2 numbers are the same
- print ("You have successfully logged in", enterUser)
- time.sleep(5)
- clear()
- #log them in
- return True
- #moves on to the next definition
- else:
- #else...
- print("You have entered the wrong password, please try again!")
- #if the password was wrong, ask them to re enter the user and password and restarts program
- time.sleep(5)
- clear()
- else:
- #else...
- print("We couldn't find your password, please try again!")
- #if the password isn't in pass.txt, ask them to re enter user and password and resets program
- time.sleep(5)
- clear()
- else:
- #else...
- retryUser = messagebox.askyesno("Retry?","We couldn't find your username, would you like to retry?")
- #asks them if they want to try another username
- if retryUser == True:
- #if they do...
- i = 0
- #returns i as 0, restarting the loop
- time.sleep(5)
- clear()
- else:
- #else...
- quit()
- #it quits the program
- return False
- #returns false (I believe this is obselete)
- else:
- #else if they don't have a user
- file = open('users.txt','a')
- #open users.txt as 'amend'
- newUser = input("Please enter username\n").lower()
- #asks for a new username and puts it in lower case
- file.write("\n" + newUser)
- #writes the new user on a new line
- file.close()
- #closes the file
- f = 0
- #defines f as 0
- while f == 0:
- #creates a loop
- file = open('pass.txt','a')
- #opens pass.txt as 'amend'
- newPass = input("Please enter password\n")
- #asks for a new password
- confirmPass = input("Please confirm password\n")
- #asks for comformation
- if newPass == confirmPass:
- #if they are the same...
- file.write("\n" + newPass)
- #write the password to the file
- file.close()
- #close the file
- print("New user created! Please restart the program and log in with your credidentials")
- #prints text
- quit()
- #quits the program
- else:
- #else...
- print("Passwords didn't match, please try again or use a different password.")
- #tells them the passwords don't match and restarts the password loop
- f == 0
- time.sleep(5)
- clear()
- #sets f to 0
- def selectMusic(songList, artistList):
- #defines the code and imports songlist and artistlist
- hiddenSong = random.choice(songList)
- #randomly choses a song
- hiddenSongPosition = songList.index(hiddenSong)
- #indexes the song (read above for index definition)
- hiddenArtist = artistList[hiddenSongPosition]
- #grabs the artist in the same position as the song and stores it
- return hiddenSong, hiddenArtist
- #returns the variables
- def load(hiddenSong, hiddenArtist):
- #defines the code and imports hiddensong and hiddenartist
- word_guessed = []
- #makes the variable blank
- hiddenSongLetter = hiddenSong.split()
- #splits the song into individual letters
- songLetters = messagebox.askyesno("Hint?","Would you like to see the first letter of each word in the song?")
- #asks if they want to see the first letter of each song
- if songLetters == True:
- #if they do...
- print("The artist is ", hiddenArtist, "and the song starts with:")
- #print the text
- for word in hiddenSongLetter:
- #for each word in the song name
- print(word[0])
- #print the first letter
- for letter in hiddenSong:
- #for each letter in the song
- if letter == " ":
- #if the letter is a space
- word_guessed.append(" / ")
- #change the spaces to /s
- else:
- #else
- word_guessed.append("-")
- #change the letters to -s
- for letter in word_guessed:
- print (letter, end = " ")
- return hiddenSong
- #returns the variables
- else:
- #else if they don't want the first letters...
- print("The artist is ", hiddenArtist)
- #print the artist name
- for letter in hiddenSong:
- #the rest of this is the same as above
- if letter == " ":
- word_guessed.append(" / ")
- else:
- word_guessed.append("-")
- for letter in word_guessed:
- print (letter, end = " ")
- return hiddenSong
- def play(hiddenSong, s):
- #defines the code and brings in hiddensong and s
- tries = 0
- #defines tries as 0
- while True:
- #whilst it's true...
- userGuess = str(input("\nWhat is the name of the song? Type 'hint' to hear 5 seconds of the song!\n> ")).lower()
- #get the guess and store it in lower case
- if userGuess == 'hint' and tries == 0:
- #if they want a hint and have 0 tries...
- p = vlc.MediaPlayer("C:/Users/Ryan/Documents/Comp Science Project/Songs/" + hiddenSong + ".mp3")
- #grab the song from the folder and open it in VLC
- p.play()
- #play the song
- time.sleep(5)
- #stop the code for 5 seconds
- p.stop()
- #stop the song
- tries = 0
- #set tries to 0
- True
- #return to the start of the loop
- elif userGuess == 'hint' and tries == 1:
- #this is the same as the above, but with 1 try instead of 0
- p = vlc.MediaPlayer("C:/Users/Ryan/Documents/Comp Science Project/Songs/" + hiddenSong + ".mp3")
- p.play()
- time.sleep(5)
- p.stop()
- tries == 1
- True
- else:
- #else...
- if userGuess == hiddenSong:
- #if the guess is the same as the hidden song...
- print("Checking our database...")
- #print checking database
- time.sleep(0.5)
- #pause the code for 0.5 seconds
- print("You are right!")
- #print that they're right
- if tries == 0:
- #if the tries are 0...
- s = s + 3
- #set the score to whatever it is + 3
- print("You got 3 points! You have", s, "points in total.")
- #print that they got 3 points and print their total points
- time.sleep(0.5)
- #pause it for 0.5 seconds
- clear()
- return s, True
- #return the score and to the start of the loop
- elif tries == 1:
- #else if the tries are 1...
- s = s + 1
- #add 1 to the score
- print("You got 1 point! You have", s, "points in total.")
- #print that they got 1 point and print their total
- time.sleep(0.5)
- #pause for 0.5 seconds
- clear()
- return s, True
- #return the score and to the start of the loop
- else:
- #else...
- tries = tries + 1
- #it adds a try
- if tries == 2:
- #if the tries are 2...
- print("Checking our database...")
- #prints checking the database
- time.sleep(0.5)
- #pauses for half a second
- print("Game over! You got", s, "points!")
- #prints that they lost and their score
- print("The answer was:", hiddenSong)
- #prints the song name
- time.sleep(5)
- clear()
- return s, False
- #returns the score and 'False'
- break
- def highScore(s, enterUser):
- #defines the code and graabs s and enteruser
- file = open('scores.txt','a')
- #opens scores.txt as apend
- userName = enterUser ;score = s
- #sets username to enteruser's value and score to s's value
- file.write(userName + " " + str(score) + "\n")
- #writes the username and the score to the file
- file.close()
- #closes the file
- file = open('scores.txt').readlines()
- #reads the lines in scores.txt
- scores_tuples = []
- #sets scores_tuples blank
- for line in file:
- #for each line in the file...
- name, score = line.split()[0], float(line.split()[1])
- #splits the score and the username
- scores_tuples.append((name,score))
- #changes scores_tuples to the user and the password
- scores_tuples.sort(key=lambda t: t[1], reverse=True)
- #sorts the file from the lowest score to the highest
- print("HIGHSCORES\n")
- #prints highscores
- for i, (name, score) in enumerate(scores_tuples[:5]):
- #gets the top 5 scores
- print("{}. Score:{} - Player:{}".format(i+1, score, name))
- #prints the top 5 scores
- restart = messagebox.askyesno("Restart?","Do you want to retry?")
- #asks if they want to restart the game
- if restart == True:
- #if they do
- i = 0
- time.sleep(5)
- clear()
- #restarts the loop
- else:
- #else...
- print("ok")
- #prints ok
- time.sleep(2)
- #waits for 2 seconds
- quit()
- #quits the program
- def main():
- #defines main
- songList, artistList, users, passwords = readScripts()
- #reads the scripts at the top
- s = 0
- #sets s to 0
- print("Welcome to guess the song! Made by Ryan Hodder")
- #prints the first line
- time.sleep(1)
- #waits for a second
- success = enterDetails(users, passwords)
- #starts the enterdetails code
- if success == True:
- #if they log in successfully...
- while True:
- #whilst it's true
- print ('''Rules:
- 1) Only the start of each word in the song must be a capital
- 2) There cannot be a space after the name
- That's all!''')
- #prints the rules
- hiddenSong, hiddenArtist = selectMusic(songList, artistList)
- #gets the 'selectmusic' code
- load(hiddenSong, hiddenArtist)
- #plays the 'load' code
- s, win = play(hiddenSong, s)
- #s and win are defined by what happens during the 'play' code
- if win == False:
- #if they don't win
- viewScores = messagebox.askyesno("View Scores?","Would you like to view the scores?")
- #ask if they want to view the scores
- if viewScores == True:
- #if they do...
- highScore(s, enterUser)
- #runs the high scores script
- else:
- #if they don't...
- restart = messagebox.askyesno("Restart?","Do you want to retry?")
- #ask if they want to retry
- if restart == True:
- #if they do...
- i = 0
- time.sleep(5)
- clear()
- #restart the loop
- else:
- #if they don't...
- print("ok")
- #print ok
- time.sleep(2)
- #pause the code for 2 seconds
- quit()
- #quits the program
- break
- main()
- #runs the 'main' script, starting the process.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement