main.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. import tkinter as tk
  2. from random import shuffle
  3. from PIL import Image, ImageTk
  4. # Window
  5. root = tk.Tk()
  6. root.title("Elite Four")
  7. root.resizable(False, False)
  8. root.geometry("1280x720")
  9. # Canvas
  10. cv = tk.Canvas(root, width=1280, height=720)
  11. cv.place(x=0, y=0)
  12. # Game variables
  13. game_number = 4
  14. games_list = ["N", "C"] # To finish
  15. # Creation of the start menu
  16. start_button: tk.Button
  17. def menu():
  18. global start_button
  19. cv.delete("all")
  20. bg_rect = cv.create_rectangle(0, 0, 1280, 720, fill="#7993a3", outline="#7993a3")
  21. title = cv.create_text(640, 360, text="Elite Four", font=("Helvetica", 48, "bold"), fill="aliceblue")
  22. start_button = tk.Button(bg="#7993a3", fg="aliceblue", activebackground="aliceblue", activeforeground="#7993a3", text="START", font=("Helvetica", 24, "bold"), cursor="hand2", borderwidth=0, highlightthickness=0, command=start)
  23. start_button.place(x=576, y=600)
  24. def start():
  25. # To finish
  26. global game_number, games_list
  27. shuffle(games_list)
  28. game_number = 0
  29. start_button.destroy()
  30. next_game()
  31. def next_game():
  32. cv.delete("all")
  33. if game_number < len(games_list):
  34. if games_list[game_number] == "N":
  35. miss_n_init()
  36. elif games_list[game_number] == "C":
  37. mister_c_init()
  38. else:
  39. print("THE END") # TODO the end
  40. def reset():
  41. # To finish
  42. global game_number, miss_n_state, flour_qtt, flour_qtt_inc, sugar_qtt, sugar_qtt_inc, eggs_qtt, eggs_qtt_inc, milk_qtt, milk_qtt_inc, ingredients_height, mister_c_state, mister_c_dialog, mister_c_result, skyline_image, ptimage, mister_c_input, mister_c_score
  43. game_number = 4
  44. # Miss N
  45. miss_n_state = 0
  46. flour_qtt = 0
  47. flour_qtt_inc = True
  48. sugar_qtt = 0
  49. sugar_qtt_inc = True
  50. eggs_qtt = 0
  51. eggs_qtt_inc = True
  52. milk_qtt = 0
  53. milk_qtt_inc = True
  54. ingredients_height = 359
  55. # Mister C
  56. mister_c_state = 0
  57. mister_c_dialog = 0
  58. skyline_image = 0
  59. ptimage = None
  60. mister_c_input = tk.Entry(root)
  61. mister_c_input.bind("<Return>", enter)
  62. mister_c_score = 0
  63. menu()
  64. # General GUI functions
  65. def draw_dialog_area():
  66. dialog_rect = cv.create_rectangle(8, 500, 1272, 712, fill="#ffffff", outline="#ffffff")
  67. dialog_name_rect = cv.create_rectangle(580, 484, 700, 500, fill="#ffffff", outline="#ffffff")
  68. # Miss N game
  69. miss_n_state = 0
  70. flour_qtt = 0
  71. flour_qtt_inc = True
  72. sugar_qtt = 0
  73. sugar_qtt_inc = True
  74. eggs_qtt = 0
  75. eggs_qtt_inc = True
  76. milk_qtt = 0
  77. milk_qtt_inc = True
  78. miss_n_dialog: int
  79. flour_qtt_text: int
  80. sugar_qtt_text: int
  81. eggs_qtt_text: int
  82. milk_qtt_text: int
  83. ingredients_height = 359
  84. def miss_n_init():
  85. bg_rect = cv.create_rectangle(0, 0, 1280, 720, fill="#6abd9c", outline="#6abd9c")
  86. draw_dialog_area()
  87. dialog_name = cv.create_text(640, 500, text="Miss N", font=("Helvetica", 16, "bold"), fill="#163226")
  88. container_rect = cv.create_rectangle(1140, 80, 1240, 360, fill="#c9c9c9", outline="#c9c9c9")
  89. flour_rect = cv.create_rectangle(40, 100, 180, 280, fill="#fff4de", outline="#fff4de")
  90. flour_text = cv.create_text(110, 88, text="FLOUR", font=("Helvetica", 16), fill="#163226")
  91. sugar_rect = cv.create_rectangle(220, 160, 360, 280, fill="#ffb34f", outline="#ffb34f")
  92. sugar_top = cv.create_polygon(220, 160, 360, 160, 290, 100, fill="#ffb34f", outline="#ffb34f")
  93. sugar_text = cv.create_text(290, 88, text="SUGAR", font=("Helvetica", 16), fill="#163226")
  94. eggs_oval = cv.create_oval(400, 100, 540, 280, fill="#e6c493", outline="#e6c493")
  95. eggs_text = cv.create_text(470, 88, text="EGGS", font=("Helvetica", 16), fill="#163226")
  96. milk_rect = cv.create_rectangle(580, 160, 720, 280, fill="#ffffff", outline="#ffffff")
  97. milk_top = cv.create_rectangle(580, 100, 720, 160, fill="#0000cc", outline="#0000cc")
  98. milk_text = cv.create_text(650, 88, text="MILK", font=("Helvetica", 16), fill="#163226")
  99. miss_n_game()
  100. def miss_n_game():
  101. global miss_n_dialog, flour_qtt, flour_qtt_inc, flour_qtt_text, sugar_qtt, sugar_qtt_inc, sugar_qtt_text, eggs_qtt, eggs_qtt_inc, eggs_qtt_text, milk_qtt, milk_qtt_inc, milk_qtt_text
  102. if miss_n_state == 0:
  103. miss_n_dialog = cv.create_text(640, 606, text="Hiiiiii! I'm Miss N and in my game, you'll have to make a crêpe!\n\
  104. The rules are easy, just press SPACE when you think that a quantity is good.\n\
  105. If you follow the recipe, you win. If you don't, you lose.\n\
  106. Here is the recipe:\n\
  107. - 170g of flour\n\
  108. - 40g of sugar\n\
  109. - 3 eggs\n\
  110. - 600mL of milk\n\n\
  111. Press SPACE to start, good luck <3", font=("Helvetica", 12), fill="#163226")
  112. flour_qtt_text = cv.create_text(110, 308, text="", font=("Helvetica", 16), fill="#163226")
  113. sugar_qtt_text = cv.create_text(290, 308, text="", font=("Helvetica", 16), fill="#163226")
  114. eggs_qtt_text = cv.create_text(470, 308, text="", font=("Helvetica", 16), fill="#163226")
  115. milk_qtt_text = cv.create_text(650, 308, text="", font=("Helvetica", 16), fill="#163226")
  116. elif miss_n_state == 1:
  117. if flour_qtt_inc:
  118. flour_qtt += 1
  119. if flour_qtt == 400:
  120. flour_qtt_inc = False
  121. else:
  122. flour_qtt -= 1
  123. if flour_qtt == 0:
  124. flour_qtt_inc = True
  125. cv.itemconfig(flour_qtt_text, text=str(flour_qtt))
  126. root.after(8, miss_n_game)
  127. elif miss_n_state == 2:
  128. if sugar_qtt_inc:
  129. sugar_qtt += 1
  130. if sugar_qtt == 100:
  131. sugar_qtt_inc = False
  132. else:
  133. sugar_qtt -= 1
  134. if sugar_qtt == 0:
  135. sugar_qtt_inc = True
  136. cv.itemconfig(sugar_qtt_text, text=str(sugar_qtt))
  137. root.after(10, miss_n_game)
  138. elif miss_n_state == 3:
  139. if eggs_qtt_inc:
  140. eggs_qtt += 1
  141. if eggs_qtt == 5:
  142. eggs_qtt_inc = False
  143. else:
  144. eggs_qtt -= 1
  145. if eggs_qtt == 0:
  146. eggs_qtt_inc = True
  147. cv.itemconfig(eggs_qtt_text, text=str(eggs_qtt))
  148. root.after(140, miss_n_game)
  149. elif miss_n_state == 4:
  150. if milk_qtt_inc:
  151. milk_qtt += 1
  152. if milk_qtt == 1000:
  153. milk_qtt_inc = False
  154. else:
  155. milk_qtt -= 1
  156. if milk_qtt == 0:
  157. milk_qtt_inc = True
  158. cv.itemconfig(milk_qtt_text, text=str(milk_qtt))
  159. root.after(4, miss_n_game)
  160. def miss_n_end():
  161. global miss_n_state
  162. score = abs(flour_qtt - 170) * 3 + abs(sugar_qtt - 40) * 15 + abs(eggs_qtt - 3) * 200 + abs(milk_qtt - 600)
  163. if score < 300:
  164. cv.itemconfig(miss_n_dialog, text="Those crepes look delicious, great job!\nPress SPACE to continue <3")
  165. miss_n_state = 6
  166. else:
  167. cv.itemconfig(miss_n_dialog, text="It seems absolutely uneatable, I hope you'll do better next time.")
  168. root.after(5000, reset)
  169. # Mister C game
  170. mister_c_state = 0
  171. images_list = ["images/bucharest.png", "images/dallas.png", "images/dublin.png", "images/kualalumpur.png", "images/lagos.png", "images/lyon.png", "images/warsaw.png"]
  172. mister_c_answers = {"images/bucharest.png": ["bucharest", "Bucharest", "BUCHAREST"], "images/dallas.png": ["dallas", "Dallas", "DALLAS"], "images/dublin.png": ["dublin", "Dublin", "DUBLIN"], "images/kualalumpur.png": ["kuala lumpur", "Kuala lumpur", "Kuala Lumpur", "KUALA LUMPUR"], "images/lagos.png": ["lagos", "Lagos", "LAGOS"], "images/lyon.png": ["lyon", "Lyon", "LYON"], "images/warsaw.png": ["warsaw", "Warsaw", "WARSAW"]}
  173. mister_c_dialog: int
  174. mister_c_result: int
  175. skyline_image: int
  176. ptimage = None
  177. mister_c_input = tk.Entry(root)
  178. mister_c_score = 0
  179. def mister_c_init():
  180. shuffle(images_list)
  181. bg_rect = cv.create_rectangle(0, 0, 1280, 720, fill="#7cf27c", outline="#7cf27c")
  182. draw_dialog_area()
  183. dialog_name = cv.create_text(640, 500, text="Mister C", font=("Helvetica", 16, "bold"), fill="#0b6b0b")
  184. mister_c_game()
  185. def mister_c_game():
  186. global mister_c_dialog, skyline_image, ptimage
  187. if mister_c_state == 0:
  188. mister_c_dialog = cv.create_text(640, 606, text="Halloooo, my name is Mister C !\n\
  189. In my game, you'll have to recognize a city from his skyline, and the colors on the flag of its country.\n\
  190. You will see 3 images, and you'll have to make 2 correct guesses if you want to continue.\n\
  191. You only have one chance per image, good luck !\n\
  192. Press SPACE to start", font=("Helvetica", 12), fill="#0b6b0b")
  193. if mister_c_state == 1:
  194. cv.itemconfig(mister_c_dialog, text="Which city do you think it is ?")
  195. ptimage = ImageTk.PhotoImage(file=images_list[0])
  196. skyline_image = cv.create_image(640, 242, image=ptimage, anchor=tk.CENTER)
  197. mister_c_input.place(x=640, y=632, anchor=tk.CENTER)
  198. elif mister_c_state == 2:
  199. cv.itemconfig(mister_c_dialog, text="Which city do you think it is ?")
  200. ptimage = ImageTk.PhotoImage(file=images_list[1])
  201. cv.itemconfigure(skyline_image, image=ptimage)
  202. elif mister_c_state == 3:
  203. cv.itemconfig(mister_c_dialog, text="Which city do you think it is ?")
  204. ptimage = ImageTk.PhotoImage(file=images_list[2])
  205. cv.itemconfigure(skyline_image, image=ptimage)
  206. elif mister_c_state == 4:
  207. cv.itemconfig(mister_c_dialog, text="Well played!\nPress SPACE to continue.")
  208. elif mister_c_state == 5:
  209. cv.itemconfig(mister_c_dialog, text="You have to work a little more, see you later !")
  210. root.after(5000, reset)
  211. def mister_c_verif():
  212. global mister_c_score, mister_c_result
  213. if mister_c_input.get() in mister_c_answers[images_list[mister_c_state - 1]]:
  214. mister_c_score += 1
  215. if mister_c_state == 1:
  216. mister_c_result = cv.create_text(640, 680, text="Correct!", font=("Helvetica", 12), fill="#0b6b0b")
  217. else:
  218. cv.itemconfig(mister_c_result, text="Correct!")
  219. return True
  220. else:
  221. if mister_c_state == 1:
  222. mister_c_result = cv.create_text(640, 680, text="Wrong!", font=("Helvetica", 12), fill="#0b6b0b")
  223. else:
  224. cv.itemconfig(mister_c_result, text="Wrong!")
  225. return False
  226. # Handle the pressed keys
  227. def space(_):
  228. global game_number, miss_n_state, ingredients_height, mister_c_state
  229. if game_number < len(games_list):
  230. if games_list[game_number] == "N":
  231. if miss_n_state == 0:
  232. miss_n_state = 1
  233. miss_n_game()
  234. elif miss_n_state == 1:
  235. miss_n_state = 2
  236. ingredients_height -= round(flour_qtt / 3)
  237. cv.create_rectangle(1141, 359, 1239, ingredients_height, fill="#fff4de", outline="#fff4de")
  238. elif miss_n_state == 2:
  239. miss_n_state = 3
  240. cv.create_rectangle(1141, ingredients_height, 1239, ingredients_height - round(sugar_qtt / 3), fill="#ffb34f", outline="#ffb34f")
  241. ingredients_height -= round(sugar_qtt / 3)
  242. elif miss_n_state == 3:
  243. miss_n_state = 4
  244. cv.create_line(1141, ingredients_height, 1239, ingredients_height, fill="#ffffff")
  245. cv.create_line(1180, ingredients_height, 1200, ingredients_height, fill="#ffcc00")
  246. ingredients_height -= 1
  247. elif miss_n_state == 4:
  248. miss_n_state = 5
  249. cv.create_rectangle(1141, ingredients_height, 1239, ingredients_height - round(milk_qtt / 3), fill="#ffffff", outline="#ffffff")
  250. miss_n_end()
  251. elif miss_n_state == 6:
  252. game_number += 1
  253. next_game()
  254. elif games_list[game_number] == "C":
  255. if mister_c_state == 0:
  256. mister_c_state = 1
  257. mister_c_game()
  258. elif mister_c_state == 4:
  259. game_number += 1
  260. next_game()
  261. def enter(_):
  262. global mister_c_state, mister_c_score
  263. if game_number < 4:
  264. if games_list[game_number] == "C":
  265. if mister_c_state == 1:
  266. mister_c_verif()
  267. mister_c_input.delete(0, tk.END)
  268. mister_c_state = 2
  269. mister_c_game()
  270. elif mister_c_state == 2:
  271. mister_c_verif()
  272. mister_c_input.delete(0, tk.END)
  273. if mister_c_score == 2:
  274. mister_c_state = 4
  275. mister_c_input.destroy()
  276. else:
  277. mister_c_state = 3
  278. mister_c_game()
  279. elif mister_c_state == 3:
  280. if mister_c_verif():
  281. mister_c_state = 4
  282. mister_c_game()
  283. else:
  284. mister_c_state = 5
  285. mister_c_game()
  286. mister_c_input.destroy()
  287. root.bind("<space>", space)
  288. mister_c_input.bind("<Return>", enter)
  289. menu()
  290. root.mainloop()