Guest User

Untitled

a guest
Jan 25th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.26 KB | None | 0 0
  1. from turtle import Turtle, Screen
  2. from random import random, randint
  3. from time import time
  4. import os
  5. import sys
  6. import tkinter.messagebox as tm
  7. import hashlib, uuid
  8. import re
  9. import turtle
  10.  
  11. CURSOR_SIZE = 20
  12.  
  13. from tkinter import *
  14.  
  15. user_get = 0
  16. variable = 1
  17. diffSetting = 9
  18.  
  19.  
  20.  
  21. class Application(Frame):
  22. def __init__(self,master):
  23. Frame.__init__(self, master)#Set __init__ to the master class
  24. self.grid()
  25. self.create_main()#Creates function
  26. self.Read()
  27. changeVariable1(self)
  28. changeVariable2(self)
  29. changeVariable3(self)
  30.  
  31. def Read(self):
  32. top = Tk()
  33. top.geometry("%dx%d%+d%+d" % (420, 500, 250, 125))
  34.  
  35. top.title = Label(top, text="Menu ", font='Helvetica 10 bold')#TITLE
  36. top.title.grid(row=0, column=2)
  37.  
  38. top.title = Label(top, text=" ", font='Helvetica 10 bold')#blank line
  39. top.title.grid(row=1, column=1)
  40.  
  41. top.user_entry_label = Label(top, text="Difficulty: ", font='Helvetica 10 bold')#USERNAME LABEL
  42. top.user_entry_label.grid(row=3, column=1)
  43.  
  44. top.easybutt = Button(top, text="Easy", font='Helvetica 10 bold', command = changeVariable1(self))#easy
  45. top.easybutt.grid(row=3, column=2)
  46.  
  47. top.medbutt = Button(top, text="Medium", font='Helvetica 10 bold', command = changeVariable2(self))#med
  48. top.medbutt.grid(row=3, column=3)
  49.  
  50. top.hrdbutt = Button(top, text="Hard", font='Helvetica 10 bold', command = changeVariable3(self))#hrd
  51. top.hrdbutt.grid(row=3, column=4)
  52.  
  53. top.title = Label(top, text=" ", font='Helvetica 10 bold')#blank line
  54. top.title.grid(row=4, column=1)
  55.  
  56.  
  57.  
  58.  
  59. def create_main(self):
  60. self.title = Label(self, text="Reflex Login ", font='Helvetica 10 bold')#TITLE
  61. self.title.grid(row=0, column=2)
  62.  
  63. self.user_entry_label = Label(self, text="Username: ", font='Helvetica 10 bold')#USERNAME LABEL
  64. self.user_entry_label.grid(row=1, column=1)
  65.  
  66. self.user_entry = Entry(self) #USERNAME ENTRY BOX
  67. self.user_entry.grid(row=1, column=2)
  68.  
  69. self.pass_entry_label = Label(self, text="Password: ", font='Helvetica 10 bold')#PASSWORD LABEL
  70. self.pass_entry_label.grid(row=2, column=1)
  71.  
  72. self.pass_entry = Entry(self, show="●") #PASSWORD ENTRY BOX
  73. self.pass_entry.grid(row=2, column=2)
  74.  
  75. self.sign_in_butt = Button(self, text="Sign In", font='Helvetica 10 bold', command = self.logging_in)#SIGN IN BUTTON
  76. self.sign_in_butt.grid(row=3, column=2)
  77.  
  78. self.register_butt = Button(self, text="Register",font='Helvetica 10 bold', command = self.register)#Register button
  79. self.register_butt.grid(row=4, column=2)
  80.  
  81. self.title = Label(self, text=" ", font='Helvetica 10 bold')#blank line
  82. self.title.grid(row=5, column=2)
  83.  
  84. self.title = Label(self, text=" ", font='Helvetica 10 bold')#blank line
  85. self.title.grid(row=6, column=2)
  86.  
  87. #username criteria
  88. self.title = Label(self, text="---------------------------------------", font='Helvetica 10 bold')#seperator
  89. self.title.grid(row=6, column=2)
  90. self.title = Label(self, text="Username Criteria: ", font='Helvetica 10 bold')#TITLE
  91. self.title.grid(row=7, column=2)
  92. self.title = Label(self, text="One number 1-9.", font='Helvetica 10')
  93. self.title.grid(row=8, column=2)
  94. self.title = Label(self, text="6-12 characters in length.", font='Helvetica 10')
  95. self.title.grid(row=9, column=2)
  96. self.title = Label(self, text="Include no spaces.", font='Helvetica 10')
  97. self.title.grid(row=10, column=2)
  98.  
  99. #password criteria
  100. self.title = Label(self, text=" ", font='Helvetica 10 bold')#blank line
  101. self.title.grid(row=11, column=2)
  102. self.title = Label(self, text="---------------------------------------", font='Helvetica 10 bold')#seperator
  103. self.title.grid(row=11, column=2)
  104. self.title = Label(self, text="Password Criteria: ", font='Helvetica 10 bold')#TITLE
  105. self.title.grid(row=12, column=2)
  106. self.title = Label(self, text="One number 1-9.", font='Helvetica 10')
  107. self.title.grid(row=13, column=2)
  108. self.title = Label(self, text="6-12 characters in length.", font='Helvetica 10')
  109. self.title.grid(row=14, column=2)
  110. self.title = Label(self, text="One uppercase letter and one lowercase letter.", font='Helvetica 10')
  111. self.title.grid(row=15, column=2)
  112. self.title = Label(self, text="One special character [~!@#$%^&*].", font='Helvetica 10')
  113. self.title.grid(row=16, column=2)
  114. self.title = Label(self, text="Include no spaces.", font='Helvetica 10')
  115. self.title.grid(row=17, column=2)
  116. self.title = Label(self, text="---------------------------------------", font='Helvetica 10 bold')#seperator
  117. self.title.grid(row=18, column=2)
  118.  
  119.  
  120. def logging_in(self):
  121. global user_get
  122. user_get = self.user_entry.get()#Retrieve Username
  123. global pass_get
  124. pass_get = self.pass_entry.get()#Retrieve Password
  125. hash_object = hashlib.md5(pass_get.encode())
  126. for line in open("details.txt","r").readlines(): #Read the lines
  127. login_info = line.split() #Split on the space, and store the results in a list of two strings
  128. if user_get == login_info[0] and (hash_object.hexdigest()) == login_info[1]:
  129. tm.showinfo("Success", 'Login Correct!')
  130. root.destroy()
  131. return True
  132. tm.showerror('Error', 'Login Incorrect')
  133. return False
  134.  
  135.  
  136. def register(self):
  137. global user_get
  138. user_get = self.user_entry.get()
  139. pass_get = self.pass_entry.get()
  140. user_get = self.user_entry.get()#Retrieve Username
  141. pass_get = self.pass_entry.get()#Retrieve Password
  142. hash_object = hashlib.md5(pass_get.encode())
  143.  
  144. #validation check#
  145. #passvalidation
  146. is_valid = False
  147. if (len(pass_get)<6 or len(pass_get)>12):
  148. tm.showerror("Invalid!", 'Password should be between 6 and 12. Try again')
  149. is_valid = False
  150. elif not re.search("[A-Z]",pass_get):
  151. tm.showerror("Invalid!", 'Password should contain one letter between [A-Z]. Try again')
  152. is_valid = False
  153. elif not re.search("[a-z]",pass_get):
  154. tm.showerror("Invalid!", 'Password should contain one letter between [a-z]. Try again')
  155. is_valid = False
  156. elif not re.search("[1-9]",pass_get):
  157. tm.showerror("Invalid!", 'Password should contain one number between [1-9]. Try again')
  158. is_valid = False
  159. elif not re.search("[~!@#$%^&*]",pass_get):
  160. tm.showerror("Invalid!", 'Password should contain at least one letter in [~!@#$%^&*]. Try again')
  161. is_valid = False
  162. elif re.search("[s]",pass_get):
  163. tm.showerror("Invalid!", 'Password should not contain any space. Try again')
  164. is_valid = False
  165. else:
  166. is_valid = True
  167.  
  168. #uservalidation
  169. user_valid = False
  170. if (len(user_get)<6 or len(user_get)>12):
  171. tm.showerror("Invalid!", 'Username should be between 6 and 12. Try again')
  172. user_valid = False
  173. elif not re.search("[1-9]",user_get):
  174. tm.showerror("Invalid!", 'Username should contain one number between [1-9]. Try again')
  175. user_valid = False
  176. elif re.search("[s]",user_get):
  177. tm.showerror("Invalid!", 'Username should not contain any space. Try again')
  178. user_valid = False
  179. else:
  180. user_valid = True
  181.  
  182. if(is_valid) and (user_valid):
  183. tm.showinfo("Valid!", 'Username and Password is valid')
  184. file = open('details.txt', 'a+')
  185. file.write(user_get)
  186. file.write(" ")
  187. file.write(hash_object.hexdigest())
  188. file.write("n")
  189. file.close()
  190.  
  191. def changeVariable1(self):
  192. global difficulty
  193. self.difficulty = 12
  194.  
  195. def changeVariable2(self):
  196. global difficulty
  197. self.difficulty = 16
  198.  
  199. def changeVariable3(self):
  200. global difficulty
  201. self.difficulty = 20
  202.  
  203.  
  204.  
  205. ######watch video
  206.  
  207.  
  208.  
  209. root = Tk()
  210. root.title("Reflex Aim Training")
  211. root.geometry("%dx%d%+d%+d" % (420, 500, 250, 125))
  212. app = Application(root)#The frame is inside the widgit
  213. #root.overrideredirect(True) ###############################
  214. root.mainloop()#Keeps the window open/running
  215. top = Toplevel()
  216. top.title("Main Menu")
  217.  
  218.  
  219.  
  220. #reset
  221. reset = int(input("Would you like to continue or start fresh? 1 for fresh start, Any other number for continue: "))
  222.  
  223. if reset == 1:
  224. os.remove('stats.txt')
  225. os.remove('maths.txt')
  226. print("Your previous scores have been deleted")
  227. else:
  228. print("We will keep your previous scores stored.")
  229.  
  230.  
  231. #while True:
  232. # diffSetting=int(input("Set the difficulty(1-3): "))
  233. #
  234. # if diffSetting == 1:
  235. # difficulty = 12
  236. # break
  237. ## elif diffSetting == 2:
  238. # difficulty = 16
  239. # break
  240. # elif diffSetting == 3:
  241. # difficulty = 20
  242. # break
  243. # else:
  244. #print("Please choose a difficulty setting between 1 to 3.")
  245.  
  246. def addscore():
  247. global score
  248. score += 2
  249.  
  250. def deletescore():
  251. global score
  252. score -= 1
  253.  
  254. def my_circle(color):
  255.  
  256. if diffSetting==1:
  257. radius = (30)
  258. elif diffSetting==2:
  259. radius = (20)
  260. elif diffSetting==3:
  261. radius = (10)
  262.  
  263.  
  264. circle = Turtle('circle', visible=False)
  265. circle.shapesize(radius / CURSOR_SIZE)
  266. circle.color('black', color)
  267.  
  268. circle.penup()
  269.  
  270. while True:
  271. nx = randint(2 * radius - width // 2, width // 2 - radius * 2)
  272. ny = randint(2 * radius - height // 2, height // 2 - radius * 2)
  273.  
  274. circle.goto(nx, ny)
  275.  
  276. for other_radius, other_circle in circles:
  277. if circle.distance(other_circle) < 2 * max(radius, other_radius):
  278. break
  279. else:
  280. break
  281.  
  282. circle.onclick(lambda x,y,t=circle: (circle.hideturtle(), addscore()))
  283. circle.showturtle()
  284.  
  285.  
  286.  
  287. return radius, circle
  288.  
  289. def play():
  290. rgb = (random(), random(), random())
  291.  
  292. timeTaken = time() - startTime
  293.  
  294. circles.append(my_circle(rgb))
  295.  
  296. screen.title('SCORE: {}, TIME LEFT: {}'.format(score, int(round(gameLength - timeTaken, 0))))
  297.  
  298. if time() - startTime > gameLength:
  299. screen.title('FINAL SCORE: {}'.format(score))
  300. screen.onclick(None)
  301. screen.clear()
  302. else:
  303. screen.ontimer(play, 1000 // difficulty)
  304.  
  305.  
  306.  
  307. screen = Screen()
  308. screen.title("Reflex Aim Training")
  309. screen.bgcolor("grey")
  310.  
  311. width, height = screen.window_width(), screen.window_height()
  312.  
  313. score = 0
  314.  
  315. circles = []
  316.  
  317. gameLength = 30
  318.  
  319. screen.onclick(lambda x, y: deletescore())
  320.  
  321. startTime = time()
  322.  
  323. play()
  324.  
  325. screen.mainloop()
  326.  
  327.  
  328.  
  329. print ("{} points, Well Done!".format(score))
  330.  
  331. #storingscore
  332. file = open('scoreboard.txt', 'r')
  333. name = file.readline()
  334. high_score = int(file.readline())
  335. file.close()
  336.  
  337. plus_score = (score)-(high_score)
  338. minus_score = (high_score)-(score)
  339.  
  340.  
  341. if score > high_score:
  342. file = open('scoreboard.txt', 'w')
  343. file.write(str(user_get) + 'n' + str(score) + 'n' "You beat the previous high score by: "+str(plus_score)+" points!")
  344. file.close()
  345.  
  346. if high_score > score:
  347. file = open('scoreboard.txt', 'w')
  348. file.write(str(user_get) + 'n' + str(score) + 'n' "You missed the high score by: "+str(minus_score)+" points!")
  349. file.close()
  350.  
  351. if score == high_score:
  352. file = open('scoreboard.txt', 'w')
  353. file.write(str(user_get) + 'n' + str(score) + 'n' "You drew with the high score!")
  354. file.close()
  355.  
  356. file = open('stats.txt', 'a+')
  357. file.write('n' + 'Score: ' +str(score))
  358. file.close()
  359.  
  360. numbers = []
  361. with open('stats.txt') as fh:
  362. count = 0
  363. for line in fh:
  364. count += 1
  365. numbers.append(float(line.split()[1]))
  366.  
  367. file = open('maths.txt', 'w+')
  368. file.write('Average Score: ' + str(sum(numbers)/len(numbers)) + "n")
  369. file.write('Maximum Score: ' + str(max(numbers)) + "n")
  370. file.write('Minimum Score: ' + str(min(numbers)) + "n")
  371. maxn = max(numbers)
  372. minn = min(numbers)
  373. rangex = (maxn) - (minn)
  374. file.write('Range of Scores: ' + str(rangex) + "n")
  375.  
  376. l = []
  377.  
  378. with open('stats.txt', 'r') as f:
  379. for i in f:
  380. l.append(int(i.split(':')[1].strip()))
  381.  
  382. import statistics
  383. stdevi = statistics.stdev(l)
  384. file.write('Standard Deviation: ' + str(stdevi) + "n")
Add Comment
Please, Sign In to add comment