Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.36 KB | None | 0 0
  1. class Tile(object):
  2. def __init__(self, canvas, x, y, image, cardback):
  3. self.image = image
  4. more stuff...
  5.  
  6.  
  7. self.images = [
  8. photoDog,
  9. more images...
  10. ]
  11.  
  12. selected = []
  13. for i in range(10):
  14. randomInd = randint(0, len(self.images) - 1)
  15. animalImg = self.images[randomInd]
  16. selected.append(animalImg)
  17. selected.append(animalImg)
  18. del self.images[randomInd]
  19. shuffle(selected)
  20. self.flippedTiles = []
  21. NUM_COLS = 5
  22. NUM_ROWS = 4
  23.  
  24. for x in range(0, NUM_COLS):
  25. for y in range(0, NUM_ROWS):
  26. self.tiles.append(Tile(self.canvas, x * 108 + 10, y * 108 + 40, selected.pop(), photoCardback))
  27.  
  28. import tkinter as tk
  29. from random import randint
  30. from random import shuffle
  31. import pygame
  32. pygame.init()
  33.  
  34. class Controller(tk.Tk):
  35. def __init__(self, *args, **kwargs):
  36. tk.Tk.__init__(self, *args, **kwargs)
  37.  
  38. # the container is where we'll stack a bunch of frames
  39. # on top of each other, then the one we want visible
  40. # will be raised above the others
  41. container = tk.Frame(self)
  42. container.pack(side="top", fill="both", expand=True)
  43. container.grid_rowconfigure(0, weight=1)
  44. container.grid_columnconfigure(0, weight=1)
  45.  
  46. if True:
  47. self.frames = {}
  48. for F in (PageMG,):
  49. page_name = F.__name__
  50. frame = F(parent=container, controller=self)
  51. self.frames[page_name] = frame
  52.  
  53. # put all of the pages in the same location;
  54. # the one on the top of the stacking order
  55. # will be the one that is visible.
  56. frame.grid(row=0, column=0, sticky="nsew")
  57.  
  58. self.show_frame("PageMG")
  59. self.geometry("800x480")
  60.  
  61. def show_frame(self, page_name):
  62. '''Show a frame for the given page name'''
  63. frame = self.frames[page_name]
  64. frame.tkraise()
  65.  
  66.  
  67. class PageMG(tk.Frame):
  68. def __init__(self, parent, controller):
  69. tk.Frame.__init__(self, parent)
  70. x = MemGame(self)
  71. x.pack()
  72.  
  73.  
  74. class Tile(object):
  75. def __init__(self, canvas, x, y, image, cardback):
  76. self.cardback = cardback
  77. self.canvas = canvas
  78. self.y = y
  79. self.x = x
  80. self.image = image
  81.  
  82. def drawFaceDown(self):
  83. self.canvas.create_rectangle(self.x, self.y, self.x + 100, self.y + 100, fill = "white")
  84. self.canvas.create_image(self.x + 50, self.y + 50, image=self.cardback)
  85. self.isFaceUp = False
  86.  
  87. def drawFaceUp(self):
  88. self.canvas.create_rectangle(self.x, self.y, self.x + 100, self.y + 100, fill = "white")
  89. self.canvas.create_image(self.x + 50, self.y + 50, image=self.image)
  90. self.isFaceUp = True
  91.  
  92. def isUnderMouse(self, event):
  93. if(event.x > self.x and event.x < self.x + 100):
  94. if(event.y > self.y and event.y < self.y + 100):
  95. return True
  96.  
  97.  
  98. class MemGame(tk.Frame):
  99. def __init__(self, master):
  100. super(MemGame, self).__init__(master)
  101.  
  102. photoRestart = tk.PhotoImage(file="restart.png")
  103. imgRestart = tk.Label(self, anchor="s", image=photoRestart)
  104. imgRestart.image = photoRestart
  105. buttonRestart = tk.Button(self, activebackground="white",
  106. image=photoRestart, highlightthickness=0, borderwidth=0,
  107. command=lambda: self.restart())
  108. buttonRestart.place(x=560, y=200)
  109.  
  110. photoDog = tk.PhotoImage(file="Dyr/dog.png")
  111. photoElefant = tk.PhotoImage(file="Dyr/elefant.png")
  112. photoFlamingo = tk.PhotoImage(file="Dyr/flamingo.png")
  113. photoFlodhest = tk.PhotoImage(file="Dyr/flodhest.png")
  114. photoKamel = tk.PhotoImage(file="Dyr/kamel.png")
  115. photoKatt = tk.PhotoImage(file="Dyr/katt.png")
  116. photoKroko = tk.PhotoImage(file="Dyr/krokodille.png")
  117. photoNeshorn = tk.PhotoImage(file="Dyr/neshorn.png")
  118. photoSkilpadde = tk.PhotoImage(file="Dyr/skilpadde.png")
  119. photoStruts = tk.PhotoImage(file="Dyr/struts.png")
  120. photoZebra = tk.PhotoImage(file="Dyr/zebra.png")
  121. photoLove = tk.PhotoImage(file="Dyr/love.png")
  122.  
  123. photoCardback = tk.PhotoImage(file="cardback.png")
  124. self.cardback = photoCardback
  125.  
  126. self.riktig_sound = pygame.mixer.Sound("riktig.wav")
  127. self.click_sound = pygame.mixer.Sound("camerashutter.wav")
  128.  
  129. self.configure(width=650, height=480, bg="white")
  130. self.canvas = tk.Canvas(self, bg="white", width=550, height=480, highlightthickness=0, borderwidth=0)
  131. self.canvas.place(x=0, y=-30)
  132. self.tiles = []
  133. self.images = [
  134. photoDog,
  135. photoElefant,
  136. photoFlamingo,
  137. photoFlodhest,
  138. photoKamel,
  139. photoKatt,
  140. photoKroko,
  141. photoNeshorn,
  142. photoSkilpadde,
  143. photoStruts,
  144. photoZebra,
  145. photoLove
  146. ]
  147.  
  148. self.texts = [
  149. "Dog",
  150. "Elephant",
  151. "Flamingo",
  152. "Hippopotamus",
  153. "Camel",
  154. "Cat",
  155. "Crocodile",
  156. "Rhinoceros",
  157. "Turtle",
  158. "Ostrich",
  159. "Zebra",
  160. "Lion"
  161. ]
  162.  
  163. selected = []
  164. for i in range(10):
  165. randomInd = randint(0, len(self.images) - 1)
  166. randomInd2 = randint(0, len(self.texts) - 1)
  167. animalImg = self.images[randomInd]
  168. animalText = self.texts[randomInd]
  169. selected.append(animalImg)
  170. selected.append(animalText)
  171. del self.images[randomInd]
  172. del self.texts[randomInd2]
  173. shuffle(selected)
  174. self.flippedTiles = []
  175. NUM_COLS = 5
  176. NUM_ROWS = 4
  177.  
  178. for x in range(0, NUM_COLS):
  179. for y in range(0, NUM_ROWS):
  180. self.tiles.append(Tile(self.canvas, x * 108 + 10, y * 108 + 40, selected.pop(), photoCardback))
  181.  
  182. for i in range(len(self.tiles)):
  183. self.tiles[i].drawFaceDown()
  184. self.flippedThisTurn = 0
  185. self.canvas.bind("<Button-1>", self.mouseClicked)
  186.  
  187. def mouseClicked(self, event):
  188. for tile in self.tiles:
  189. if tile.isUnderMouse(event) and (self.flippedThisTurn < 2):
  190. if (not(tile.isFaceUp)):
  191. self.clickSound()
  192. tile.drawFaceUp()
  193. self.flippedTiles.append(tile)
  194. self.flippedThisTurn += 1
  195.  
  196. if (self.flippedThisTurn == 2):
  197. if (self.flippedTiles[-1].image == self.flippedTiles[-2].image): #check last two elements
  198. self.riktig()
  199. self.after(1000, self.checkTiles)
  200.  
  201. def checkTiles(self):
  202. self.flippedThisTurn = 0
  203.  
  204. if not(self.flippedTiles[-1].image == self.flippedTiles[-2].image):
  205. self.flippedTiles[-1].drawFaceDown()
  206. self.flippedTiles[-2].drawFaceDown()
  207. del self.flippedTiles[-2:]
  208.  
  209. def restart(self):
  210. for i in range(len(self.tiles)):
  211. self.tiles[i].drawFaceDown()
  212.  
  213. def riktig(self):
  214. pygame.mixer.Sound.play(self.riktig_sound)
  215. pygame.mixer.music.stop()
  216.  
  217. def clickSound(self):
  218. pygame.mixer.Sound.play(self.click_sound)
  219. pygame.mixer.music.stop()
  220.  
  221.  
  222. if __name__ == '__main__':
  223. c = Controller()
  224. c.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement