Advertisement
Guest User

JBB

a guest
Jul 22nd, 2013
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.95 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.    window = Tk()
  13.    cdtimer = 61
  14.    scorecounter = 0
  15.    _timer = None
  16. #Generates the initial canvas.
  17.    canvas = Canvas(width = 960, height = 540, bg = "white")
  18.    mainscreen() #Loops the window until another action is taken otherwise it would end straight away after it runs once.
  19.  
  20.    
  21. #Main Screen
  22. def mainscreen():
  23.     photo = PhotoImage(file="main.gif") #Allows the image to be assigned to a variable which in this case is 'photo'.
  24.     canvas.bind("<Button-1>", buttonclick_mainscreen) #Allows the left click of the mouse to be used on the canvas.
  25.     canvas.pack(expand = YES, fill = BOTH)
  26.     canvas.create_image(1, 1, image = photo, anchor = NW) #Places the image on to the canvas at the desired coordinates.
  27.     canvas.mainloop()
  28.    
  29. #Help Screen
  30. #The function of this subroutine is the same as mainscreen() except to draw another image.
  31. def helpscreen():
  32.     photo = PhotoImage(file="helpscreen.gif")
  33.     canvas.bind("<Button-1>", buttonclick_helpscreen)
  34.     canvas.pack(expand = YES, fill = BOTH)
  35.     canvas.create_image(1, 1, image = photo, anchor = NW)
  36.     canvas.mainloop()
  37.    
  38. #Game Screen
  39. #Where the game is played.
  40. def gamescreen():
  41.     global imagelist
  42.     global answerlistx
  43.     global answerlisty
  44.     global e1
  45.     global e2
  46.     global randomimage
  47.     global images
  48.     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"]
  49.     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']
  50.     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']
  51.  
  52.     canvas.bind("<Button-1>", buttonclick_gamescreen)
  53.     photo = PhotoImage(file="gamescreen.gif")
  54.     canvas.create_image(1, 1, image = photo, anchor = NW)
  55.     e1 = Entry(canvas, width = 11)
  56.     e2 = Entry(canvas, width = 11)
  57.     canvas.create_window(390, 501, window=e1, anchor = NW)
  58.     canvas.create_window(551, 501, window=e2, anchor = NW)
  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.     print("The random number is", randomimage)
  63.     canvas.pack(expand = YES, fill = BOTH)
  64.     canvas.after(1, countdowntimer)
  65.     canvas.mainloop()
  66.    
  67.        
  68. def countdowntimer():
  69.     global cdtimer
  70.     cdtimer -= 1
  71.     cdtext = canvas.create_text(510, 6, text=cdtimer, font="Ubuntu 29 bold", anchor = NW)
  72.     if cdtimer == 0:
  73.         lambda: canvas.after_cancel(_timer)
  74.         cdtimer += 61
  75.         canvas.delete(ALL)
  76.         scorescreen()
  77.     else:
  78.         _timer = canvas.after(1000, lambda: (canvas.delete(cdtext), countdowntimer()))
  79.          
  80. #Score Screen
  81. #The function of this subroutine is the same as mainscreen() except to draw another image.
  82. def scorescreen():
  83.     global scorecounter
  84.     photo = PhotoImage(file="scorescreen.gif")
  85.     canvas.create_image(1, 1, image = photo, anchor = NW)
  86.     canvas.create_text(660,170, text=scorecounter, font="Ubuntu 36 bold", anchor = NW)
  87.     canvas.bind("<Button-1>", buttonclick_scorescreen)
  88.     scorecounter = scorecounter - scorecounter
  89.     canvas.pack(expand = YES, fill = BOTH)
  90.     canvas.mainloop()
  91.    
  92. #Button Click
  93. def buttonclick_mainscreen(event):
  94.     pressed = ""
  95.  
  96.     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.
  97.     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.
  98.     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.
  99.  
  100. #Commands the program what to do if it was to click on a certain area.
  101.     if pressed == 1 :
  102.         canvas.delete(ALL)
  103.         gamescreen()
  104.     if pressed == 2 :
  105.         canvas.delete(ALL)
  106.         helpscreen()
  107.     if pressed == 3 :
  108.         window.destroy()
  109.        
  110. def buttonclick_helpscreen(event):
  111.     pressed = ""
  112.  
  113.     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.
  114.     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.
  115.     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.
  116.    
  117. #Commands the program what to do if it was to click on a certain area.
  118.     if pressed == 4 :
  119.         canvas.delete(ALL)
  120.         gamescreen()
  121.     if pressed == 5 :
  122.         canvas.delete(ALL)
  123.         mainscreen()
  124.     if pressed == 6 :
  125.         window.destroy()
  126.        
  127. def buttonclick_gamescreen(event):
  128.     global scorecounter
  129.     global pressed
  130.     global randomimage
  131.     global images
  132.     pressed = ""
  133.  
  134.     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.
  135.     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.
  136.  
  137. #Commands the program what to do if it was to click on a certain area.
  138.     if pressed == 7 :
  139.         window.destroy()
  140.     while pressed == 8 :
  141.         entryx = e1.get()
  142.         entryy = e2.get()
  143.         answerx = answerlistx[randomimage]
  144.         answery = answerlisty[randomimage]
  145.         print("The answer to X is", answerx, "You entered", entryx,"The answer to Y is", answery, "You entered ", entryy)
  146.         if entryx == answerx and entryy == answery:
  147.             print("correct")
  148.             canvas.delete(images)
  149.             randomimage = random.randrange(0,49+1)
  150.             scorecounter = scorecounter + 1
  151.             print("You score is now", scorecounter, "New random number is", randomimage)
  152.             game = PhotoImage(file=imagelist[randomimage])
  153.             images = canvas.create_image(30, 65, image = self.game, anchor = NW)
  154.             e1.delete(0, END)  
  155.             e2.delete(0, END)
  156.             pressed = ''
  157.         else:
  158.             print("incorrect")
  159.             e1.delete(0, END)  
  160.             e2.delete(0, END)
  161.             pressed = ''
  162.        
  163.  
  164. def buttonclick_scorescreen(event):
  165.     pressed = ""
  166.  
  167.     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.
  168.     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.
  169.     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.
  170.  
  171. #Commands the program what to do if it was to click on a certain area.
  172.     if pressed == 9 :
  173.         canvas.delete(ALL)
  174.         gamescreen()
  175.     if pressed == 10 :
  176.         canvas.delete(ALL)
  177.         helpscreen()
  178.     if pressed == 11 :
  179.         window.destroy()
  180.            
  181. #Runs the main subroutine  
  182. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement