SHARE
TWEET

Untitled

a guest Jul 19th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import time
  2. import random
  3. import kivy
  4. from kivy.app import App
  5. from kivy.uix.button import Label
  6. from kivy.uix.button import Button
  7.  
  8. from kivy.uix.gridlayout import GridLayout
  9. from kivy.core.window import Window
  10.  
  11. class Cell(Button):
  12.     def __init__(self, text, loc, board, **kwargs):
  13.         super(Cell, self).__init__(**kwargs)
  14.         self.size = (25,25)
  15.         self.board = board
  16.         self.loc = loc
  17.         self.text = text
  18.         self.val = 0
  19.         self.bind(on_touch_up = self.onPress)
  20.         self.visible = False
  21.         self.app = App.get_running_app()
  22.  
  23.     def onPress(self, instance, touch):
  24.         if self.collide_point(*touch.pos):
  25.             if self.visible == False:
  26.                 self.visible = True
  27.                 if touch.button == 'right':
  28.                     if self.text == '!':
  29.                         self.text = '?'
  30.                     else:
  31.                         self.text = '!'
  32.                 elif touch.button == 'left':
  33.                     self.text = self.val
  34.                     if self.val == 'X':
  35.                         None
  36.                     elif self.val == '0':
  37.                         self.revealNeighbors(self.loc[0],self.loc[1])
  38.                     else:
  39.                         None
  40.  
  41.     def revealNeighbors(self,y,x):
  42.         for i in range(-1,2):
  43.             for j in range(-1,2):
  44.                 if (0 <= (self.loc[0] + i) < self.board[0]) and (0 <= (self.loc[1] + j) < self.board[1]):
  45.                     print(self)
  46.                     if self.app.root.ids.myboard.cells[i][j].visible != True:
  47.                         self.app.ids.myboard.cells[i][j].visible = True
  48.                         self.app.ids.myboard.cells[i][j].text = self.app.ids.myboard.cells[i][j].val
  49.                         if self.app.ids.myboard.cells[i][j].val == '0':
  50.                             self.app.root.ids.myboard.cells[i][j].revealNeighbors(self.app.ids.myboard.cells[i][j],i,j)
  51.  
  52.  
  53.         None
  54.  
  55.  
  56. class GameBoard(GridLayout):
  57.     def __init__(self, **kwargs):
  58.         id: myboard
  59.         name: "Board"
  60.         super(GameBoard, self).__init__(**kwargs)
  61.  
  62.         self.row_force_default = True
  63.         self.row_default_height = 30
  64.         self.col_force_default = True
  65.         self.col_default_width = 30
  66.  
  67.         self.cols = 30
  68.         self.rows = 16
  69.         Window.size = (self.cols * self.col_default_width, self.rows * self.row_default_height)
  70.         self.cells = list()
  71.         mines = 0
  72.         minelst = list()
  73.  
  74.         self.score = 0
  75.  
  76.         for i in range(self.rows):
  77.             temp = list()
  78.             for j in range(self.cols):
  79.                 newCell = Cell(text = '0',loc = [i,j], board = [self.rows,self.cols])
  80.                 temp.append(newCell)
  81.                 self.add_widget(temp[-1])
  82.             self.cells.append(temp)
  83.  
  84.         for k in range(mines):
  85.             randX = random.randint(0,self.cols - 1)
  86.             randY = random.randint(0,self.rows - 1)
  87.             while self.cells[randY][randX].text == 'X':
  88.                 randX = random.randint(0,self.cols - 1)
  89.                 randY = random.randint(0,self.rows - 1)
  90.             self.cells[randY][randX].text = 'X'
  91.             temp = list()
  92.             temp.append(randY)
  93.             temp.append(randX)
  94.             minelst.append(temp)
  95.         minelst.sort()
  96.  
  97.         for mine in minelst:
  98.             if mine[0] == 0: # first row
  99.                 if mine[1] == 0: # top-left corner
  100.                     if self.cells[0][1].text != 'X':
  101.                         self.cells[0][1].text = str(int(self.cells[0][1].text) + 1)
  102.                     if self.cells[1][0].text != 'X':
  103.                         self.cells[1][0].text = str(int(self.cells[1][0].text) + 1)
  104.                     if self.cells[1][1].text != 'X':
  105.                         self.cells[1][1].text = str(int(self.cells[1][1].text) + 1)
  106.                 elif mine[1] == (self.cols -1): # top-right corner
  107.                     if self.cells[0][self.cols -2].text != 'X':
  108.                         self.cells[0][self.cols -2].text = str(int(self.cells[0][self.cols -2].text) + 1)
  109.                     if self.cells[1][self.cols -1].text != 'X':
  110.                         self.cells[1][self.cols -1].text = str(int(self.cells[1][self.cols -1].text) + 1)
  111.                     if self.cells[1][self.cols -2].text != 'X':
  112.                         self.cells[1][self.cols -2].text = str(int(self.cells[1][self.cols -2].text) + 1)
  113.                 else:
  114.                     if self.cells[0][mine[1]-1].text != 'X':
  115.                         self.cells[0][mine[1]-1].text = str(int(self.cells[0][mine[1]-1].text) + 1)
  116.                     if self.cells[0][mine[1]+1].text != 'X':
  117.                         self.cells[0][mine[1]+1].text = str(int(self.cells[0][mine[1]+1].text) + 1)
  118.                     for i in range(3):
  119.                         if self.cells[1][(mine[1]-1)+i].text != 'X':
  120.                             self.cells[1][(mine[1]-1)+i].text = str(int(self.cells[1][(mine[1]-1)+i].text) + 1)
  121.             elif mine[0] == (self.rows -1): # bottom row
  122.                 if mine[1] == 0: # bottom-left corner
  123.                     if self.cells[self.rows -1][1].text != 'X':
  124.                         self.cells[self.rows -1][1].text = str(int(self.cells[self.rows -1][1].text) + 1)
  125.                     if self.cells[self.rows -2][0].text != 'X':
  126.                         self.cells[self.rows -2][0].text = str(int(self.cells[self.rows -2][0].text) + 1)
  127.                     if self.cells[self.rows -2][1].text != 'X':
  128.                         self.cells[self.rows -2][1].text = str(int(self.cells[self.rows -2][1].text) + 1)
  129.                 elif mine[1] == (self.cols -1): # bottom-right corner
  130.                     if self.cells[self.rows -1][self.cols -2].text != 'X':
  131.                         self.cells[self.rows -1][self.cols -2].text = str(int(self.cells[self.rows -1][self.cols -2].text) + 1)
  132.                     if self.cells[self.rows -2][self.cols -1].text != 'X':
  133.                         self.cells[self.rows -2][self.cols -1].text = str(int(self.cells[self.rows -2][self.cols -1].text) + 1)
  134.                     if self.cells[self.rows -2][self.cols -2].text != 'X':
  135.                         self.cells[self.rows -2][self.cols -2].text = str(int(self.cells[self.rows -2][self.cols -2].text) + 1)
  136.                 else:
  137.                     if self.cells[self.rows -1][mine[1]-1].text != 'X':
  138.                         self.cells[self.rows -1][mine[1]-1].text = str(int(self.cells[self.rows -1][mine[1]-1].text) + 1)
  139.                     if self.cells[self.rows -1][mine[1]+1].text != 'X':
  140.                         self.cells[self.rows -1][mine[1]+1].text = str(int(self.cells[self.rows -1][mine[1]+1].text) + 1)
  141.                     for i in range(3):
  142.                         if self.cells[self.rows -2][(mine[1]-1)+i].text != 'X':
  143.                             self.cells[self.rows -2][(mine[1]-1)+i].text = str(int(self.cells[self.rows -2][(mine[1]-1)+i].text) + 1)
  144.             elif mine[1] == 0: # left column w/o corners
  145.                 if self.cells[mine[0]-1][0].text != 'X':
  146.                     self.cells[mine[0]-1][0].text = str(int(self.cells[mine[0]-1][0].text) + 1)
  147.                 if self.cells[mine[0]+1][0].text != 'X':
  148.                     self.cells[mine[0]+1][0].text = str(int(self.cells[mine[0]+1][0].text) + 1)
  149.                 for i in range(3):
  150.                     if self.cells[(mine[0]-1)+i][1].text != 'X':
  151.                         self.cells[(mine[0]-1)+i][1].text = str(int(self.cells[(mine[0]-1)+i][1].text)+1)
  152.             elif mine[1] == (self.cols -1): # right column w/o corners
  153.                 if self.cells[mine[0]-1][self.cols -1].text != 'X':
  154.                     self.cells[mine[0]-1][self.cols -1].text = str(int(self.cells[mine[0]-1][self.cols -1].text) + 1)
  155.                 if self.cells[mine[0]+1][self.cols -1].text != 'X':
  156.                     self.cells[mine[0]+1][self.cols -1].text = str(int(self.cells[mine[0]+1][self.cols -1].text) + 1)
  157.                 for i in range(3):
  158.                     if self.cells[(mine[0]-1)+i][self.cols -2].text != 'X':
  159.                         self.cells[(mine[0]-1)+i][self.cols -2].text = str(int(self.cells[(mine[0]-1)+i][self.cols -2].text)+1)
  160.             else: # interior mines
  161.                 for i in range(3):
  162.                     if self.cells[mine[0]-1][(mine[1]-1)+i].text != 'X':
  163.                         self.cells[mine[0]-1][(mine[1]-1)+i].text = str(int(self.cells[mine[0]-1][(mine[1]-1)+i].text)+1)
  164.                     if self.cells[mine[0]+1][(mine[1]-1)+i].text != 'X':
  165.                         self.cells[mine[0]+1][(mine[1]-1)+i].text = str(int(self.cells[mine[0]+1][(mine[1]-1)+i].text)+1)
  166.                 if self.cells[mine[0]][mine[1]-1].text != 'X':
  167.                     self.cells[mine[0]][mine[1]-1].text = str(int(self.cells[mine[0]][mine[1]-1].text)+1)
  168.                 if self.cells[mine[0]][mine[1]+1].text != 'X':
  169.                     self.cells[mine[0]][mine[1]+1].text = str(int(self.cells[mine[0]][mine[1]+1].text)+1)
  170.  
  171.         for i in range(self.rows):
  172.             for j in range(self.cols):
  173.                 self.cells[i][j].val = self.cells[i][j].text
  174.                 self.cells[i][j].text = '?'
  175.  
  176. class MineSweeper(App):
  177.     def build(self):
  178.         self.root = GameBoard()
  179.         return self.root
  180.  
  181. mineSweeper = MineSweeper()
  182.  
  183. mineSweeper.run()
  184.      
  185. Traceback (most recent call last):
  186.    File "kivy/properties.pyx", line 860, in kivy.properties.ObservableDict.__getattr__
  187.  KeyError: 'myboard'
  188.  
  189.  During handling of the above exception, another exception occurred:
  190.  
  191.  Traceback (most recent call last):
  192.    File "MineSweeper.py", line 183, in <module>
  193.      mineSweeper.run()
  194.    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kivy/app.py", line 855, in run
  195.      runTouchApp()
  196.    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kivy/base.py", line 504, in runTouchApp
  197.      EventLoop.window.mainloop()
  198.    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kivy/core/window/window_sdl2.py", line 747, in mainloop
  199.      self._mainloop()
  200.    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kivy/core/window/window_sdl2.py", line 479, in _mainloop
  201.      EventLoop.idle()
  202.    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kivy/base.py", line 342, in idle
  203.      self.dispatch_input()
  204.    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kivy/base.py", line 327, in dispatch_input
  205.      post_dispatch_input(*pop(0))
  206.    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kivy/base.py", line 233, in post_dispatch_input
  207.      listener.dispatch('on_motion', etype, me)
  208.    File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
  209.    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kivy/core/window/__init__.py", line 1406, in on_motion
  210.      self.dispatch('on_touch_up', me)
  211.    File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
  212.    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kivy/core/window/__init__.py", line 1442, in on_touch_up
  213.      if w.dispatch('on_touch_up', touch):
  214.    File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch
  215.    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kivy/uix/widget.py", line 571, in on_touch_up
  216.      if child.dispatch('on_touch_up', touch):
  217.    File "kivy/_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch
  218.    File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch
  219.    File "kivy/_event.pyx", line 1138, in kivy._event.EventObservers._dispatch
  220.    File "MineSweeper.py", line 37, in onPress
  221.      self.revealNeighbors(self.loc[0],self.loc[1])
  222.    File "MineSweeper.py", line 46, in revealNeighbors
  223.      if self.app.root.ids.myboard.cells[i][j].visible != True:
  224.    File "kivy/properties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__
  225.  AttributeError: 'super' object has no attribute '__getattr__'
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top