Advertisement
Guest User

Untitled

a guest
May 6th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.66 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter import Tk, Canvas
  3. from itertools import product
  4. from Game import game
  5. import os
  6. import sqlite3
  7. import random
  8. from itertools import product
  9.  
  10. #These are the functions I made to delete unwanted screens at given moments
  11. def delete():
  12. screen.destroy()
  13.  
  14. def delete2():
  15. screen2.destroy()
  16. screen3.destroy()
  17. screen4.destroy()
  18.  
  19. def delete3():
  20. screen10.destroy()
  21.  
  22. def delete4():
  23. screen11.destroy()
  24.  
  25. def delete5():
  26. screen9.destroy()
  27.  
  28. def delete6():
  29. screen13.destroy()
  30.  
  31. def delete7():
  32. screen7.destroy()
  33.  
  34. def delete8():
  35. screen2.destroy()
  36. screen14.destroy()
  37. screen7.destroy()
  38.  
  39. def back():
  40. screen2.destroy()
  41.  
  42. def logout():
  43. screen2.destroy()
  44. screen5.destroy()
  45.  
  46. def retry():
  47. screen6.destroy()
  48.  
  49. #This is my main screen menu where all the screens will be created from this one
  50. def main_screen():
  51. global screen
  52. screen = Tk()
  53. screen.title("Main Menu")
  54. screen.geometry("300x250")
  55.  
  56. Label(text = "Learning Race", bg = "cornflowerblue", width = "300", height = "2", font = ("Comic Sans MS", 15)).pack()
  57. Label(text = "").pack()
  58. Button(text = "Log in", height = "2", width = "30", command = login).pack()
  59. Label(text = "").pack()
  60. Button(text = "Register", height = "2", width = "30", command = register).pack()
  61. Label(text = "").pack()
  62. Button(text = "Quit", height = "1", width = "10", command = delete).pack()
  63. screen.mainloop()
  64.  
  65. #This is the menu where you can choose the type of login student or teacher
  66. def login():
  67. global screen2
  68. screen2 = Toplevel(screen)
  69. screen2.title("Log in")
  70. screen2.geometry("300x250")
  71.  
  72. Label(screen2, text = "Please select what you want to log in as:").pack()
  73. Button(screen2, text = "Student", width = 30, height = 2, command = login_student).pack()
  74. Label(screen2,text = "").pack()
  75. Button(screen2, text = "Teacher", width = 30, height = 2, command = login_teacher).pack()
  76. Label(screen2,text = "").pack()
  77. Button(screen2, text = "Back", width = 10, height = 1, command = back).pack()
  78.  
  79. #This is the student log in screen where the user enters the data and is then checked
  80. def login_student():
  81. global screen3
  82. screen3 = Toplevel(screen)
  83. screen3.title("Log in as a Student")
  84. screen3.geometry("300x250")
  85.  
  86. Label(screen3, text = "Please enter details below to log in as a Student").pack()
  87.  
  88. global username_verify
  89. global password_verify
  90.  
  91. username_verify = StringVar()
  92. password_verify = StringVar()
  93.  
  94. global username_entry1
  95. global password_entry1
  96.  
  97. Label(screen3, text = "Username * ").pack()
  98. username_entry1 = Entry (screen3, textvariable = username_verify)
  99. username_entry1.pack()
  100. Label(screen3, text = "").pack()
  101. Label(screen3, text = "Password * ").pack()
  102. password_entry1 = Entry(screen3, show="*", textvariable = password_verify)
  103. password_entry1.pack()
  104. Label(screen3, text = "").pack()
  105. Button(screen3, text = "Log in", width = 10, height = 1, command = login_verify).pack()
  106.  
  107. #The main menu once you log in as a student
  108. def session():
  109. global screen5
  110. screen5 = Toplevel(screen)
  111. screen5.title("Game")
  112. screen5.geometry("400x300")
  113.  
  114. Label(screen5, text = "Welcome to the game").pack()
  115. Label(screen5, text = "").pack()
  116. Button(screen5, text = "Play", height = "2", width = "30", command = play).pack()
  117. Label(screen5, text = "").pack()
  118. Button(screen5, text = "Profile", height = "2", width = "30", command = profile).pack()
  119. Label(screen5, text = "").pack()
  120. Button(screen5, text = "Log out", height = "1", width = "10",command = logout).pack()
  121.  
  122. #This is the teacher log in screen where the user enters the data and is then checked
  123. def login_teacher():
  124. global screen7
  125. screen7 = Toplevel(screen)
  126. screen7.title("Log in as a Teacher")
  127. screen7.geometry("300x250")
  128.  
  129. Label(screen7, text = "Please enter details below to log in as a Teacher").pack()
  130.  
  131. global username_verify2
  132. global password_verify2
  133.  
  134. username_verify2 = StringVar()
  135. password_verify2 = StringVar()
  136.  
  137. global username_entry2
  138. global password_entry2
  139. global code_entry2
  140.  
  141. Label(screen7, text = "Username * ").pack()
  142. username_entry2 = Entry (screen7, textvariable = username_verify2)
  143. username_entry2.pack()
  144. Label(screen7, text = "Password * ").pack()
  145. password_entry2 = Entry(screen7, show="*", textvariable = password_verify2)
  146. password_entry2.pack()
  147. Label(screen7, text = "Class Code * ").pack()
  148. code_entry2 = Entry(screen7)
  149. code_entry2.pack()
  150. Label(screen7, text = "").pack()
  151. Button(screen7, text = "Log in", width = 10, height = 1, command = login_verify2).pack()
  152.  
  153. #The main menu once you log in as a teacher
  154. def session2():
  155. global screen15
  156. screen15 = Toplevel(screen)
  157. screen15.title("Menu")
  158. screen15.geometry("400x300")
  159.  
  160. Label(screen15, text = "Welcome to the teacher area!").pack()
  161. Label(screen15, text = "").pack()
  162. Button(screen15, text = "See x ", height = "2", width = "30", command = play).pack()
  163. Label(screen15, text = "").pack()
  164. Button(screen15, text = "Profile", height = "2", width = "30", command = profile).pack()
  165. Label(screen15, text = "").pack()
  166. Button(screen15, text = "Log out", height = "1", width = "10",command = logout).pack()
  167.  
  168. #This the screen is where students can register a new account
  169. def register():
  170. global screen1
  171. screen1 = Toplevel(screen)
  172. screen1.title("Register")
  173. screen1.geometry("300x250")
  174.  
  175. global username
  176. global password
  177. global password1
  178. global code
  179. global username_entry
  180. global password_entry
  181. global password_entry0
  182. global code_entry
  183.  
  184. username = StringVar()
  185. password = StringVar()
  186. password1 = StringVar()
  187. code = StringVar()
  188.  
  189. Label(screen1, text = "Please enter details below").pack()
  190. Label(screen1, text = "").pack()
  191. Label(screen1, text = "Username * ").pack()
  192. username_entry = Entry(screen1, textvariable = username)
  193. username_entry.pack()
  194. Label(screen1, text = "Password * ").pack()
  195. password_entry = Entry(screen1, show="*", textvariable = password)
  196. password_entry.pack()
  197. Label(screen1, text = "Re-enter password * ").pack()
  198. password_entry0 = Entry(screen1, show="*", textvariable = password1)
  199. password_entry0.pack()
  200. Label(screen1, text = "Class code * ").pack()
  201. code_entry = Entry(screen1, textvariable = code)
  202. code_entry.pack()
  203. Button(screen1, text = "Register", width = 10, height = 1, command = register_user).pack()
  204.  
  205. #This is where the student´s login data is checked to the saved data in the file
  206. def login_verify():
  207. global username1
  208. username1 = username_verify.get()
  209. password1 = password_verify.get()
  210. username_entry1.delete(0, END)
  211. password_entry1.delete(0, END)
  212.  
  213. list_of_files = os.listdir()
  214. #The apropiate file acording to the username is searched in the OS
  215. if username1 in list_of_files:
  216. #The file is opened as a read
  217. file1 = open(username1, "r")
  218. verify = file1.read().splitlines()
  219. #First it is checked to see if it is the correct type of account as in Student not Teacher
  220. if verify[0][0] == "S":
  221. #This is a message to say you have loged in successfully
  222. if password1 in verify:
  223. login_success()
  224. #This is an error message to say the password is incorrect
  225. else:
  226. password_not_recognised()
  227. #This is an error message to say the account is a teacher account not a student one
  228. else:
  229. incorrect_login()
  230. #This is an error message to say that the user doesn´t exist
  231. else:
  232. user_not_found()
  233.  
  234. #This is where the teacher´s login data is checked to the saved data in the file
  235. def login_verify2():
  236. global username2
  237. username2 = username_verify2.get()
  238. password2 = password_verify2.get()
  239. username_entry2.delete(0, END)
  240. password_entry2.delete(0, END)
  241. code_entry2.delete(0, END)
  242.  
  243. list_of_files = os.listdir()
  244. #The apropiate file acording to the username is searched in the OS
  245. if username2 in list_of_files:
  246. #The file is opened as a read
  247. file2 = open(username2, "r")
  248. verify2 = file2.read().splitlines()
  249. #First it is checked to see if it is the correct type of account as in Teacher not Student
  250. if verify2[0][0] == "T":
  251. if password2 in verify2:
  252. #This is a message to say you have loged in successfully
  253. login_success2()
  254. #This is an error message to say the password is incorrect
  255. else:
  256. password_not_recognised()
  257. #This is an error message to say the account is a student account not a teacher one
  258. else:
  259. incorrect_login()
  260. #This is an error message to say that the user doesn´t exist
  261. else:
  262. user_not_found()
  263.  
  264. #This is an error message screen
  265. def password_not_recognised():
  266. global screen10
  267. screen10 = Toplevel(screen)
  268. screen10.title("Error")
  269. screen10.geometry("150x100")
  270. Label(screen10, text = "Password Error").pack()
  271. Button(screen10, text = "OK", command = delete3).pack()
  272.  
  273. #This is an error message screen
  274. def user_not_found():
  275. global screen11
  276. screen11 = Toplevel(screen)
  277. screen11.title("Error")
  278. screen11.geometry("150x100")
  279. Label(screen11, text = "User not found").pack()
  280. Button(screen11, text = "OK", command = delete4).pack()
  281.  
  282. #This is an error message screen
  283. def incorrect_login():
  284. global screen13
  285. screen13 = Toplevel(screen)
  286. screen13.title("Error")
  287. screen13.geometry("550x80")
  288. Label(screen13, text = "User not a Teacher/Student, choose the appropiate login and try again").pack()
  289. Button(screen13, text = "OK", command = delete6).pack()
  290.  
  291. #This is a login success screen for students
  292. def login_success():
  293. global screen4
  294. screen4 = Toplevel(screen)
  295. screen4.title("Success")
  296. screen4.geometry("150x100")
  297.  
  298. Label(screen4, text = "Login Success").pack()
  299. Button(screen4, text = "OK", command = delete2).pack()
  300. session()
  301.  
  302. #This is a login success screen for teachers
  303. def login_success2():
  304. global screen14
  305. screen14 = Toplevel(screen)
  306. screen14.title("Success")
  307. screen14.geometry("150x100")
  308.  
  309. Label(screen14, text = "Login Success").pack()
  310. Button(screen14, text = "OK", command = delete8).pack()
  311. session2()
  312.  
  313. #This is where the student is registered
  314. def register_user():
  315. p1=password.get()
  316. p2=password1.get()
  317. if p1 == p2:
  318. username_info = username.get()
  319. password_info = password.get()
  320. code_info = code.get()
  321.  
  322. #A new file named like the username is created and the password and classcode are written to the file
  323. file = open(username_info, "w")
  324. file.write("S" + username_info + "\n")
  325. file.write(password_info + "\n")
  326. file.write(code_info)
  327. file.close
  328.  
  329. username_entry.delete(0, END)
  330. password_entry.delete(0, END)
  331. password_entry0.delete(0, END)
  332. code_entry.delete(0, END)
  333.  
  334. Label(screen1, text = "Registration Success", fg = "green", font = ("calibri", 11)).pack()
  335. #If passwords aren´t equal, then it will give an error message
  336. else:
  337. global screen6
  338. screen6 = Toplevel(screen)
  339. screen6.title("Error")
  340. screen6.geometry("150x100")
  341.  
  342. Label(screen6, text = "Passwords aren´t equal").pack()
  343. Button(screen6, text = "Ok", width = 10, height = 1, command = retry).pack()
  344. username_entry.delete(0, END)
  345. password_entry.delete(0, END)
  346. password_entry0.delete(0, END)
  347. code_entry.delete(0, END)
  348.  
  349. #Play button starts game
  350. def play():
  351. screen5.destroy()
  352. dice()
  353. game()
  354.  
  355. #Profile button gives a screen with info on the user
  356. def profile():
  357. global screen8
  358. screen8 = Toplevel(screen)
  359. screen8.title("Profile")
  360. screen8.geometry("400x400")
  361.  
  362. Label(screen8, text = "Username: " + username1, bg = "light green", width = "300", height = "2", font = ("calibri", 14)).pack()
  363. Label(screen8, text = "Level: " + userLevel).pack()
  364. Label(screen8, text = "Number of games played: " + number_of_games).pack()
  365. Label(screen8, text = "Number of games won: " + games_won).pack()
  366. Label(screen8, text = "Number of wrong answers for Spelling: " + number_wrong_spelling).pack()
  367. Label(screen8, text = "Number of wrong answers for Translation: " + number_wrong_translation).pack()
  368. Label(screen8, text = "Number of wrong answers for Synonyms: " + number_wrong_syn).pack()
  369. #Also, here you can change the password
  370. Button(screen8, text = "Change password", command = change_password).pack()
  371.  
  372. #This is a screen where the new password is entered
  373. def change_password():
  374. global screen9
  375. screen9 = Toplevel(screen)
  376. screen9.title("Change Password")
  377. screen9.geometry("300x250")
  378.  
  379. global password3
  380. global password4
  381. global password_entry3
  382. global password_entry4
  383.  
  384. password3 = StringVar()
  385. password4 = StringVar()
  386.  
  387. Label(screen9, text = "Current password *").pack()
  388. password_entry3 = Entry(screen9, show = "*", textvariable = password3).pack()
  389. Label(screen9, text = "New password *").pack()
  390. password_entry4 = Entry(screen9, show = "*", textvariable = password4).pack()
  391. Button(screen9, text = "Change", command = change_password1).pack()
  392.  
  393. #This is where the password is changed
  394. def change_password1():
  395. p3 = password3.get()
  396. p4 = password4.get()
  397. if p3 != "" or p4 != "":
  398. #This is done by rewritting the password in the file
  399. with open(username1, "r") as file1:
  400. data = file1.readlines()
  401. data[1] = str(p4) + "\n"
  402. with open(username1, "w") as file1:
  403. file1.writelines( data )
  404.  
  405. Label(screen9, text = "Password changed", fg = "green", font = ("calibri", 11)).pack()
  406. Button(screen9, text = "OK", command = delete5).pack()
  407. else:
  408. Label(screen9, text = "Error, password not changed", fg = "red", font = ("calibri", 11)).pack()
  409.  
  410. main_screen()
  411.  
  412. def delete_1():
  413. dice_result.destroy()
  414. board.destroy()
  415.  
  416. def delete_2():
  417. dice.destroy()
  418. dice()
  419.  
  420. def game():
  421. class Board(Tk):
  422. def __init__(self, width, height, cellsize):
  423. Tk.__init__(self)
  424. self.cellsize = cellsize
  425. self.canvas = Canvas(self, width=width, height=height)
  426. self.canvas.pack()
  427. def draw_rectangle(self, x1, y1, x2, y2, color):
  428. self.canvas.create_rectangle(x1, y1, x2, y2, fill=color, outline="black")
  429. def draw_circle(self, x1, y1, x2, y2, color):
  430. self.canvas.create_oval(x1, y1, x2, y2, fill=color, outline="black")
  431. def coordinates(self, event):
  432. i = int(event.x / self.cellsize)
  433. j = int(event.y / self.cellsize)
  434. global i
  435. global j
  436.  
  437. size = 80
  438. global board
  439. board = Board(675, 675, size)
  440. board.title("Board")
  441. logicBoard = [[0, 0, 0, 0, 0, 0, 0, 0],
  442. [0, 0, 0, 0, 0, 0, 0, 0],
  443. [0, 0, 0, 0, 0, 0, 0, 0],
  444. [0, 0, 0, 0, 0, 0, 0, 0],
  445. [0, 0, 0, 0, 0, 0, 0, 0],
  446. [0, 0, 0, 0, 0, 0, 0, 0],
  447. [0, 0, 0, 0, 0, 0, 0, 0],
  448. [0, 0, 0, 0, 0, 0, 0, 0]]
  449. for (i, j) in product(range(8), range(8)):
  450. coordX1 = (i * size)
  451. coordY1 = (j * size)
  452. coordX2 = coordX1 + size
  453. coordY2 = coordY1 + size
  454. if coordX1==0 and coordY1==0 and coordX2==80 and coordY2==80 or coordX1==0 and coordY1==560 and coordX2==80 and coordY2==640:
  455. board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, "blue")
  456. else:
  457. board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, "white")
  458. cell = logicBoard[i][j]
  459. pawnColor = "red"
  460. global current_pos_x1
  461. global current_pos_x2
  462. current_pos_x1 = 0
  463. current_pos_x2 = 40
  464. board.draw_circle(current_pos_x1, 560, current_pos_x2, 600, pawnColor)
  465. board.mainloop()
  466.  
  467. def dice():
  468. global dice
  469. dice = Tk()
  470. dice.title("Dice")
  471. dice.geometry("300x250")
  472.  
  473. Label(text = "Click the button to throw the dice").pack()
  474. Button(text = "Throw", height = "2", width = "30", command = dice_throw).pack()
  475. Label(text = "").pack()
  476.  
  477. def dice_throw():
  478. global dice_result
  479. dice_result = Toplevel(dice)
  480. dice_result.geometry("150x100")
  481.  
  482. roll=True
  483. random_number(roll)
  484. Label(dice_result, text = random_numb, font = ("Comic Sans MS", 15)).pack()
  485. Label(dice_result, text = "").pack()
  486. Button(dice_result, text = "Ok", height = "1", width = "10", command = move_counter).pack()
  487. Label(dice_result, text = "").pack()
  488.  
  489. def random_number(roll):
  490. global random_numb
  491. global counter
  492. counter = 0
  493. while roll==True:
  494. random_numb = random.randint(1,6)
  495. roll=False
  496.  
  497. def move_counter():
  498. delete_1()
  499. if counter!=7:
  500. class Board(Tk):
  501. def __init__(self, width, height, cellsize):
  502. Tk.__init__(self)
  503. self.cellsize = cellsize
  504. self.canvas = Canvas(self, width=width, height=height)
  505. self.canvas.pack()
  506. def draw_rectangle(self, x1, y1, x2, y2, color):
  507. self.canvas.create_rectangle(x1, y1, x2, y2, fill=color, outline="black")
  508. def draw_circle(self, x1, y1, x2, y2, color):
  509. self.canvas.create_oval(x1, y1, x2, y2, fill=color, outline="black")
  510. def coordinates(self, event):
  511. i = int(event.x / self.cellsize)
  512. j = int(event.y / self.cellsize)
  513. global i
  514. global j
  515.  
  516. size = 80
  517. global board
  518. board = Board(675, 675, size)
  519. board.title("Board")
  520. logicBoard = [[0, 0, 0, 0, 0, 0, 0, 0],
  521. [0, 0, 0, 0, 0, 0, 0, 0],
  522. [0, 0, 0, 0, 0, 0, 0, 0],
  523. [0, 0, 0, 0, 0, 0, 0, 0],
  524. [0, 0, 0, 0, 0, 0, 0, 0],
  525. [0, 0, 0, 0, 0, 0, 0, 0],
  526. [0, 0, 0, 0, 0, 0, 0, 0],
  527. [0, 0, 0, 0, 0, 0, 0, 0]]
  528. for (i, j) in product(range(8), range(8)):
  529. coordX1 = (i * size)
  530. coordY1 = (j * size)
  531. coordX2 = coordX1 + size
  532. coordY2 = coordY1 + size
  533. if coordX1==0 and coordY1==0 and coordX2==80 and coordY2==80 or coordX1==0 and coordY1==560 and coordX2==80 and coordY2==640:
  534. board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, "blue")
  535. else:
  536. board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, "white")
  537. cell = logicBoard[i][j]
  538. pawnColor = "red"
  539. global current_pos_x1
  540. global current_pos_x2
  541. current_pos_x1= random_numb*80 + current_pos_x1
  542. current_pos_x2= (random_numb*80) + current_pos_x2
  543. board.draw_circle((current_pos_x1), 560, (current_pos_x2), 600, pawnColor)
  544. board.mainloop()
  545. delete_2()
  546.  
  547. def game():
  548. class Board(Tk):
  549. def __init__(self, width, height, cellsize):
  550. Tk.__init__(self)
  551. self.cellsize = cellsize
  552. self.canvas = Canvas(self, width=width, height=height)
  553. self.canvas.pack()
  554. def draw_rectangle(self, x1, y1, x2, y2, color):
  555. self.canvas.create_rectangle(x1, y1, x2, y2, fill=color, outline="black")
  556. def draw_circle(self, x1, y1, x2, y2, color):
  557. self.canvas.create_oval(x1, y1, x2, y2, fill=color, outline="black")
  558. def coordinates(self, event):
  559. i = int(event.x / self.cellsize)
  560. j = int(event.y / self.cellsize)
  561. global i
  562. global j
  563.  
  564. size = 80
  565. global board
  566. board = Board(675, 675, size)
  567. board.title("Board")
  568. logicBoard = [[0, 0, 0, 0, 0, 0, 0, 0],
  569. [0, 0, 0, 0, 0, 0, 0, 0],
  570. [0, 0, 0, 0, 0, 0, 0, 0],
  571. [0, 0, 0, 0, 0, 0, 0, 0],
  572. [0, 0, 0, 0, 0, 0, 0, 0],
  573. [0, 0, 0, 0, 0, 0, 0, 0],
  574. [0, 0, 0, 0, 0, 0, 0, 0],
  575. [0, 0, 0, 0, 0, 0, 0, 0]]
  576. for (i, j) in product(range(8), range(8)):
  577. coordX1 = (i * size)
  578. coordY1 = (j * size)
  579. coordX2 = coordX1 + size
  580. coordY2 = coordY1 + size
  581. if coordX1==0 and coordY1==0 and coordX2==80 and coordY2==80 or coordX1==0 and coordY1==560 and coordX2==80 and coordY2==640:
  582. board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, "blue")
  583. else:
  584. board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, "white")
  585. cell = logicBoard[i][j]
  586. pawnColor = "red"
  587. global current_pos_x1
  588. global current_pos_x2
  589. current_pos_x1 = 0
  590. current_pos_x2 = 40
  591. board.draw_circle(current_pos_x1, 40, current_pos_x2, 0, pawnColor)
  592. board.mainloop()
  593.  
  594. def end():
  595. global winner
  596. winner = Tk()
  597. winner.title("Winner")
  598. winner.geometry("300x250")
  599.  
  600. Label(winner, text = "YOU HAVE WON", font = ("Comic Sans MS", 15)).pack()
  601. Label(winner, text = "").pack()
  602. Label(winner, text = "JAMES", font = ("Comic Sans MS", 10)).pack()
  603. Label(winner, text = "").pack()
  604. Label(winner, text = "You are now level: 2").pack()
  605. Label(winner, text = "").pack()
  606. Label(winner, text = "You have won : 1 game").pack()
  607. Label(winner, text = "").pack()
  608. Button(winner, text = "Exit", height = "2", width = "30", command = quit).pack()
  609. Label(winner, text = "").pack()
  610.  
  611. def quit():
  612. dice.destroy()
  613. winner.destroy()
  614. board.destroy()
  615. def extra():
  616. print("2")
  617.  
  618. def extra1():
  619. screen_2.destroy()
  620. dice()
  621.  
  622. def session():
  623. global screen_1
  624. screen_1 = Tk()
  625. screen_1.title("Game")
  626. screen_1.geometry("400x300")
  627.  
  628. Label(screen_1, text = "Welcome to the game").pack()
  629. Label(screen_1, text = "").pack()
  630. Button(screen_1, text = "Play", height = "2", width = "30", command = play).pack()
  631. Label(screen_1, text = "").pack()
  632. Button(screen_1, text = "Profile", height = "2", width = "30", command = extra).pack()
  633. Label(screen_1, text = "").pack()
  634. Button(screen_1, text = "Log out", height = "1", width = "10",command = extra).pack()
  635. screen_1.mainloop()
  636.  
  637. def play():
  638. screen_1.destroy()
  639. global screen_2
  640. screen_2 = Tk()
  641. screen_2.title("Game")
  642. screen_2.geometry("400x300")
  643.  
  644. Label(screen_2, text = "Choose the number of players:").pack()
  645. Label(screen_2, text = "").pack()
  646. Button(screen_2, text = "1 - player", height = "2", width = "30", command = extra).pack()
  647. Label(screen_2, text = "").pack()
  648. Button(screen_2, text = "2 - players", height = "2", width = "30", command = game).pack()
  649. Label(screen_2, text = "").pack()
  650.  
  651.  
  652. def delete_1():
  653. dice_result.destroy()
  654. board.destroy()
  655.  
  656. def delete_2():
  657. dice.destroy()
  658. dice()
  659.  
  660. def game():
  661. extra1()
  662. class Board(Tk):
  663. def __init__(self, width, height, cellsize):
  664. Tk.__init__(self)
  665. self.cellsize = cellsize
  666. self.canvas = Canvas(self, width=width, height=height)
  667. self.canvas.pack()
  668. def draw_rectangle(self, x1, y1, x2, y2, color):
  669. self.canvas.create_rectangle(x1, y1, x2, y2, fill=color, outline="black")
  670. def draw_circle(self, x1, y1, x2, y2, color):
  671. self.canvas.create_oval(x1, y1, x2, y2, fill=color, outline="black")
  672. def coordinates(self, event):
  673. i = int(event.x / self.cellsize)
  674. j = int(event.y / self.cellsize)
  675. global i
  676. global j
  677.  
  678. size = 80
  679. global board
  680. board = Board(675, 675, size)
  681. board.title("Board")
  682. logicBoard = [[0, 0, 0, 0, 0, 0, 0, 0],
  683. [0, 0, 0, 0, 0, 0, 0, 0],
  684. [0, 0, 0, 0, 0, 0, 0, 0],
  685. [0, 0, 0, 0, 0, 0, 0, 0],
  686. [0, 0, 0, 0, 0, 0, 0, 0],
  687. [0, 0, 0, 0, 0, 0, 0, 0],
  688. [0, 0, 0, 0, 0, 0, 0, 0],
  689. [0, 0, 0, 0, 0, 0, 0, 0]]
  690. for (i, j) in product(range(8), range(8)):
  691. coordX1 = (i * size)
  692. coordY1 = (j * size)
  693. coordX2 = coordX1 + size
  694. coordY2 = coordY1 + size
  695. if coordX1==0 and coordY1==0 and coordX2==80 and coordY2==80 or coordX1==0 and coordY1==560 and coordX2==80 and coordY2==640:
  696. board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, "blue")
  697. else:
  698. board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, "white")
  699. cell = logicBoard[i][j]
  700. pawnColor = "red"
  701. global current_pos_x1
  702. global current_pos_x2
  703. current_pos_x1 = 0
  704. current_pos_x2 = 40
  705. board.draw_circle(current_pos_x1, 560, current_pos_x2, 600, pawnColor)
  706. board.draw_circle(current_pos_x1+40, 600, current_pos_x2+40, 640, pawnColor)
  707. board.mainloop()
  708.  
  709. def dice():
  710. global dice
  711. dice = Tk()
  712. dice.title("Dice")
  713. dice.geometry("300x250")
  714.  
  715. Label(text = "It´s James´s turn!").pack()
  716. Label(text = "Click the button to throw the dice").pack()
  717. Button(text = "Throw", height = "2", width = "30", command = dice_throw).pack()
  718. Label(text = "").pack()
  719.  
  720. def dice_throw():
  721. counter=1
  722. if counter==1:
  723. global dice_result
  724. dice_result = Toplevel(dice)
  725. dice_result.geometry("150x100")
  726.  
  727. roll=True
  728. counter=counter+1
  729. random_number(roll)
  730. Label(dice_result, text = random_numb, font = ("Comic Sans MS", 15)).pack()
  731. Label(dice_result, text = "").pack()
  732. Button(dice_result, text = "Ok", height = "1", width = "10", command = move_counter).pack()
  733. Label(dice_result, text = "").pack()
  734.  
  735. def dice_throw2():
  736. dice_result2 = Toplevel(dice)
  737. dice_result2.geometry("150x100")
  738.  
  739. roll=True
  740. counter=counter+1
  741. random_number(roll)
  742. Label(dice_result2, text = random_numb, font = ("Comic Sans MS", 15)).pack()
  743. Label(dice_result2, text = "").pack()
  744. Button(dice_result2, text = "Ok", height = "1", width = "10", command = move_counter2).pack()
  745. Label(dice_result2, text = "").pack()
  746.  
  747.  
  748. def move_counter():
  749. delete_1()
  750. class Board(Tk):
  751. def __init__(self, width, height, cellsize):
  752. Tk.__init__(self)
  753. self.cellsize = cellsize
  754. self.canvas = Canvas(self, width=width, height=height)
  755. self.canvas.pack()
  756. def draw_rectangle(self, x1, y1, x2, y2, color):
  757. self.canvas.create_rectangle(x1, y1, x2, y2, fill=color, outline="black")
  758. def draw_circle(self, x1, y1, x2, y2, color):
  759. self.canvas.create_oval(x1, y1, x2, y2, fill=color, outline="black")
  760. def coordinates(self, event):
  761. i = int(event.x / self.cellsize)
  762. j = int(event.y / self.cellsize)
  763. global i
  764. global j
  765.  
  766. size = 80
  767. global board
  768. board = Board(675, 675, size)
  769. board.title("Board")
  770. logicBoard = [[0, 0, 0, 0, 0, 0, 0, 0],
  771. [0, 0, 0, 0, 0, 0, 0, 0],
  772. [0, 0, 0, 0, 0, 0, 0, 0],
  773. [0, 0, 0, 0, 0, 0, 0, 0],
  774. [0, 0, 0, 0, 0, 0, 0, 0],
  775. [0, 0, 0, 0, 0, 0, 0, 0],
  776. [0, 0, 0, 0, 0, 0, 0, 0],
  777. [0, 0, 0, 0, 0, 0, 0, 0]]
  778. for (i, j) in product(range(8), range(8)):
  779. coordX1 = (i * size)
  780. coordY1 = (j * size)
  781. coordX2 = coordX1 + size
  782. coordY2 = coordY1 + size
  783. if coordX1==0 and coordY1==0 and coordX2==80 and coordY2==80 or coordX1==0 and coordY1==560 and coordX2==80 and coordY2==640:
  784. board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, "blue")
  785. else:
  786. board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, "white")
  787. cell = logicBoard[i][j]
  788. pawnColor = "red"
  789. board.draw_circle((random_numb*80 + current_pos_x1), 560, ((random_numb*80) + current_pos_x2), 600, pawnColor)
  790. board.draw_circle(current_pos_x1+40, 600, current_pos_x2+40, 640, pawnColor)
  791. board.mainloop()
  792. delete_2()
  793.  
  794. def move_counter2():
  795. delete_1()
  796. class Board(Tk):
  797. def __init__(self, width, height, cellsize):
  798. Tk.__init__(self)
  799. self.cellsize = cellsize
  800. self.canvas = Canvas(self, width=width, height=height)
  801. self.canvas.pack()
  802. def draw_rectangle(self, x1, y1, x2, y2, color):
  803. self.canvas.create_rectangle(x1, y1, x2, y2, fill=color, outline="black")
  804. def draw_circle(self, x1, y1, x2, y2, color):
  805. self.canvas.create_oval(x1, y1, x2, y2, fill=color, outline="black")
  806. def coordinates(self, event):
  807. i = int(event.x / self.cellsize)
  808. j = int(event.y / self.cellsize)
  809. global i
  810. global j
  811.  
  812. size = 80
  813. global board
  814. board = Board(675, 675, size)
  815. board.title("Board")
  816. logicBoard = [[0, 0, 0, 0, 0, 0, 0, 0],
  817. [0, 0, 0, 0, 0, 0, 0, 0],
  818. [0, 0, 0, 0, 0, 0, 0, 0],
  819. [0, 0, 0, 0, 0, 0, 0, 0],
  820. [0, 0, 0, 0, 0, 0, 0, 0],
  821. [0, 0, 0, 0, 0, 0, 0, 0],
  822. [0, 0, 0, 0, 0, 0, 0, 0],
  823. [0, 0, 0, 0, 0, 0, 0, 0]]
  824. for (i, j) in product(range(8), range(8)):
  825. coordX1 = (i * size)
  826. coordY1 = (j * size)
  827. coordX2 = coordX1 + size
  828. coordY2 = coordY1 + size
  829. if coordX1==0 and coordY1==0 and coordX2==80 and coordY2==80 or coordX1==0 and coordY1==560 and coordX2==80 and coordY2==640:
  830. board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, "blue")
  831. else:
  832. board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, "white")
  833. cell = logicBoard[i][j]
  834. pawnColor = "red"
  835. global current_pos_x1
  836. global current_pos_x2
  837. board.draw_circle((current_pos_x1), 560, (current_pos_x2), 600, pawnColor)
  838. board.draw_circle((random_numb*80 + current_pos_x1)+40, 600, ((random_numb*80) + current_pos_x2)+40, 640, pawnColor)
  839. board.mainloop()
  840. delete_2()
  841.  
  842. session()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement