Advertisement
ItIsShadow

*FULL OCR GCSE SONG GAME SCRIPT (PYTHON)*

Jan 24th, 2020
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 15.72 KB | None | 0 0
  1. #requires vlc player
  2. #feel free to remove elements that you don't need
  3.  
  4. import random
  5. import time
  6. import tkinter
  7. from tkinter import messagebox
  8. import vlc
  9. import os
  10. #These import all the extras I need in the program
  11.  
  12. global i
  13. #Allows to use the variable throughout all the code
  14. i = 0
  15. #This allows my while loop to work to restart the program if the user wants to
  16. global clear
  17. clear = lambda:os.system('cls')
  18.  
  19. while i == 0:
  20.     #Makes a loop I can return to
  21.     def readScripts():
  22.         #Defines the portion of code to run in Main()
  23.         with open("songlist.txt", "r") as f:
  24.             #Opens the .txt file to read as the variable 'f'
  25.             songList = [line.rstrip('\n') for line in f]
  26.             #Strips the song list down into lines
  27.             f.close()
  28.             #Closes the file
  29.         with open("artistlist.txt", "r") as f:
  30.             artistList = [line.rstrip('\n') for line in f]
  31.             f.close()
  32.         with open("users.txt", "r") as f:
  33.             users = [line.rstrip('\n') for line in f]
  34.             f.close()
  35.         with open("pass.txt", "r") as f:
  36.             passwords = [line.rstrip('\n') for line in f]
  37.             f.close()
  38.         return songList, artistList, users, passwords
  39.  
  40.  
  41.     def enterDetails(users, passwords):
  42.         #Another definition
  43.         global enterUser
  44.         #Another global variable
  45.         userAlready = messagebox.askyesno("Account?","Do you already have an account?")
  46.         #Creates a message box asking if they have got an account, returning a boolean
  47.         if userAlready == True:
  48.             #If the user presses 'Yes'...
  49.             enterUser = input("Enter your username\n> ").lower()
  50.             #Gets the username and puts it in lower case
  51.             if enterUser in users:
  52.                 #if the user is in the users.txt document...
  53.                 enterPass = str(input("Enter your password\n> "))
  54.                 #Gets the password
  55.                 userPos = users.index(enterUser)
  56.                 #Creates a variable and stores the number of lines in users
  57.                 if enterPass in passwords:
  58.                     #if the passwrod entered is in pass.txt...
  59.                     passPos = passwords.index(enterPass)
  60.                     #indexes the passwords  
  61.                     if userPos == passPos:
  62.                         #if the 2 numbers are the same
  63.                         print ("You have successfully logged in", enterUser)
  64.                         time.sleep(5)
  65.                         clear()
  66.                         #log them in
  67.                         return True
  68.                     #moves on to the next definition
  69.                     else:
  70.                         #else...
  71.                         print("You have entered the wrong password, please try again!")
  72.                         #if the password was wrong, ask them to re enter the user and password and restarts program
  73.                         time.sleep(5)
  74.                         clear()
  75.                 else:
  76.                     #else...
  77.                     print("We couldn't find your password, please try again!")
  78.                     #if the password isn't in pass.txt, ask them to re enter user and password and resets program
  79.                     time.sleep(5)
  80.                     clear()
  81.             else:
  82.                 #else...
  83.                 retryUser = messagebox.askyesno("Retry?","We couldn't find your username, would you like to retry?")
  84.                 #asks them if they want to try another username
  85.                 if retryUser == True:
  86.                     #if they do...
  87.                     i = 0
  88.                     #returns i as 0, restarting the loop
  89.                     time.sleep(5)
  90.                     clear()
  91.                 else:
  92.                     #else...
  93.                     quit()
  94.                     #it quits the program
  95.             return False
  96.         #returns false (I believe this is obselete)
  97.         else:
  98.             #else if they don't have a user
  99.             file = open('users.txt','a')
  100.             #open users.txt as 'amend'
  101.             newUser = input("Please enter username\n").lower()
  102.             #asks for a new username and puts it in lower case
  103.             file.write("\n" + newUser)
  104.             #writes the new user on a new line
  105.             file.close()
  106.             #closes the file
  107.  
  108.             f = 0
  109.             #defines f as 0
  110.             while f == 0:
  111.                 #creates a loop
  112.                 file = open('pass.txt','a')
  113.                 #opens pass.txt as 'amend'
  114.                 newPass = input("Please enter password\n")
  115.                 #asks for a new password
  116.                 confirmPass = input("Please confirm password\n")
  117.                 #asks for comformation
  118.                 if newPass == confirmPass:
  119.                     #if they are the same...
  120.                     file.write("\n" + newPass)
  121.                     #write the password to the file
  122.                     file.close()
  123.                     #close the file
  124.                     print("New user created! Please restart the program and log in with your credidentials")
  125.                     #prints text
  126.                     quit()
  127.                     #quits the program
  128.                 else:
  129.                     #else...
  130.                     print("Passwords didn't match, please try again or use a different password.")
  131.                     #tells them the passwords don't match and restarts the password loop
  132.                     f == 0
  133.                     time.sleep(5)
  134.                     clear()
  135.                     #sets f to 0
  136.  
  137.  
  138.     def selectMusic(songList, artistList):
  139.         #defines the code and imports songlist and artistlist
  140.         hiddenSong = random.choice(songList)
  141.         #randomly choses a song
  142.         hiddenSongPosition = songList.index(hiddenSong)
  143.         #indexes the song (read above for index definition)
  144.         hiddenArtist = artistList[hiddenSongPosition]
  145.         #grabs the artist in the same position as the song and stores it
  146.         return hiddenSong, hiddenArtist
  147.     #returns the variables
  148.  
  149.  
  150.     def load(hiddenSong, hiddenArtist):
  151.         #defines the code and imports hiddensong and hiddenartist
  152.         word_guessed = []
  153.         #makes the variable blank
  154.         hiddenSongLetter = hiddenSong.split()
  155.         #splits the song into individual letters
  156.         songLetters = messagebox.askyesno("Hint?","Would you like to see the first letter of each word in the song?")
  157.         #asks if they want to see the first letter of each song
  158.         if songLetters == True:
  159.             #if they do...
  160.             print("The artist is ", hiddenArtist, "and the song starts with:")
  161.             #print the text
  162.             for word in hiddenSongLetter:
  163.                 #for each word in the song name
  164.                 print(word[0])
  165.                 #print the first letter
  166.             for letter in hiddenSong:
  167.                 #for each letter in the song
  168.                 if letter == " ":
  169.                     #if the letter is a space
  170.                     word_guessed.append(" / ")
  171.                     #change the spaces to /s
  172.                 else:
  173.                     #else
  174.                     word_guessed.append("-")
  175.                     #change the letters to -s
  176.             for letter in word_guessed:
  177.                 print (letter, end = " ")
  178.             return hiddenSong
  179.         #returns the variables
  180.         else:
  181.             #else if they don't want the first letters...
  182.             print("The artist is ", hiddenArtist)
  183.             #print the artist name
  184.             for letter in hiddenSong:
  185.                 #the rest of this is the same as above
  186.                 if letter == " ":
  187.                     word_guessed.append(" / ")      
  188.                 else:
  189.                     word_guessed.append("-")
  190.             for letter in word_guessed:
  191.                 print (letter, end = " ")
  192.             return hiddenSong
  193.  
  194.     def play(hiddenSong, s):
  195.         #defines the code and brings in hiddensong and s
  196.         tries = 0
  197.         #defines tries as 0
  198.         while True:
  199.             #whilst it's true...
  200.             userGuess = str(input("\nWhat is the name of the song? Type 'hint' to hear 5 seconds of the song!\n> ")).lower()
  201.             #get the guess and store it in lower case
  202.             if userGuess == 'hint' and tries == 0:
  203.                 #if they want a hint and have 0 tries...
  204.                 p = vlc.MediaPlayer("C:/Users/Ryan/Documents/Comp Science Project/Songs/" + hiddenSong + ".mp3")
  205.                 #grab the song from the folder and open it in VLC
  206.                 p.play()
  207.                 #play the song
  208.                 time.sleep(5)
  209.                 #stop the code for 5 seconds
  210.                 p.stop()
  211.                 #stop the song
  212.                 tries = 0
  213.                 #set tries to 0
  214.                 True
  215.                 #return to the start of the loop
  216.             elif userGuess == 'hint' and tries == 1:
  217.                 #this is the same as the above, but with 1 try instead of 0
  218.                 p = vlc.MediaPlayer("C:/Users/Ryan/Documents/Comp Science Project/Songs/" + hiddenSong + ".mp3")
  219.                 p.play()
  220.                 time.sleep(5)
  221.                 p.stop()
  222.                 tries == 1
  223.                 True
  224.             else:
  225.                 #else...
  226.                 if userGuess == hiddenSong:
  227.                     #if the guess is the same as the hidden song...
  228.                     print("Checking our database...")
  229.                     #print checking database
  230.                     time.sleep(0.5)
  231.                     #pause the code for 0.5 seconds
  232.                     print("You are right!")
  233.                     #print that they're right
  234.                     if tries == 0:
  235.                         #if the tries are 0...
  236.                         s = s + 3
  237.                         #set the score to whatever it is + 3
  238.                         print("You got 3 points! You have", s, "points in total.")
  239.                         #print that they got 3 points and print their total points
  240.                         time.sleep(0.5)
  241.                         #pause it for 0.5 seconds
  242.                         clear()
  243.                         return s, True
  244.                     #return the score and to the start of the loop
  245.                     elif tries == 1:
  246.                         #else if the tries are 1...
  247.                         s = s + 1
  248.                         #add 1 to the score
  249.                         print("You got 1 point! You have", s, "points in total.")
  250.                         #print that they got 1 point and print their total
  251.                         time.sleep(0.5)
  252.                         #pause for 0.5 seconds
  253.                         clear()
  254.                         return s, True
  255.                     #return the score and to the start of the loop
  256.                 else:
  257.                     #else...
  258.                     tries = tries + 1
  259.                     #it adds a try
  260.                     if tries == 2:
  261.                         #if the tries are 2...
  262.                         print("Checking our database...")
  263.                         #prints checking the database
  264.                         time.sleep(0.5)
  265.                         #pauses for half a second
  266.                         print("Game over! You got", s, "points!")
  267.                         #prints that they lost and their score
  268.                         print("The answer was:", hiddenSong)
  269.                         #prints the song name
  270.                         time.sleep(5)
  271.                         clear()
  272.                         return s, False
  273.                     #returns the score and 'False'
  274.                         break
  275.  
  276.  
  277.  
  278.     def highScore(s, enterUser):
  279.         #defines the code and graabs s and enteruser
  280.         file = open('scores.txt','a')
  281.         #opens scores.txt as apend
  282.         userName = enterUser ;score = s
  283.         #sets username to enteruser's value and score to s's value
  284.         file.write(userName + " " + str(score) + "\n")
  285.         #writes the username and the score to the file
  286.         file.close()
  287.         #closes the file
  288.  
  289.         file = open('scores.txt').readlines()
  290.         #reads the lines in scores.txt
  291.         scores_tuples = []
  292.         #sets scores_tuples blank
  293.         for line in file:
  294.             #for each line in the file...
  295.             name, score = line.split()[0], float(line.split()[1])
  296.             #splits the score and the username
  297.             scores_tuples.append((name,score))
  298.             #changes scores_tuples to the user and the password
  299.         scores_tuples.sort(key=lambda t: t[1], reverse=True)
  300.         #sorts the file from the lowest score to the highest
  301.         print("HIGHSCORES\n")
  302.         #prints highscores
  303.         for i, (name, score) in enumerate(scores_tuples[:5]):
  304.             #gets the top 5 scores
  305.             print("{}. Score:{} - Player:{}".format(i+1, score, name))
  306.             #prints the top 5 scores
  307.  
  308.         restart = messagebox.askyesno("Restart?","Do you want to retry?")
  309.         #asks if they want to restart the game
  310.         if restart == True:
  311.             #if they do
  312.              i = 0
  313.              time.sleep(5)
  314.              clear()
  315.              #restarts the loop
  316.         else:
  317.             #else...
  318.             print("ok")
  319.             #prints ok
  320.             time.sleep(2)
  321.             #waits for 2 seconds
  322.             quit()
  323.             #quits the program
  324.  
  325.  
  326.     def main():
  327.         #defines main
  328.         songList, artistList, users, passwords = readScripts()
  329.         #reads the scripts at the top
  330.         s = 0
  331.         #sets s to 0
  332.         print("Welcome to guess the song! Made by Ryan Hodder")
  333.         #prints the first line
  334.         time.sleep(1)
  335.         #waits for a second
  336.         success = enterDetails(users, passwords)
  337.         #starts the enterdetails code
  338.         if success == True:
  339.             #if they log in successfully...
  340.             while True:
  341.                 #whilst it's true
  342.                 print ('''Rules:                                                            
  343.    1) Only the start of each word in the song must be a capital
  344.    2) There cannot be a space after the name
  345.    That's all!''')
  346.                 #prints the rules
  347.                 hiddenSong, hiddenArtist = selectMusic(songList, artistList)
  348.                 #gets the 'selectmusic' code
  349.                 load(hiddenSong, hiddenArtist)
  350.                 #plays the 'load' code
  351.                 s, win = play(hiddenSong, s)
  352.                 #s and win are defined by what happens during the 'play' code
  353.                 if win == False:
  354.                     #if they don't win
  355.                     viewScores = messagebox.askyesno("View Scores?","Would you like to view the scores?")
  356.                     #ask if they want to view the scores
  357.                     if viewScores == True:
  358.                         #if they do...
  359.                         highScore(s, enterUser)
  360.                         #runs the high scores script
  361.                     else:
  362.                         #if they don't...
  363.                         restart = messagebox.askyesno("Restart?","Do you want to retry?")
  364.                         #ask if they want to retry
  365.                         if restart == True:
  366.                             #if they do...
  367.                              i = 0
  368.                              time.sleep(5)
  369.                              clear()
  370.                              #restart the loop
  371.                         else:
  372.                             #if they don't...
  373.                             print("ok")
  374.                             #print ok
  375.                             time.sleep(2)
  376.                             #pause the code for 2 seconds
  377.                             quit()
  378.                             #quits the program
  379.                         break
  380.     main()
  381.     #runs the 'main' script, starting the process.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement