Advertisement
Guest User

Untitled

a guest
Apr 29th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.42 KB | None | 0 0
  1. # This program runs the music game
  2. string32 = "\n"
  3. import tkinter
  4. from random import*
  5. from tkinter import *
  6. from hashlib import sha1
  7. def destroy_rule_window_play_game():
  8. # only want the rule game window to close the first time i run the function play_game
  9. Rule_game_window.destroy()
  10. play_game()
  11. def encrypt(username, password):
  12. # must encode the the username and password before hashing them
  13. # This then writes the hashed username and password to a file
  14. global password_checker
  15. hashob = sha1(username.encode())
  16. hasho = sha1(password.encode())
  17. hashed_username = hashob.hexdigest()
  18. hashed_password = hasho.hexdigest()
  19. password_checker = hashed_password
  20. with open("User_Password_NEA_Peter_Leyden.txt", "a")as g:
  21. g.write("\n")
  22. g.write(hashed_username)
  23. g.write("\n")
  24. g.write(hashed_password)
  25. g.write("\n")
  26. g.write("0\n")
  27. def check_login_information():
  28. # variable login_confirmed is used to run the Rule_game_window
  29. # this reads each line and checks if it is the hashed username
  30. # if a match is found it then checks the next line for the hashed password
  31. # if both are correct then it destroys the login window and opens the Rule_game_window
  32. # if either are incorrect then it changes the text to say that they had incorrect username or password
  33. global login_confirmed
  34. global number
  35. global password_checker
  36. user_username = username_entry_widget.get()
  37. user_password = password_entry_widget.get()
  38. login_confirmed = False
  39. line_in_file = False
  40. number = 0
  41. with open("User_Password_NEA_Peter_Leyden.txt", "r")as f:
  42. while line_in_file == False:
  43. number += 1
  44. file_ended = f.readline()
  45. if file_ended == "":
  46. line_in_file = True
  47. else:
  48. pass
  49. with open("User_Password_NEA_Peter_Leyden.txt", "r")as f:
  50. for x in range(0, number):
  51. username_checker = f.readline()
  52. user_username_encoded = sha1(user_username.encode())
  53. user_username_hashed = user_username_encoded.hexdigest()
  54. user_username_hashed = user_username_hashed + string32
  55. if username_checker == user_username_hashed:
  56. password_checker = f.readline()
  57. user_password_encoded = sha1(user_password.encode())
  58. user_password_hashed = user_password_encoded.hexdigest()
  59. user_password_hashed1 = user_password_hashed + string32
  60. if password_checker == user_password_hashed or password_checker == user_password_hashed1:
  61. login_communicator.config(text="You have now Logged In!", fg="deep pink")
  62. login_confirmed = True
  63. login_window.destroy()
  64. else:
  65. pass
  66. else:
  67. pass
  68. if login_confirmed == True:
  69. pass
  70. else:
  71. login_communicator.config(text="The Username or Password was incorrect", font="helvetica 20 bold",
  72. fg='green')
  73. def check_answer():
  74. global num_of_guesses_string
  75. global num_of_guesses
  76. global score
  77. global score_string
  78. global score_string_final
  79. global score_num_string
  80. global next_guess_button
  81. global guesses_string_num
  82. global answered_correctly
  83. global guess_button
  84. if answered_correctly is False:
  85. # adds 1 to number of guesses
  86. num_of_guesses += 1
  87. guesses_string_num = str(num_of_guesses)
  88. # gets the user's answer and converts it to lower case
  89. user_answer = song_entry.get()
  90. user_answer_lower = user_answer.lower()
  91. user_answer_lower = user_answer_lower + string32
  92. song_lower = song.lower()
  93. # changes number of guesses displayed to the correct amount of guesses
  94. # changes the string to be displayed on the window to the correct amount of guesses
  95. num_of_guesses_string_final = num_of_guesses_string + guesses_string_num
  96. num_of_guesses_label.config(text=num_of_guesses_string_final)
  97. if song_lower == user_answer_lower:
  98. guess_correct_label.pack()
  99. # adds 3 or 1 to score depending on the number of guesses they've had
  100. if num_of_guesses == 1:
  101. score += 3
  102. score_num_string = str(score)
  103. score_string_final = score_string + score_num_string
  104. score_label.config(text=score_string_final)
  105. next_guess_button.pack()
  106. guess_button.pack_forget()
  107. answered_correctly = True
  108. elif num_of_guesses == 2:
  109. score += 1
  110. score_num_string = str(score)
  111. score_string_final = score_string + score_num_string
  112. score_label.config(text=score_string_final)
  113. next_guess_button.pack()
  114. guess_button.pack_forget()
  115. answered_correctly = True
  116. # if they've had 2 guesses they are offered the chance to restart and score is reset
  117. elif num_of_guesses == 2:
  118. guess_correct_label.config(text='Game Over!')
  119. score_end_string = 'Your Score was '
  120. score_string = str(score)
  121. score_end_string_final= score_end_string + score_string
  122. score_label.config(text=score_end_string_final, font='helvetica 40 bold')
  123. song_explainer = 'The song was '
  124. song_explainer_final = song_explainer + song
  125. actual_song = tkinter.Label(play_window, text=song_explainer_final, font="helvetica 25 bold",
  126. bg='deep sky blue')
  127. view_high_scores_button = tkinter.Button(play_window, text="View high scores", bg='deep sky blue',
  128. font="helvetica 25 bold", command=view_high_scores)
  129. guess_correct_label.pack()
  130. try_again_button.pack()
  131. view_high_scores_button.pack()
  132. guess_button.pack_forget()
  133. actual_song.pack()
  134. def view_high_scores():
  135. with open("User_Password_NEA_Peter_Leyden.txt", "r")as f:
  136. high_scores_window = tkinter.Tk()
  137. username_password_high_score_list = [""]
  138. for x in range(0, 1000):
  139. file_entry = f.readline()
  140. if file_entry == password_checker:
  141. username_password_high_score_list.append(password_checker)
  142. old_high_score = f.readline()
  143. print(old_high_score)
  144. print(score)
  145. old_high_score_num = int(old_high_score)
  146. if old_high_score_num <= score:
  147. score_str = str(score)
  148. score_str = score_str+string32
  149. username_password_high_score_list.append(score_str)
  150. congratulation_new_high_score_label = tkinter.Label(high_scores_window, text="*NEW HIGH SCORE!!!!!*",
  151. font=("Comic Sans MS", 25, "bold"), bg='deep sky blue', fg='green')
  152. congratulation_new_high_score_label.pack()
  153. else:
  154. username_password_high_score_list.append(old_high_score)
  155. else:
  156. username_password_high_score_list.append(file_entry)
  157. print(username_password_high_score_list)
  158. with open("User_password_NEA_Peter_Leyden.txt", "w")as f:
  159. length_of_list = len(username_password_high_score_list)
  160. place_in_list = -1
  161. for x in range(0, length_of_list):
  162. place_in_list += 1
  163. list_entry = username_password_high_score_list[place_in_list]
  164. f.write(list_entry)
  165. with open("User_Password_NEA_Peter_Leyden.txt", "r")as f:
  166. high_score_list = [""]
  167. high_score_list.remove("")
  168. for x in range(0, 250):
  169. f.readline()
  170. f.readline()
  171. f.readline()
  172. high_score = f.readline()
  173. if high_score == '':
  174. pass
  175. else:
  176. high_score_num = int(high_score)
  177. high_score_list.append(high_score_num)
  178. print(high_score)
  179. high_scores_label = tkinter.Label(high_scores_window, text="High Scores!", font=("Comic Sans MS", 25, "bold"),
  180. bg='deep sky blue')
  181. high_scores_label.pack()
  182. high_score_list.sort(reverse=True)
  183. print(high_score_list)
  184. for x in range(0,5):
  185. high_score_display = high_score_list[x]
  186. print(high_score_display)
  187. high_score_num_label = tkinter.Label(high_scores_window, text=high_score_display, bg='deep sky blue')
  188. high_score_num_label.pack()
  189.  
  190. high_scores_window.title("High Scores")
  191. high_scores_window.configure(background='deep sky blue')
  192. high_score1 = tkinter.Label(high_scores_window)
  193. high_scores_window.mainloop()
  194.  
  195. def play_game_restart():
  196. # restarts the game if the game ended
  197. play_window.destroy()
  198. play_game()
  199. def label_that_change():
  200. # Only needs to be run once so it is in a seperate function
  201. global song_first_letter_label
  202. global play_artist_label
  203. global num_of_guesses_label
  204. song_first_letter_label = tkinter.Label(play_window, text=list_of_first_letters, bg='deep sky blue',
  205. font=("Comic Sans MS", 25, "bold"))
  206. play_artist_label = tkinter.Label(play_window, text=artist, bg='deep sky blue', fg='green',
  207. font=("Comic Sans MS", 25, "bold"))
  208. num_of_guesses_label = tkinter.Label(play_window, text=num_of_guesses_string_final, bg='deep sky blue',
  209. font='helvetica 15 bold')
  210. def generate_song3():
  211. global artist
  212. global song
  213. global list_of_first_letters
  214. global score_string_final
  215. global num_of_guesses_string
  216. global score_string
  217. global play_artist_label
  218. global num_of_guesses_label
  219. global num_of_guesses
  220. global song_first_letter_label
  221. global num_of_guesses_string_final
  222. global answered_correctly
  223. # reads the number of lines divides by 2 to get the number of songs
  224. num_of_lines = -1
  225. answered_correctly = False
  226. line = " "
  227. num_of_guesses = 0
  228. with open("Song_List_NEA_Peter_Leyden.txt", 'r')as f:
  229. while line != '':
  230. line = f.readline()
  231. num_of_lines += 1
  232. num_of_songs = num_of_lines / 2
  233. num_of_songs = int(num_of_songs)
  234. random_song = randint(0, num_of_songs)
  235. random_song = random_song - 1
  236. random_song = random_song * 2
  237. # reads the random song from the list
  238. with open("Song_List_NEA_Peter_Leyden.txt", 'r')as f:
  239. for x in range(0, random_song):
  240. f.readline()
  241. artist = f.readline()
  242. song = f.readline()
  243. # splits the song into the first letters
  244. song_split = song.split(' ')
  245. song_number_of_words = len(song_split)
  246. num_of_words = 0
  247. list_of_first_letters = ['']
  248. for x in range(0, song_number_of_words):
  249. word_of_song = song_split[num_of_words]
  250. num_of_words += 1
  251. first_letter_of_word_song = word_of_song[0]
  252. capital_first_letter_of_word_song = first_letter_of_word_song.upper()
  253. list_of_first_letters.append(capital_first_letter_of_word_song)
  254. list_of_first_letters.remove("")
  255. # changes the guesses string and sets it
  256. num_of_guesses_string = "Guesses: "
  257. guesses_string_num = str(num_of_guesses)
  258. num_of_guesses_string_final = num_of_guesses_string + guesses_string_num
  259. score_string = 'Score: '
  260. score_num_string = str(score)
  261. score_string_final = score_string + score_num_string
  262. def generate_song():
  263. # This is only needed for the first time the program runs
  264. generate_song3()
  265. label_that_change()
  266. def generate_song2():
  267. # generates the new song if they get it correct
  268. global song_first_letter_label
  269. global play_artist_label
  270. global num_of_guesses_label
  271. global guess_correct_label
  272. global next_guess_button
  273. global guess_button
  274. generate_song3()
  275. # changes the Labels to the new score/artist/first letter of song
  276. song_first_letter_label.config(text=list_of_first_letters)
  277. play_artist_label.config(text=artist)
  278. num_of_guesses_label.config(text=num_of_guesses_string_final)
  279. # deletes previous song in the text box
  280. song_entry.delete(0, 'end')
  281. # hides the label and button that the user uses if they get it correct
  282. guess_correct_label.pack_forget()
  283. next_guess_button.pack_forget()
  284. guess_button.pack()
  285. def play_game():
  286. global num_of_guesses_string
  287. global song
  288. global song_entry
  289. global guess_correct_label
  290. global num_of_guesses
  291. global score
  292. global num_of_guesses_label
  293. global play_window
  294. global score_string
  295. global score_string_final
  296. global score_label
  297. global try_again_button
  298. global Rule_game_window
  299. global next_guess_button
  300. global song_first_letter_label
  301. global play_artist_label
  302. global guess_button
  303. # sets variables to 0
  304. score = 0
  305. num_of_guesses = 0
  306. # creates the play_window so that the user can play the game
  307. play_window = Tk()
  308. play_window.title("Music Game!")
  309. play_window.configure(background='deep sky blue')
  310. play_gif = tkinter.PhotoImage(file='Music.gif')
  311. play_gif_label = tkinter.Label(play_window, image=play_gif, bg='black')
  312. play_welcome = tkinter.Label(play_window, text='Welcome to the Music Game!', bg="deep sky blue",
  313. font=("Comic Sans MS", 32, "bold"))
  314. # generates song to be displayed on
  315. generate_song()
  316. artist_label = tkinter.Label(play_window, text='Artist: ', bg="deep sky blue", font='helvetica 15 bold')
  317. song_label = tkinter.Label(play_window, text="First letter of each word of the song: ",
  318. bg='deep sky blue', font="helvetica 15 bold")
  319. song_entry = tkinter.Entry(play_window)
  320. guess_button = tkinter.Button(play_window, text='Guess', bg='deep sky blue', font='helvetica 20 bold',
  321. command=check_answer)
  322. guess_correct_label = tkinter.Label(play_window, text="Correct!!!", bg='deep sky blue',
  323. fg='green', font=("Comic Sans MS", 25, "bold"))
  324. score_label = tkinter.Label(play_window, text=score_string_final, bg="deep sky blue", font='helvetica 20 bold')
  325. try_again_button = tkinter.Button(play_window, text="Try again", bg='deep sky blue',
  326. font='helvetica 20 bold', command=play_game_restart)
  327. next_guess_button = tkinter.Button(play_window, text="Next Question", bg="deep sky blue",
  328. font='helvetica 20 bold', command=generate_song2)
  329. # packs all of the relevant label/button into the tkinter window
  330. play_welcome.pack()
  331. #play_gif_label.pack()
  332. score_label.pack()
  333. artist_label.pack()
  334. play_artist_label.pack()
  335. song_label.pack()
  336. song_first_letter_label.pack()
  337. num_of_guesses_label.pack()
  338. song_entry.pack()
  339. guess_button.pack()
  340. # doesn't pack these because they only appear after the user has guessed
  341. guess_correct_label.pack_forget()
  342. try_again_button.pack_forget()
  343. next_guess_button.pack_forget()
  344. # runs the window
  345. play_window.mainloop()
  346. login_confirmed = False
  347. with open("User_Password_NEA_Peter_Leyden.txt", "a")as f:
  348. # if file is empty then this will be your first entry so that the user can set up the program
  349. f.write('')
  350. with open("User_Password_NEA_Peter_Leyden.txt", "r")as f:
  351. # if the file is empty then the user will be able to set up their program
  352. Check_if_File_Empty = f.read()
  353. if Check_if_File_Empty == '':
  354. username = input("You are our first customer Please input your username: ")
  355. password = input("Please input your password: ")
  356. encrypt(username, password)
  357. def new_user():
  358. # adds new user to the list of users
  359. global sign_up_username_entry
  360. global sign_up_password_entry
  361. global sign_up_password2_entry
  362. global number
  363. global failed_already
  364. failed_already = 0
  365. login_Welcome.pack_forget()
  366. login_gif_label.pack_forget()
  367. username_widget.pack_forget()
  368. username_entry_widget.pack_forget()
  369. password_widget.pack_forget()
  370. password_entry_widget.pack_forget()
  371. login_button.pack_forget()
  372. sign_up_button.pack_forget()
  373. login_communicator.pack_forget()
  374. sign_up_title = tkinter.Label(login_window, text='Welcome to the sign up', font=("Comic Sans MS", 32, "bold"),
  375. bg = 'deep sky blue')
  376. sign_up_label = tkinter.Label(login_window, text='Please enter your username and password below',
  377. font='helvetica 15 bold', bg='deep sky blue')
  378. sign_up_username_label = tkinter.Label(login_window, text='Enter Username:', font='helvetica 15 bold',
  379. bg='deep sky blue')
  380. sign_up_username_entry = tkinter.Entry(login_window)
  381. sign_up_password_label = tkinter.Label(login_window, text='Enter Password:', font='helvetica 15 bold',
  382. bg='deep sky blue')
  383. sign_up_password_entry = tkinter.Entry(login_window, show='*')
  384. sign_up_password2_label = tkinter.Label(login_window, text='Re-Enter Password:', font='helvetica 15 bold',
  385. bg='deep sky blue')
  386. sign_up_password2_entry = tkinter.Entry(login_window, show='*')
  387. sign_up_confirm_button = tkinter.Button(login_window, text='Sign Up', bg='deep sky blue', font='helvetica 20 bold',
  388. command=add_new_user)
  389. sign_up_title.pack()
  390. sign_up_label.pack()
  391. sign_up_username_label.pack()
  392. sign_up_username_entry.pack()
  393. sign_up_password_label.pack()
  394. sign_up_password_entry.pack()
  395. sign_up_password2_label.pack()
  396. sign_up_password2_entry.pack()
  397. sign_up_confirm_button.pack()
  398. line_in_file = False
  399. number = 0
  400. with open("User_Password_NEA_Peter_Leyden.txt", "r")as f:
  401. while line_in_file == False:
  402. number += 1
  403. file_ended = f.readline()
  404. if file_ended == "":
  405. line_in_file = True
  406. else:
  407. pass
  408. def new_user_finished():
  409. global login_confirmed
  410. login_window.destroy()
  411. login_confirmed = True
  412. encrypt(username, password)
  413. def add_new_user():
  414. global password
  415. global username
  416. global failed_already
  417. password = sign_up_password_entry.get()
  418. password2 = sign_up_password2_entry.get()
  419. username = sign_up_username_entry.get()
  420. sign_up_fail_label = tkinter.Label(login_window, text="The passwords weren't the same try again",
  421. bg='deep sky blue', font=("Comic Sans MS", 20, "bold"))
  422. if password == password2:
  423. sign_up_fail_label.pack_forget()
  424. sign_up_confirmed_label = tkinter.Label(login_window, text='You have siged up!', bg='deep sky blue',
  425. font=("Comic Sans MS", 32, "bold"))
  426. sign_up_continue_button = tkinter.Button(login_window, text='Continue', bg='deep sky blue',
  427. font='helvetica 20 bold', command=new_user_finished)
  428. sign_up_confirmed_label.pack()
  429. sign_up_continue_button.pack()
  430. else:
  431. if failed_already == 0:
  432. sign_up_fail_label.pack()
  433. failed_already = 1
  434. # defines all of the relevant information for the login window
  435. login_window = tkinter.Tk()
  436. login_window.title("Login")
  437. login_Welcome = tkinter.Label(login_window, text='Welcome to the Music Game!',
  438. bg='deep sky blue', font=("Comic Sans MS", 32, "bold"))
  439. username_entry_widget = tkinter.Entry(login_window)
  440. # makes the password secret from casual observers
  441. password_entry_widget = tkinter.Entry(login_window, show='*')
  442. sign_up_button = tkinter.Button(login_window, text="Sign Up", bg='deep sky blue', command=new_user)
  443. login_gif = tkinter.PhotoImage(file='Music.gif')
  444. login_gif_label = tkinter.Label(login_window, image=login_gif, bg='black')
  445. username_widget = tkinter.Label(login_window, text='Username:', bg='deep sky blue', font='helvetica 20 bold')
  446. password_widget = tkinter.Label(login_window, text='Password:', bg='deep sky blue', font='helvetica 20 bold')
  447. login_button = tkinter.Button(login_window, text='Submit', bg='deep sky blue', command=check_login_information)
  448. login_communicator = tkinter.Label(login_window, text='You are currently logged out',
  449. bg="deep sky blue", fg='Green', font='helvetica 25 bold')
  450. login_window.configure(background='deep sky blue')
  451. # packs all of the relevant information into the login_window
  452. login_Welcome.pack()
  453. login_gif_label.pack()
  454. username_widget.pack()
  455. username_entry_widget.pack()
  456. password_widget.pack()
  457. password_entry_widget.pack()
  458. login_button.pack()
  459. sign_up_button.pack()
  460. login_communicator.pack()
  461. login_window.mainloop()
  462. if login_confirmed == True:
  463. Rule_game_window = tkinter.Tk()
  464. Rule_game_window.title("Music Game Rules")
  465. Rule_game_window.configure(background='deep sky blue')
  466. Rule_game_Welcome = tkinter.Label(Rule_game_window, text='Welcome to the Music Game!!!!', bg='deep sky blue',
  467. font=("Comic Sans MS", 32, "bold"))
  468. Rule_game_rule = tkinter.Label(Rule_game_window, text="The rules of the game are:", font='helvetica 25 bold',
  469. bg="deep sky blue", fg='Green')
  470. Rule_game_rules1 = tkinter.Label(Rule_game_window, text="–A song will be Chosen at random,", bg="deep sky blue")
  471. Rule_game_rules2 = tkinter.Label(Rule_game_window, text="–The name of artist will be given,", bg="deep sky blue")
  472. Rule_game_rules3 = tkinter.Label(Rule_game_window, text="–The first letter of each word in the song will be given,",
  473. bg="deep sky blue")
  474. Rule_game_rules4 = tkinter.Label(Rule_game_window, text="–You will have 2 Guesses,", bg="deep sky blue")
  475. Rule_game_rules5 = tkinter.Label(Rule_game_window, text="–If you get it on the first guess you will get 3 points,",
  476. bg="deep sky blue")
  477. Rule_game_rules6 = tkinter.Label(Rule_game_window, text="–If you get it on the second guess you will get 1 point,",
  478. bg="deep sky blue")
  479. Rule_game_rules7 = tkinter.Label(Rule_game_window, text="–If you fail both guesses the game is over.",
  480. bg="deep sky blue")
  481. Rule_game_play = tkinter.Button(Rule_game_window, text="Play Game!", bg="deep sky blue",
  482. font=("Comic Sans MS", 20, "bold"), command=destroy_rule_window_play_game)
  483. Rule_game_gif = tkinter.PhotoImage(file='Music.gif')
  484. Rule_game_gif_label = tkinter.Label(Rule_game_window, image=Rule_game_gif, bg='black')
  485. Rule_game_Welcome.pack()
  486. Rule_game_gif_label.pack()
  487. Rule_game_rule.pack()
  488. Rule_game_rules1.pack()
  489. Rule_game_rules2.pack()
  490. Rule_game_rules3.pack()
  491. Rule_game_rules4.pack()
  492. Rule_game_rules5.pack()
  493. Rule_game_rules6.pack()
  494. Rule_game_rules7.pack()
  495. Rule_game_play.pack()
  496. Rule_game_window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement