· 6 years ago · May 06, 2019, 01:32 PM
1from tkinter import *
2from tkinter import Tk, Canvas
3from itertools import product
4from Game import game
5import os
6import sqlite3
7import random
8from itertools import product
9
10#These are the functions I made to delete unwanted screens at given moments
11def delete():
12 screen.destroy()
13
14def delete2():
15 screen2.destroy()
16 screen3.destroy()
17 screen4.destroy()
18
19def delete3():
20 screen10.destroy()
21
22def delete4():
23 screen11.destroy()
24
25def delete5():
26 screen9.destroy()
27
28def delete6():
29 screen13.destroy()
30
31def delete7():
32 screen7.destroy()
33
34def delete8():
35 screen2.destroy()
36 screen14.destroy()
37 screen7.destroy()
38
39def back():
40 screen2.destroy()
41
42def logout():
43 screen2.destroy()
44 screen5.destroy()
45
46def retry():
47 screen6.destroy()
48
49#This is my main screen menu where all the screens will be created from this one
50def 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
66def 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
80def 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
108def 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
123def 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
154def 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
169def 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
206def 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
235def 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
265def 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
274def 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
283def 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
292def 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
303def 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
314def 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
350def play():
351 screen5.destroy()
352 dice()
353 game()
354
355#Profile button gives a screen with info on the user
356def 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
373def 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
394def 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
410main_screen()
411
412def delete_1():
413 dice_result.destroy()
414 board.destroy()
415
416def delete_2():
417 dice.destroy()
418 dice()
419
420def 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 #This is to create the marks for the start and the beginning of the board (in blue)
455 if coordX1==0 and coordY1==0 and coordX2==80 and coordY2==80 or coordX1==0 and coordY1==560 and coordX2==80 and coordY2==640:
456 board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, "blue")
457 #This is just to create the rest of the board (in white)
458 else:
459 board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, "white")
460 cell = logicBoard[i][j]
461 pawnColor = "red"
462 global current_pos_x1
463 global current_pos_x2
464 current_pos_x1 = 0
465 current_pos_x2 = 40
466 #This is to create the counter
467 board.draw_circle(current_pos_x1, 560, current_pos_x2, 600, pawnColor)
468 board.mainloop()
469
470#This is to create a screen with a button to call the dice
471def dice():
472 global dice
473 dice = Tk()
474 dice.title("Dice")
475 dice.geometry("300x250")
476
477 Label(text = "Click the button to throw the dice").pack()
478 Button(text = "Throw", height = "2", width = "30", command = dice_throw).pack()
479 Label(text = "").pack()
480
481#This is to actually display the number given by the dice
482def dice_throw():
483 global dice_result
484 dice_result = Toplevel(dice)
485 dice_result.geometry("150x100")
486
487 roll=True
488 random_number(roll)
489 Label(dice_result, text = random_numb, font = ("Comic Sans MS", 15)).pack()
490 Label(dice_result, text = "").pack()
491 Button(dice_result, text = "Ok", height = "1", width = "10", command = move_counter).pack()
492 Label(dice_result, text = "").pack()
493
494#This gives the random number from 1 to 6 simulating a dice
495def random_number(roll):
496 global random_numb
497 global counter
498 counter = 0
499 while roll==True:
500 random_numb = random.randint(1,6)
501 roll=False
502
503#This redraws the board to move the counter the number of slots given by the dice
504def move_counter():
505 delete_1()
506 if counter!=7:
507 class Board(Tk):
508 def __init__(self, width, height, cellsize):
509 Tk.__init__(self)
510 self.cellsize = cellsize
511 self.canvas = Canvas(self, width=width, height=height)
512 self.canvas.pack()
513 def draw_rectangle(self, x1, y1, x2, y2, color):
514 self.canvas.create_rectangle(x1, y1, x2, y2, fill=color, outline="black")
515 def draw_circle(self, x1, y1, x2, y2, color):
516 self.canvas.create_oval(x1, y1, x2, y2, fill=color, outline="black")
517 def coordinates(self, event):
518 i = int(event.x / self.cellsize)
519 j = int(event.y / self.cellsize)
520 global i
521 global j
522
523 size = 80
524 global board
525 board = Board(675, 675, size)
526 board.title("Board")
527 logicBoard = [[0, 0, 0, 0, 0, 0, 0, 0],
528 [0, 0, 0, 0, 0, 0, 0, 0],
529 [0, 0, 0, 0, 0, 0, 0, 0],
530 [0, 0, 0, 0, 0, 0, 0, 0],
531 [0, 0, 0, 0, 0, 0, 0, 0],
532 [0, 0, 0, 0, 0, 0, 0, 0],
533 [0, 0, 0, 0, 0, 0, 0, 0],
534 [0, 0, 0, 0, 0, 0, 0, 0]]
535 for (i, j) in product(range(8), range(8)):
536 coordX1 = (i * size)
537 coordY1 = (j * size)
538 coordX2 = coordX1 + size
539 coordY2 = coordY1 + size
540 if coordX1==0 and coordY1==0 and coordX2==80 and coordY2==80 or coordX1==0 and coordY1==560 and coordX2==80 and coordY2==640:
541 board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, "blue")
542 else:
543 board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, "white")
544 cell = logicBoard[i][j]
545 pawnColor = "red"
546 global current_pos_x1
547 global current_pos_x2
548 current_pos_x1= random_numb*80 + current_pos_x1
549 current_pos_x2= (random_numb*80) + current_pos_x2
550 board.draw_circle((current_pos_x1), 560, (current_pos_x2), 600, pawnColor)
551 board.mainloop()
552 delete_2()
553
554#This draws the board and creates the ends and the counters
555def game():
556 class Board(Tk):
557 def __init__(self, width, height, cellsize):
558 Tk.__init__(self)
559 self.cellsize = cellsize
560 self.canvas = Canvas(self, width=width, height=height)
561 self.canvas.pack()
562 def draw_rectangle(self, x1, y1, x2, y2, color):
563 self.canvas.create_rectangle(x1, y1, x2, y2, fill=color, outline="black")
564 def draw_circle(self, x1, y1, x2, y2, color):
565 self.canvas.create_oval(x1, y1, x2, y2, fill=color, outline="black")
566 def coordinates(self, event):
567 i = int(event.x / self.cellsize)
568 j = int(event.y / self.cellsize)
569 global i
570 global j
571
572 size = 80
573 global board
574 board = Board(675, 675, size)
575 board.title("Board")
576 logicBoard = [[0, 0, 0, 0, 0, 0, 0, 0],
577 [0, 0, 0, 0, 0, 0, 0, 0],
578 [0, 0, 0, 0, 0, 0, 0, 0],
579 [0, 0, 0, 0, 0, 0, 0, 0],
580 [0, 0, 0, 0, 0, 0, 0, 0],
581 [0, 0, 0, 0, 0, 0, 0, 0],
582 [0, 0, 0, 0, 0, 0, 0, 0],
583 [0, 0, 0, 0, 0, 0, 0, 0]]
584 for (i, j) in product(range(8), range(8)):
585 coordX1 = (i * size)
586 coordY1 = (j * size)
587 coordX2 = coordX1 + size
588 coordY2 = coordY1 + size
589 if coordX1==0 and coordY1==0 and coordX2==80 and coordY2==80 or coordX1==0 and coordY1==560 and coordX2==80 and coordY2==640:
590 board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, "blue")
591 else:
592 board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, "white")
593 cell = logicBoard[i][j]
594 pawnColor = "red"
595 global current_pos_x1
596 global current_pos_x2
597 current_pos_x1 = 0
598 current_pos_x2 = 40
599 board.draw_circle(current_pos_x1, 40, current_pos_x2, 0, pawnColor)
600 board.mainloop()
601
602#This is for the end of the game, it displays a winner message
603def end():
604 global winner
605 winner = Tk()
606 winner.title("Winner")
607 winner.geometry("300x250")
608
609 Label(winner, text = "YOU HAVE WON", font = ("Comic Sans MS", 15)).pack()
610 Label(winner, text = "").pack()
611 Label(winner, text = username, font = ("Comic Sans MS", 10)).pack()
612 Label(winner, text = "").pack()
613 Label(winner, text = "You are now level: userLevel").pack()
614 Label(winner, text = "").pack()
615 Label(winner, text = "You have won :"+ games_won +"game").pack()
616 Label(winner, text = "").pack()
617 Button(winner, text = "Exit", height = "2", width = "30", command = quit).pack()
618 Label(winner, text = "").pack()
619
620#These are functions to delete determined unwanted screens
621def quit():
622 dice.destroy()
623 winner.destroy()
624 board.destroy()
625
626def extra():
627 print("2")
628
629def extra1():
630 screen_2.destroy()
631 dice()
632
633#Menu to choose 1 or 2 players
634def play():
635 screen_1.destroy()
636 global screen_2
637 screen_2 = Tk()
638 screen_2.title("Game")
639 screen_2.geometry("400x300")
640
641 Label(screen_2, text = "Choose the number of players:").pack()
642 Label(screen_2, text = "").pack()
643 Button(screen_2, text = "1 - player", height = "2", width = "30", command = extra).pack()
644 Label(screen_2, text = "").pack()
645 Button(screen_2, text = "2 - players", height = "2", width = "30", command = game).pack()
646 Label(screen_2, text = "").pack()
647
648#These are functions to delete determined unwanted screens
649def delete_1():
650 dice_result.destroy()
651 board.destroy()
652
653def delete_2():
654 dice.destroy()
655 dice()
656
657#This is the game for 2 players
658def game():
659 extra1()
660 class Board(Tk):
661 def __init__(self, width, height, cellsize):
662 Tk.__init__(self)
663 self.cellsize = cellsize
664 self.canvas = Canvas(self, width=width, height=height)
665 self.canvas.pack()
666 def draw_rectangle(self, x1, y1, x2, y2, color):
667 self.canvas.create_rectangle(x1, y1, x2, y2, fill=color, outline="black")
668 def draw_circle(self, x1, y1, x2, y2, color):
669 self.canvas.create_oval(x1, y1, x2, y2, fill=color, outline="black")
670 def coordinates(self, event):
671 i = int(event.x / self.cellsize)
672 j = int(event.y / self.cellsize)
673 global i
674 global j
675
676 size = 80
677 global board
678 board = Board(675, 675, size)
679 board.title("Board")
680 logicBoard = [[0, 0, 0, 0, 0, 0, 0, 0],
681 [0, 0, 0, 0, 0, 0, 0, 0],
682 [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 for (i, j) in product(range(8), range(8)):
689 coordX1 = (i * size)
690 coordY1 = (j * size)
691 coordX2 = coordX1 + size
692 coordY2 = coordY1 + size
693 if coordX1==0 and coordY1==0 and coordX2==80 and coordY2==80 or coordX1==0 and coordY1==560 and coordX2==80 and coordY2==640:
694 board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, "blue")
695 else:
696 board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, "white")
697 cell = logicBoard[i][j]
698 pawnColor = "red"
699 global current_pos_x1
700 global current_pos_x2
701 current_pos_x1 = 0
702 current_pos_x2 = 40
703 #2 counters
704 board.draw_circle(current_pos_x1, 560, current_pos_x2, 600, pawnColor)
705 board.draw_circle(current_pos_x1+40, 600, current_pos_x2+40, 640, pawnColor)
706 board.mainloop()
707
708def dice():
709 global dice
710 dice = Tk()
711 dice.title("Dice")
712 dice.geometry("300x250")
713
714 Label(text = "It´s James´s turn!").pack()
715 Label(text = "Click the button to throw the dice").pack()
716 Button(text = "Throw", height = "2", width = "30", command = dice_throw).pack()
717 Label(text = "").pack()
718
719def dice_throw():
720 counter=1
721 if counter==1:
722 global dice_result
723 dice_result = Toplevel(dice)
724 dice_result.geometry("150x100")
725
726 roll=True
727 counter=counter+1
728 random_number(roll)
729 Label(dice_result, text = random_numb, font = ("Comic Sans MS", 15)).pack()
730 Label(dice_result, text = "").pack()
731 Button(dice_result, text = "Ok", height = "1", width = "10", command = move_counter).pack()
732 Label(dice_result, text = "").pack()
733
734#2 dice
735def 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#For first player to move counter
748def 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#For 2nd player to move counter
795def move_counter2():
796 delete_1()
797 class Board(Tk):
798 def __init__(self, width, height, cellsize):
799 Tk.__init__(self)
800 self.cellsize = cellsize
801 self.canvas = Canvas(self, width=width, height=height)
802 self.canvas.pack()
803 def draw_rectangle(self, x1, y1, x2, y2, color):
804 self.canvas.create_rectangle(x1, y1, x2, y2, fill=color, outline="black")
805 def draw_circle(self, x1, y1, x2, y2, color):
806 self.canvas.create_oval(x1, y1, x2, y2, fill=color, outline="black")
807 def coordinates(self, event):
808 i = int(event.x / self.cellsize)
809 j = int(event.y / self.cellsize)
810 global i
811 global j
812
813 size = 80
814 global board
815 board = Board(675, 675, size)
816 board.title("Board")
817 logicBoard = [[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 [0, 0, 0, 0, 0, 0, 0, 0]]
825 for (i, j) in product(range(8), range(8)):
826 coordX1 = (i * size)
827 coordY1 = (j * size)
828 coordX2 = coordX1 + size
829 coordY2 = coordY1 + size
830 if coordX1==0 and coordY1==0 and coordX2==80 and coordY2==80 or coordX1==0 and coordY1==560 and coordX2==80 and coordY2==640:
831 board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, "blue")
832 else:
833 board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, "white")
834 cell = logicBoard[i][j]
835 pawnColor = "red"
836 global current_pos_x1
837 global current_pos_x2
838 board.draw_circle((current_pos_x1), 560, (current_pos_x2), 600, pawnColor)
839 board.draw_circle((random_numb*80 + current_pos_x1)+40, 600, ((random_numb*80) + current_pos_x2)+40, 640, pawnColor)
840 board.mainloop()
841 delete_2()
842
843session()
844
845#Create the database
846conn = sqlite3.connect("Database.db")
847c = conn.cursor()
848
849#Create a table in the database
850def create_table():
851 c.execute("CREATE TABLE IF NOT EXISTS questions(level INTEGER, type TEXT, question TEXT, answer TEXT)")
852
853#Enter data to the table in columns
854def data_entry():
855 c.execute("INSERT INTO questions VALUES(1, 'Translation', 'How do you say DOG in spanish?', 'Perro')")
856 c.execute("INSERT INTO questions VALUES(1, 'Translation', 'How do you say PAPER in spanish?', 'Papel')")
857 c.execute("INSERT INTO questions VALUES(1, 'Translation', 'How do you say COMPUTER in spanish?', 'Ordenador')")
858 c.execute("INSERT INTO questions VALUES(1, 'Translation', 'How do you say PENCIL in spanish?', 'Lapiz')")
859 c.execute("INSERT INTO questions VALUES(1, 'Translation', 'How do you say BOOK in spanish?', 'Libro')")
860 c.execute("INSERT INTO questions VALUES(1, 'Translation', 'How do you say STAIRS in spanish?', 'Escaleras')")
861 c.execute("INSERT INTO questions VALUES(1, 'Synonyms', 'What is a synonym of GRANDE?', 'Enorme')")
862 c.execute("INSERT INTO questions VALUES(1, 'Spelling', 'How do you spell HORSE in spanish?', 'Caballo')")
863 c.execute("INSERT INTO questions VALUES(2, 'Translation', 'How do you say BABY in spanish?', 'Bebe')")
864 c.execute("INSERT INTO questions VALUES(3, 'Spelling', 'How do you spell RAIN in spanish?', 'Lluvia')")
865 c.execute("INSERT INTO questions VALUES(4, 'Synonyms', 'What is a synonym of DIFICIL?', 'Complicado')")
866 c.execute("INSERT INTO questions VALUES(5, 'Translation', 'How do you say SKYSCRAPER in spanish?', 'Rascacielos')")
867 conn.commit()
868
869#Quit function
870def quit1():
871 screen_quest.destroy()
872
873#To fetch the records and put them in a list
874def read_from_db():
875 c.execute("SELECT * FROM questions")
876 global list1
877 list1= []
878 for row in c.fetchall():
879 for column in row:
880 list1.append(column)
881 conn.commit()
882 c.close()
883 conn.close()
884
885#Random question from the list
886def random1():
887 global random_numb2
888 list2 =[2]
889 for x in range (1,11):
890 numbers = (4*x)+2
891 list2.append(numbers)
892 random_numb2 = random.choice(list2)
893
894#Display the question in a screen and give an entry for the user
895def disp_quest():
896 global screen_quest
897 global answer_entered
898 global list1
899 screen_quest = Tk()
900 screen_quest.title("Question")
901 screen_quest.geometry("300x250")
902
903 answer_entered = StringVar()
904 Label(screen_quest, text = "Level: " + str(list1[random_numb2-2]) + " " + "Type: " + list1[random_numb2-1]).pack()
905 Label(screen_quest, text = list1[random_numb2] ).pack()
906 answer_entry = Entry(screen_quest, textvariable = answer_entered).pack()
907 Button(text = "OK", command = check_ans).pack()
908 screen_quest.mainloop()
909
910#Check the users entry to the real answer saved in the database
911def check_ans():
912 ans_get=answer_entered.get()
913 if ans_get==list1[random_numb2+1]:
914 Label(screen_quest, text = "Correct!", fg = "green", font = ("calibri", 11) ).pack()
915 else:
916 Label(screen_quest, text = "Incorrect, correct answer: " + list1[random_numb2+1] , fg = "red", font = ("calibri", 11) ).pack()
917 Button(screen_quest, text = "Finish", command = quit1).pack()
918
919create_table()
920random1()
921read_from_db()
922disp_quest()
923conn.close()