main.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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, 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. skyline_image: int
  175. ptimage = None
  176. mister_c_input = tk.Entry(root)
  177. mister_c_score = 0
  178. def mister_c_init():
  179. shuffle(images_list)
  180. bg_rect = cv.create_rectangle(0, 0, 1280, 720, fill="#7cf27c", outline="#7cf27c")
  181. draw_dialog_area()
  182. dialog_name = cv.create_text(640, 500, text="Mister C", font=("Helvetica", 16, "bold"), fill="#0b6b0b")
  183. mister_c_game()
  184. def mister_c_game():
  185. global mister_c_dialog, skyline_image, ptimage
  186. if mister_c_state == 0:
  187. mister_c_dialog = cv.create_text(640, 606, text="Halloooo, my name is Mister C !\n\
  188. In my game, you'll have to recognize a city from his skyline, and the colors on the flag of its country.\n\
  189. You will see 3 images, and you'll have to make 2 correct guesses if you want to continue.\n\
  190. You only have one chance per image, good luck !\n\
  191. Press SPACE to start", font=("Helvetica", 12), fill="#0b6b0b")
  192. if mister_c_state == 1:
  193. cv.itemconfig(mister_c_dialog, text="Which city do you think it is ?")
  194. ptimage = ImageTk.PhotoImage(file=images_list[0])
  195. skyline_image = cv.create_image(640, 242, image=ptimage, anchor=tk.CENTER)
  196. mister_c_input.place(x=640, y=680, anchor=tk.CENTER)
  197. elif mister_c_state == 2:
  198. cv.itemconfig(mister_c_dialog, text="Which city do you think it is ?")
  199. ptimage = ImageTk.PhotoImage(file=images_list[1])
  200. cv.itemconfigure(skyline_image, image=ptimage)
  201. elif mister_c_state == 3:
  202. cv.itemconfig(mister_c_dialog, text="Which city do you think it is ?")
  203. ptimage = ImageTk.PhotoImage(file=images_list[2])
  204. cv.itemconfigure(skyline_image, image=ptimage)
  205. elif mister_c_state == 4:
  206. cv.itemconfig(mister_c_dialog, text="Well played!\nPress SPACE to continue.")
  207. elif mister_c_state == 5:
  208. cv.itemconfig(mister_c_dialog, text="You have to work a little more, see you later !")
  209. root.after(5000, reset)
  210. def mister_c_verif():
  211. global mister_c_score
  212. if mister_c_input.get() in mister_c_answers[images_list[mister_c_state - 1]]:
  213. mister_c_score += 1
  214. return True
  215. else:
  216. return False
  217. # Handle the pressed keys
  218. def space(_):
  219. global game_number, miss_n_state, ingredients_height, mister_c_state
  220. if game_number < len(games_list):
  221. if games_list[game_number] == "N":
  222. if miss_n_state == 0:
  223. miss_n_state = 1
  224. miss_n_game()
  225. elif miss_n_state == 1:
  226. miss_n_state = 2
  227. ingredients_height -= round(flour_qtt / 3)
  228. cv.create_rectangle(1141, 359, 1239, ingredients_height, fill="#fff4de", outline="#fff4de")
  229. elif miss_n_state == 2:
  230. miss_n_state = 3
  231. cv.create_rectangle(1141, ingredients_height, 1239, ingredients_height - round(sugar_qtt / 3), fill="#ffb34f", outline="#ffb34f")
  232. ingredients_height -= round(sugar_qtt / 3)
  233. elif miss_n_state == 3:
  234. miss_n_state = 4
  235. cv.create_line(1141, ingredients_height, 1239, ingredients_height, fill="#ffffff")
  236. cv.create_line(1180, ingredients_height, 1200, ingredients_height, fill="#ffcc00")
  237. ingredients_height -= 1
  238. elif miss_n_state == 4:
  239. miss_n_state = 5
  240. cv.create_rectangle(1141, ingredients_height, 1239, ingredients_height - round(milk_qtt / 3), fill="#ffffff", outline="#ffffff")
  241. miss_n_end()
  242. elif miss_n_state == 6:
  243. game_number += 1
  244. next_game()
  245. elif games_list[game_number] == "C":
  246. if mister_c_state == 0:
  247. mister_c_state = 1
  248. mister_c_game()
  249. elif mister_c_state == 4:
  250. game_number += 1
  251. next_game()
  252. def enter(_):
  253. global mister_c_state, mister_c_score
  254. if game_number < 4:
  255. if games_list[game_number] == "C":
  256. if mister_c_state == 1:
  257. mister_c_verif()
  258. mister_c_input.delete(0, tk.END)
  259. mister_c_state = 2
  260. mister_c_game()
  261. elif mister_c_state == 2:
  262. mister_c_verif()
  263. mister_c_input.delete(0, tk.END)
  264. if mister_c_score == 2:
  265. mister_c_state = 4
  266. mister_c_input.destroy()
  267. else:
  268. mister_c_state = 3
  269. mister_c_game()
  270. elif mister_c_state == 3:
  271. if mister_c_verif():
  272. mister_c_state = 4
  273. mister_c_game()
  274. else:
  275. mister_c_state = 5
  276. mister_c_game()
  277. mister_c_input.destroy()
  278. root.bind("<space>", space)
  279. mister_c_input.bind("<Return>", enter)
  280. menu()
  281. root.mainloop()