Advertisement
Guest User

jbb

a guest
Jul 16th, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.60 KB | None | 0 0
  1. #Imports tkinter which is used to draw the canvas on which the images will be placed on.
  2. from tkinter import *
  3. import random
  4.  
  5. #Main subroutine
  6. def main():
  7. global window
  8. global tkinter
  9. global canvas
  10. global cdtimer
  11. global scorecounter
  12. global pressed
  13. pressed = ''
  14. window = Tk()
  15. cdtimer = 61
  16. scorecounter = 0
  17. _timer = None
  18. window.title("JailBreak Bob") #Defines the name of the title of the window.
  19. #Generates the initial canvas.
  20. canvas = Canvas(width = 960, height = 540, bg = "white")
  21. mainscreen() #Loops the window until another action is taken otherwise it would end straight away after it runs once.
  22.  
  23.  
  24. #Main Screen
  25. def mainscreen():
  26. photo = PhotoImage(file="main.gif") #Allows the image to be assigned to a variable which in this case is 'photo'.
  27. canvas.bind("<Button-1>", buttonclick_mainscreen) #Allows the left click of the mouse to be used on the canvas.
  28. canvas.pack(expand = YES, fill = BOTH)
  29. canvas.create_image(1, 1, image = photo, anchor = NW) #Places the image on to the canvas at the desired coordinates.
  30. canvas.mainloop()
  31.  
  32. #Help Screen
  33. #The function of this subroutine is the same as mainscreen() except to draw another image.
  34. def helpscreen():
  35. photo = PhotoImage(file="helpscreen.gif")
  36. canvas.bind("<Button-1>", buttonclick_helpscreen)
  37. canvas.pack(expand = YES, fill = BOTH)
  38. canvas.create_image(1, 1, image = photo, anchor = NW)
  39. canvas.mainloop()
  40.  
  41. #Game Screen
  42. #Where the game is played.
  43. def gamescreen():
  44. imagelist = ["1.gif","2.gif","3.gif","4.gif","5.gif","6.gif","7.gif","8.gif","9.gif","10.gif","11.gif","12.gif","13.gif","14.gif","15.gif","16.gif","17.gif","18.gif","19.gif","20.gif","21.gif","22.gif","23.gif","24.gif","25.gif","26.gif","27.gif","28.gif","29.gif","30.gif","31.gif","32.gif","33.gif","34.gif","35.gif","36.gif","37.gif","38.gif","39.gif","40.gif","41.gif","42.gif","43.gif","44.gif","45.gif","46.gif","47.gif","48.gif","49.gif","50.gif"]
  45. answerlistx = [-1, -3, 3, 4, -7, 8, 9, 10, -10, 2, -7, 7, -1, -5, -6, -9, -7, 10, -4, 8, 1, -8, -10, -1, -3, -7, -3, 7, 3, -4, 1, -8, -4, 9, -5, -10, 10, 2, 2, -10, 4, 9, -3, 6, 10, -6, 4, 9, -10, -10]
  46. answerlisty = [3, -6, -5, 3, -2, 4, -4, -3, 4, -6, 1, 2, 2, 3, 2, -1, -5, 1, -3, 1, -2, -2, -5, -3, -2, -6, -3, 6, 2, 0, -5, 6, -4, 4, 1, -6, 0, -6, 5, 2, 4, -4, -2, 0, -3, -6, -4, 1, -3, 1]
  47.  
  48. canvas.bind("<Button-1>", buttonclick_gamescreen)
  49. canvas.pack(expand = YES, fill = BOTH)
  50. photo = PhotoImage(file="gamescreen.gif")
  51. canvas.create_image(1, 1, image = photo, anchor = NW)
  52. e1 = Entry(canvas, width = 11)
  53. e2 = Entry(canvas, width = 11)
  54. canvas.create_window(390, 501, window=e1, anchor = NW)
  55. canvas.create_window(551, 501, window=e2, anchor = NW)
  56. canvas.after(1, countdowntimer)
  57. while cdtimer > 0:
  58. print("start of while loop")
  59. randomimage = random.randrange(0,49+1)
  60. game = PhotoImage(file=imagelist[randomimage])
  61. images = canvas.create_image(30, 65, image = game, anchor = NW)
  62. if pressed == 8 and e1 == answerlistx[randomimage] and e2 == answerlisty[randomimage]:
  63. print("correct")
  64. canvas.delete(images)
  65. randomimage = random.randrange(0,49+1)
  66. scorecounter = scorecounter + 1
  67. game = PhotoImage(file=imagelist[randomimage])
  68. images = canvas.create_image(30, 65, image = game, anchor = NW)
  69. e1.delete(0, END)
  70. e2.delete(0, END)
  71. elif pressed == 8 and e1 != answerlistx[randomimage] or pressed == 8 and e2 != answerlisty[randomimage]:
  72. print("incorrect")
  73. wronganswer = canvas.create_text(400, 200, text="Incorrect", font="Ubuntu 29 bold", fill='red', anchor=NW)
  74. e1.delete(0, END)
  75. e2.delete(0, END)
  76. canvas.after(1500,(canvas.delete(wronganswer)))
  77. canvas.mainloop()
  78.  
  79. def countdowntimer():
  80. global cdtimer
  81. cdtimer -= 1
  82. cdtext = canvas.create_text(510, 6, text=cdtimer, font="Ubuntu 29 bold", anchor = NW)
  83. if cdtimer == 0:
  84. lambda: canvas.after_cancel(_timer)
  85. cdtimer += 61
  86. canvas.delete(ALL)
  87. scorescreen()
  88. else:
  89. _timer = canvas.after(1000, lambda: (canvas.delete(cdtext), countdowntimer()))
  90.  
  91. #Score Screen
  92. #The function of this subroutine is the same as mainscreen() except to draw another image.
  93. def scorescreen():
  94. global scorecounter
  95. photo = PhotoImage(file="scorescreen.gif")
  96. canvas.create_image(1, 1, image = photo, anchor = NW)
  97. canvas.create_text(660,170, text=scorecounter, font="Ubuntu 36 bold", anchor = NW)
  98. canvas.bind("<Button-1>", buttonclick_scorescreen)
  99. scorecounter = scorecounter - scorecounter
  100. canvas.pack(expand = YES, fill = BOTH)
  101. canvas.mainloop()
  102.  
  103. #Button Click
  104. def buttonclick_mainscreen(event):
  105. pressed = ""
  106.  
  107. if event.x >18 and event.x <365 and event.y > 359 and event.y < 417 : pressed = 1 #Defines where the mouse can interact with the canvas.
  108. if event.x >18 and event.x <365 and event.y > 421 and event.y < 473 : pressed = 2 #Defines where the mouse can interact with the canvas.
  109. if event.x >18 and event.x <365 and event.y > 477 and event.y < 517 : pressed = 3 #Defines where the mouse can interact with the canvas.
  110.  
  111. #Commands the program what to do if it was to click on a certain area.
  112. if pressed == 1 :
  113. canvas.delete(ALL)
  114. gamescreen()
  115. if pressed == 2 :
  116. canvas.delete(ALL)
  117. helpscreen()
  118. if pressed == 3 :
  119. window.destroy()
  120.  
  121. def buttonclick_helpscreen(event):
  122. pressed = ""
  123.  
  124. if event.x >18 and event.x <365 and event.y > 359 and event.y < 417 : pressed = 4 #Defines where the mouse can interact with the canvas.
  125. if event.x >18 and event.x <365 and event.y > 421 and event.y < 473 : pressed = 5 #Defines where the mouse can interact with the canvas.
  126. if event.x >18 and event.x <365 and event.y > 477 and event.y < 517 : pressed = 6 #Defines where the mouse can interact with the canvas.
  127.  
  128. #Commands the program what to do if it was to click on a certain area.
  129. if pressed == 4 :
  130. canvas.delete(ALL)
  131. gamescreen()
  132. if pressed == 5 :
  133. canvas.delete(ALL)
  134. mainscreen()
  135. if pressed == 6 :
  136. window.destroy()
  137.  
  138. def buttonclick_gamescreen(event):
  139. pressed = ""
  140.  
  141. if event.x >853 and event.x <957 and event.y > 8 and event.y < 56 : pressed = 7 #Defines where the mouse can interact with the canvas.
  142. if event.x >666 and event.x <947 and event.y > 491 and event.y < 534 : pressed = 8 #Defines where the mouse can interact with the canvas.
  143.  
  144. #Commands the program what to do if it was to click on a certain area.
  145. if pressed == 7 :
  146. window.destroy()
  147. if pressed == 8 :
  148. print("next")
  149.  
  150.  
  151. def buttonclick_scorescreen(event):
  152. pressed = ""
  153.  
  154. if event.x >300 and event.x <645 and event.y > 361 and event.y < 418 : pressed = 9 #Defines where the mouse can interact with the canvas.
  155. if event.x >300 and event.x <645 and event.y > 423 and event.y < 474 : pressed = 10 #Defines where the mouse can interact with the canvas.
  156. if event.x >300 and event.x <645 and event.y > 478 and event.y < 522 : pressed = 11 #Defines where the mouse can interact with the canvas.
  157.  
  158. #Commands the program what to do if it was to click on a certain area.
  159. if pressed == 9 :
  160. canvas.delete(ALL)
  161. gamescreen()
  162. if pressed == 10 :
  163. canvas.delete(ALL)
  164. helpscreen()
  165. if pressed == 11 :
  166. window.destroy()
  167.  
  168. #Runs the main subroutine
  169. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement