Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This program runs the music game
- string32 = "\n"
- import tkinter
- from random import*
- from tkinter import *
- from hashlib import sha1
- def destroy_rule_window_play_game():
- # only want the rule game window to close the first time i run the function play_game
- Rule_game_window.destroy()
- play_game()
- def encrypt(username, password):
- # must encode the the username and password before hashing them
- # This then writes the hashed username and password to a file
- global password_checker
- hashob = sha1(username.encode())
- hasho = sha1(password.encode())
- hashed_username = hashob.hexdigest()
- hashed_password = hasho.hexdigest()
- password_checker = hashed_password
- with open("User_Password_NEA_Peter_Leyden.txt", "a")as g:
- g.write("\n")
- g.write(hashed_username)
- g.write("\n")
- g.write(hashed_password)
- g.write("\n")
- g.write("0\n")
- def check_login_information():
- # variable login_confirmed is used to run the Rule_game_window
- # this reads each line and checks if it is the hashed username
- # if a match is found it then checks the next line for the hashed password
- # if both are correct then it destroys the login window and opens the Rule_game_window
- # if either are incorrect then it changes the text to say that they had incorrect username or password
- global login_confirmed
- global number
- global password_checker
- user_username = username_entry_widget.get()
- user_password = password_entry_widget.get()
- login_confirmed = False
- line_in_file = False
- number = 0
- with open("User_Password_NEA_Peter_Leyden.txt", "r")as f:
- while line_in_file == False:
- number += 1
- file_ended = f.readline()
- if file_ended == "":
- line_in_file = True
- else:
- pass
- with open("User_Password_NEA_Peter_Leyden.txt", "r")as f:
- for x in range(0, number):
- username_checker = f.readline()
- user_username_encoded = sha1(user_username.encode())
- user_username_hashed = user_username_encoded.hexdigest()
- user_username_hashed = user_username_hashed + string32
- if username_checker == user_username_hashed:
- password_checker = f.readline()
- user_password_encoded = sha1(user_password.encode())
- user_password_hashed = user_password_encoded.hexdigest()
- user_password_hashed1 = user_password_hashed + string32
- if password_checker == user_password_hashed or password_checker == user_password_hashed1:
- login_communicator.config(text="You have now Logged In!", fg="deep pink")
- login_confirmed = True
- login_window.destroy()
- else:
- pass
- else:
- pass
- if login_confirmed == True:
- pass
- else:
- login_communicator.config(text="The Username or Password was incorrect", font="helvetica 20 bold",
- fg='green')
- def check_answer():
- global num_of_guesses_string
- global num_of_guesses
- global score
- global score_string
- global score_string_final
- global score_num_string
- global next_guess_button
- global guesses_string_num
- global answered_correctly
- global guess_button
- if answered_correctly is False:
- # adds 1 to number of guesses
- num_of_guesses += 1
- guesses_string_num = str(num_of_guesses)
- # gets the user's answer and converts it to lower case
- user_answer = song_entry.get()
- user_answer_lower = user_answer.lower()
- user_answer_lower = user_answer_lower + string32
- song_lower = song.lower()
- # changes number of guesses displayed to the correct amount of guesses
- # changes the string to be displayed on the window to the correct amount of guesses
- num_of_guesses_string_final = num_of_guesses_string + guesses_string_num
- num_of_guesses_label.config(text=num_of_guesses_string_final)
- if song_lower == user_answer_lower:
- guess_correct_label.pack()
- # adds 3 or 1 to score depending on the number of guesses they've had
- if num_of_guesses == 1:
- score += 3
- score_num_string = str(score)
- score_string_final = score_string + score_num_string
- score_label.config(text=score_string_final)
- next_guess_button.pack()
- guess_button.pack_forget()
- answered_correctly = True
- elif num_of_guesses == 2:
- score += 1
- score_num_string = str(score)
- score_string_final = score_string + score_num_string
- score_label.config(text=score_string_final)
- next_guess_button.pack()
- guess_button.pack_forget()
- answered_correctly = True
- # if they've had 2 guesses they are offered the chance to restart and score is reset
- elif num_of_guesses == 2:
- guess_correct_label.config(text='Game Over!')
- score_end_string = 'Your Score was '
- score_string = str(score)
- score_end_string_final= score_end_string + score_string
- score_label.config(text=score_end_string_final, font='helvetica 40 bold')
- song_explainer = 'The song was '
- song_explainer_final = song_explainer + song
- actual_song = tkinter.Label(play_window, text=song_explainer_final, font="helvetica 25 bold",
- bg='deep sky blue')
- view_high_scores_button = tkinter.Button(play_window, text="View high scores", bg='deep sky blue',
- font="helvetica 25 bold", command=view_high_scores)
- guess_correct_label.pack()
- try_again_button.pack()
- view_high_scores_button.pack()
- guess_button.pack_forget()
- actual_song.pack()
- def view_high_scores():
- with open("User_Password_NEA_Peter_Leyden.txt", "r")as f:
- high_scores_window = tkinter.Tk()
- username_password_high_score_list = [""]
- for x in range(0, 1000):
- file_entry = f.readline()
- if file_entry == password_checker:
- username_password_high_score_list.append(password_checker)
- old_high_score = f.readline()
- print(old_high_score)
- print(score)
- old_high_score_num = int(old_high_score)
- if old_high_score_num <= score:
- score_str = str(score)
- score_str = score_str+string32
- username_password_high_score_list.append(score_str)
- congratulation_new_high_score_label = tkinter.Label(high_scores_window, text="*NEW HIGH SCORE!!!!!*",
- font=("Comic Sans MS", 25, "bold"), bg='deep sky blue', fg='green')
- congratulation_new_high_score_label.pack()
- else:
- username_password_high_score_list.append(old_high_score)
- else:
- username_password_high_score_list.append(file_entry)
- print(username_password_high_score_list)
- with open("User_password_NEA_Peter_Leyden.txt", "w")as f:
- length_of_list = len(username_password_high_score_list)
- place_in_list = -1
- for x in range(0, length_of_list):
- place_in_list += 1
- list_entry = username_password_high_score_list[place_in_list]
- f.write(list_entry)
- with open("User_Password_NEA_Peter_Leyden.txt", "r")as f:
- high_score_list = [""]
- high_score_list.remove("")
- for x in range(0, 250):
- f.readline()
- f.readline()
- f.readline()
- high_score = f.readline()
- if high_score == '':
- pass
- else:
- high_score_num = int(high_score)
- high_score_list.append(high_score_num)
- print(high_score)
- high_scores_label = tkinter.Label(high_scores_window, text="High Scores!", font=("Comic Sans MS", 25, "bold"),
- bg='deep sky blue')
- high_scores_label.pack()
- high_score_list.sort(reverse=True)
- print(high_score_list)
- for x in range(0,5):
- high_score_display = high_score_list[x]
- print(high_score_display)
- high_score_num_label = tkinter.Label(high_scores_window, text=high_score_display, bg='deep sky blue')
- high_score_num_label.pack()
- high_scores_window.title("High Scores")
- high_scores_window.configure(background='deep sky blue')
- high_score1 = tkinter.Label(high_scores_window)
- high_scores_window.mainloop()
- def play_game_restart():
- # restarts the game if the game ended
- play_window.destroy()
- play_game()
- def label_that_change():
- # Only needs to be run once so it is in a seperate function
- global song_first_letter_label
- global play_artist_label
- global num_of_guesses_label
- song_first_letter_label = tkinter.Label(play_window, text=list_of_first_letters, bg='deep sky blue',
- font=("Comic Sans MS", 25, "bold"))
- play_artist_label = tkinter.Label(play_window, text=artist, bg='deep sky blue', fg='green',
- font=("Comic Sans MS", 25, "bold"))
- num_of_guesses_label = tkinter.Label(play_window, text=num_of_guesses_string_final, bg='deep sky blue',
- font='helvetica 15 bold')
- def generate_song3():
- global artist
- global song
- global list_of_first_letters
- global score_string_final
- global num_of_guesses_string
- global score_string
- global play_artist_label
- global num_of_guesses_label
- global num_of_guesses
- global song_first_letter_label
- global num_of_guesses_string_final
- global answered_correctly
- # reads the number of lines divides by 2 to get the number of songs
- num_of_lines = -1
- answered_correctly = False
- line = " "
- num_of_guesses = 0
- with open("Song_List_NEA_Peter_Leyden.txt", 'r')as f:
- while line != '':
- line = f.readline()
- num_of_lines += 1
- num_of_songs = num_of_lines / 2
- num_of_songs = int(num_of_songs)
- random_song = randint(0, num_of_songs)
- random_song = random_song - 1
- random_song = random_song * 2
- # reads the random song from the list
- with open("Song_List_NEA_Peter_Leyden.txt", 'r')as f:
- for x in range(0, random_song):
- f.readline()
- artist = f.readline()
- song = f.readline()
- # splits the song into the first letters
- song_split = song.split(' ')
- song_number_of_words = len(song_split)
- num_of_words = 0
- list_of_first_letters = ['']
- for x in range(0, song_number_of_words):
- word_of_song = song_split[num_of_words]
- num_of_words += 1
- first_letter_of_word_song = word_of_song[0]
- capital_first_letter_of_word_song = first_letter_of_word_song.upper()
- list_of_first_letters.append(capital_first_letter_of_word_song)
- list_of_first_letters.remove("")
- # changes the guesses string and sets it
- num_of_guesses_string = "Guesses: "
- guesses_string_num = str(num_of_guesses)
- num_of_guesses_string_final = num_of_guesses_string + guesses_string_num
- score_string = 'Score: '
- score_num_string = str(score)
- score_string_final = score_string + score_num_string
- def generate_song():
- # This is only needed for the first time the program runs
- generate_song3()
- label_that_change()
- def generate_song2():
- # generates the new song if they get it correct
- global song_first_letter_label
- global play_artist_label
- global num_of_guesses_label
- global guess_correct_label
- global next_guess_button
- global guess_button
- generate_song3()
- # changes the Labels to the new score/artist/first letter of song
- song_first_letter_label.config(text=list_of_first_letters)
- play_artist_label.config(text=artist)
- num_of_guesses_label.config(text=num_of_guesses_string_final)
- # deletes previous song in the text box
- song_entry.delete(0, 'end')
- # hides the label and button that the user uses if they get it correct
- guess_correct_label.pack_forget()
- next_guess_button.pack_forget()
- guess_button.pack()
- def play_game():
- global num_of_guesses_string
- global song
- global song_entry
- global guess_correct_label
- global num_of_guesses
- global score
- global num_of_guesses_label
- global play_window
- global score_string
- global score_string_final
- global score_label
- global try_again_button
- global Rule_game_window
- global next_guess_button
- global song_first_letter_label
- global play_artist_label
- global guess_button
- # sets variables to 0
- score = 0
- num_of_guesses = 0
- # creates the play_window so that the user can play the game
- play_window = Tk()
- play_window.title("Music Game!")
- play_window.configure(background='deep sky blue')
- play_gif = tkinter.PhotoImage(file='Music.gif')
- play_gif_label = tkinter.Label(play_window, image=play_gif, bg='black')
- play_welcome = tkinter.Label(play_window, text='Welcome to the Music Game!', bg="deep sky blue",
- font=("Comic Sans MS", 32, "bold"))
- # generates song to be displayed on
- generate_song()
- artist_label = tkinter.Label(play_window, text='Artist: ', bg="deep sky blue", font='helvetica 15 bold')
- song_label = tkinter.Label(play_window, text="First letter of each word of the song: ",
- bg='deep sky blue', font="helvetica 15 bold")
- song_entry = tkinter.Entry(play_window)
- guess_button = tkinter.Button(play_window, text='Guess', bg='deep sky blue', font='helvetica 20 bold',
- command=check_answer)
- guess_correct_label = tkinter.Label(play_window, text="Correct!!!", bg='deep sky blue',
- fg='green', font=("Comic Sans MS", 25, "bold"))
- score_label = tkinter.Label(play_window, text=score_string_final, bg="deep sky blue", font='helvetica 20 bold')
- try_again_button = tkinter.Button(play_window, text="Try again", bg='deep sky blue',
- font='helvetica 20 bold', command=play_game_restart)
- next_guess_button = tkinter.Button(play_window, text="Next Question", bg="deep sky blue",
- font='helvetica 20 bold', command=generate_song2)
- # packs all of the relevant label/button into the tkinter window
- play_welcome.pack()
- #play_gif_label.pack()
- score_label.pack()
- artist_label.pack()
- play_artist_label.pack()
- song_label.pack()
- song_first_letter_label.pack()
- num_of_guesses_label.pack()
- song_entry.pack()
- guess_button.pack()
- # doesn't pack these because they only appear after the user has guessed
- guess_correct_label.pack_forget()
- try_again_button.pack_forget()
- next_guess_button.pack_forget()
- # runs the window
- play_window.mainloop()
- login_confirmed = False
- with open("User_Password_NEA_Peter_Leyden.txt", "a")as f:
- # if file is empty then this will be your first entry so that the user can set up the program
- f.write('')
- with open("User_Password_NEA_Peter_Leyden.txt", "r")as f:
- # if the file is empty then the user will be able to set up their program
- Check_if_File_Empty = f.read()
- if Check_if_File_Empty == '':
- username = input("You are our first customer Please input your username: ")
- password = input("Please input your password: ")
- encrypt(username, password)
- def new_user():
- # adds new user to the list of users
- global sign_up_username_entry
- global sign_up_password_entry
- global sign_up_password2_entry
- global number
- global failed_already
- failed_already = 0
- login_Welcome.pack_forget()
- login_gif_label.pack_forget()
- username_widget.pack_forget()
- username_entry_widget.pack_forget()
- password_widget.pack_forget()
- password_entry_widget.pack_forget()
- login_button.pack_forget()
- sign_up_button.pack_forget()
- login_communicator.pack_forget()
- sign_up_title = tkinter.Label(login_window, text='Welcome to the sign up', font=("Comic Sans MS", 32, "bold"),
- bg = 'deep sky blue')
- sign_up_label = tkinter.Label(login_window, text='Please enter your username and password below',
- font='helvetica 15 bold', bg='deep sky blue')
- sign_up_username_label = tkinter.Label(login_window, text='Enter Username:', font='helvetica 15 bold',
- bg='deep sky blue')
- sign_up_username_entry = tkinter.Entry(login_window)
- sign_up_password_label = tkinter.Label(login_window, text='Enter Password:', font='helvetica 15 bold',
- bg='deep sky blue')
- sign_up_password_entry = tkinter.Entry(login_window, show='*')
- sign_up_password2_label = tkinter.Label(login_window, text='Re-Enter Password:', font='helvetica 15 bold',
- bg='deep sky blue')
- sign_up_password2_entry = tkinter.Entry(login_window, show='*')
- sign_up_confirm_button = tkinter.Button(login_window, text='Sign Up', bg='deep sky blue', font='helvetica 20 bold',
- command=add_new_user)
- sign_up_title.pack()
- sign_up_label.pack()
- sign_up_username_label.pack()
- sign_up_username_entry.pack()
- sign_up_password_label.pack()
- sign_up_password_entry.pack()
- sign_up_password2_label.pack()
- sign_up_password2_entry.pack()
- sign_up_confirm_button.pack()
- line_in_file = False
- number = 0
- with open("User_Password_NEA_Peter_Leyden.txt", "r")as f:
- while line_in_file == False:
- number += 1
- file_ended = f.readline()
- if file_ended == "":
- line_in_file = True
- else:
- pass
- def new_user_finished():
- global login_confirmed
- login_window.destroy()
- login_confirmed = True
- encrypt(username, password)
- def add_new_user():
- global password
- global username
- global failed_already
- password = sign_up_password_entry.get()
- password2 = sign_up_password2_entry.get()
- username = sign_up_username_entry.get()
- sign_up_fail_label = tkinter.Label(login_window, text="The passwords weren't the same try again",
- bg='deep sky blue', font=("Comic Sans MS", 20, "bold"))
- if password == password2:
- sign_up_fail_label.pack_forget()
- sign_up_confirmed_label = tkinter.Label(login_window, text='You have siged up!', bg='deep sky blue',
- font=("Comic Sans MS", 32, "bold"))
- sign_up_continue_button = tkinter.Button(login_window, text='Continue', bg='deep sky blue',
- font='helvetica 20 bold', command=new_user_finished)
- sign_up_confirmed_label.pack()
- sign_up_continue_button.pack()
- else:
- if failed_already == 0:
- sign_up_fail_label.pack()
- failed_already = 1
- # defines all of the relevant information for the login window
- login_window = tkinter.Tk()
- login_window.title("Login")
- login_Welcome = tkinter.Label(login_window, text='Welcome to the Music Game!',
- bg='deep sky blue', font=("Comic Sans MS", 32, "bold"))
- username_entry_widget = tkinter.Entry(login_window)
- # makes the password secret from casual observers
- password_entry_widget = tkinter.Entry(login_window, show='*')
- sign_up_button = tkinter.Button(login_window, text="Sign Up", bg='deep sky blue', command=new_user)
- login_gif = tkinter.PhotoImage(file='Music.gif')
- login_gif_label = tkinter.Label(login_window, image=login_gif, bg='black')
- username_widget = tkinter.Label(login_window, text='Username:', bg='deep sky blue', font='helvetica 20 bold')
- password_widget = tkinter.Label(login_window, text='Password:', bg='deep sky blue', font='helvetica 20 bold')
- login_button = tkinter.Button(login_window, text='Submit', bg='deep sky blue', command=check_login_information)
- login_communicator = tkinter.Label(login_window, text='You are currently logged out',
- bg="deep sky blue", fg='Green', font='helvetica 25 bold')
- login_window.configure(background='deep sky blue')
- # packs all of the relevant information into the login_window
- login_Welcome.pack()
- login_gif_label.pack()
- username_widget.pack()
- username_entry_widget.pack()
- password_widget.pack()
- password_entry_widget.pack()
- login_button.pack()
- sign_up_button.pack()
- login_communicator.pack()
- login_window.mainloop()
- if login_confirmed == True:
- Rule_game_window = tkinter.Tk()
- Rule_game_window.title("Music Game Rules")
- Rule_game_window.configure(background='deep sky blue')
- Rule_game_Welcome = tkinter.Label(Rule_game_window, text='Welcome to the Music Game!!!!', bg='deep sky blue',
- font=("Comic Sans MS", 32, "bold"))
- Rule_game_rule = tkinter.Label(Rule_game_window, text="The rules of the game are:", font='helvetica 25 bold',
- bg="deep sky blue", fg='Green')
- Rule_game_rules1 = tkinter.Label(Rule_game_window, text="–A song will be Chosen at random,", bg="deep sky blue")
- Rule_game_rules2 = tkinter.Label(Rule_game_window, text="–The name of artist will be given,", bg="deep sky blue")
- Rule_game_rules3 = tkinter.Label(Rule_game_window, text="–The first letter of each word in the song will be given,",
- bg="deep sky blue")
- Rule_game_rules4 = tkinter.Label(Rule_game_window, text="–You will have 2 Guesses,", bg="deep sky blue")
- Rule_game_rules5 = tkinter.Label(Rule_game_window, text="–If you get it on the first guess you will get 3 points,",
- bg="deep sky blue")
- Rule_game_rules6 = tkinter.Label(Rule_game_window, text="–If you get it on the second guess you will get 1 point,",
- bg="deep sky blue")
- Rule_game_rules7 = tkinter.Label(Rule_game_window, text="–If you fail both guesses the game is over.",
- bg="deep sky blue")
- Rule_game_play = tkinter.Button(Rule_game_window, text="Play Game!", bg="deep sky blue",
- font=("Comic Sans MS", 20, "bold"), command=destroy_rule_window_play_game)
- Rule_game_gif = tkinter.PhotoImage(file='Music.gif')
- Rule_game_gif_label = tkinter.Label(Rule_game_window, image=Rule_game_gif, bg='black')
- Rule_game_Welcome.pack()
- Rule_game_gif_label.pack()
- Rule_game_rule.pack()
- Rule_game_rules1.pack()
- Rule_game_rules2.pack()
- Rule_game_rules3.pack()
- Rule_game_rules4.pack()
- Rule_game_rules5.pack()
- Rule_game_rules6.pack()
- Rule_game_rules7.pack()
- Rule_game_play.pack()
- Rule_game_window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement