Advertisement
Fhernd

buscaminas.py

Nov 18th, 2018
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.53 KB | None | 0 0
  1. from __future__ import print_function
  2. import pygame
  3. from pygame.locals import *
  4. import random
  5.  
  6. VACIA = 0
  7. MINA = 9
  8. UNA = 1
  9. DOS = 2
  10. TRES = 3
  11. CUATRO = 4
  12. CINCO = 5
  13.  
  14. class Celda:
  15.     def __init__(self, tipo=VACIA):
  16.         self.tipo = tipo
  17.         self.x = 0
  18.         self.y = 0
  19.         self.destapada = False
  20.  
  21. class Tablero:
  22.     def __init__(self):
  23.         self.n = 16
  24.         self.m = 16
  25.         self.matriz = []
  26.         self.numero_minas = 40
  27.         self.celdas_destapadas = 0
  28.  
  29.         self.inicializar_tablero()
  30.         self.colocar_minas()
  31.         self.computar_minas_vecinas()
  32.  
  33.     def inicializar_tablero(self):
  34.         for i in range(self.n):
  35.             fila = []
  36.             for j in range(self.m):
  37.                 fila.append(Celda())
  38.             self.matriz.append(fila)
  39.  
  40.     def colocar_minas(self):
  41.         contador = 0
  42.  
  43.         while contador < self.numero_minas:
  44.             i = random.randint(0, self.n - 1)
  45.             j = random.randint(0, self.m - 1)
  46.  
  47.             if self.matriz[i][j].tipo == VACIA:
  48.                 self.matriz[i][j].tipo = MINA
  49.                 contador += 1
  50.  
  51.     def computar_minas_vecinas(self):
  52.         for i in range(self.n):
  53.             for j in range(self.m):
  54.                 if self.matriz[i][j].tipo == VACIA:
  55.                     tipo_celda_calculado = self.calcular_tipo(i, j)
  56.  
  57.                     if tipo_celda_calculado >= 3 and (i == 0 or i == self.n - 1 or j == 0 or j == self.m - 1):
  58.                         self.matriz[i][j].tipo = DOS
  59.                     else:
  60.                         self.matriz[i][j].tipo = tipo_celda_calculado
  61.  
  62.     def calcular_tipo(self, i, j):
  63.         contador_minas = 0
  64.  
  65.         if i - 1 >= 0:
  66.             if self.matriz[i - 1][j].tipo == MINA:
  67.                 contador_minas += 1
  68.  
  69.         if i - 1 >= 0 and j + 1 < self.m:
  70.             if self.matriz[i - 1][j + 1].tipo == MINA:
  71.                 contador_minas += 1
  72.  
  73.         if j + 1 < self.m:
  74.             if self.matriz[i][j + 1].tipo == MINA:
  75.                 contador_minas += 1
  76.  
  77.         if i + 1 < self.n and j + 1 < self.m:
  78.             if self.matriz[i + 1][j + 1].tipo == MINA:
  79.                 contador_minas += 1
  80.  
  81.         if i + 1 < self.n:
  82.             if self.matriz[i + 1][j].tipo == MINA:
  83.                 contador_minas += 1
  84.  
  85.         if i + 1 < self.n and j - 1 >= 0:
  86.             if self.matriz[i + 1][j - 1].tipo == MINA:
  87.                 contador_minas += 1
  88.  
  89.         if j - 1 >= 0:
  90.             if self.matriz[i][j - 1].tipo == MINA:
  91.                 contador_minas += 1
  92.  
  93.         if i - 1 >= 0 and j - 1 >= 0:
  94.             if self.matriz[i - 1][j - 1].tipo == MINA:
  95.                 contador_minas += 1
  96.  
  97.         return contador_minas
  98.  
  99.     def imprimir_tablero(self):
  100.         for i in range(self.n):
  101.             for j in range(self.m):
  102.                 print(self.matriz[i][j].tipo, ' ', end='')
  103.             print()
  104.  
  105. class App:
  106.     def __init__(self):
  107.         self._running = True
  108.         self._display_surf = None
  109.         self.size = self.weight, self.height = 640, 640
  110.         self.clock = pygame.time.Clock()
  111.         self.tablero = Tablero()
  112.         print(self.tablero.imprimir_tablero())
  113.  
  114.     def on_init(self):
  115.         pygame.init()
  116.         self._display_surf = pygame.display.set_mode(self.size)
  117.         self._display_surf.fill((255, 255, 255))
  118.         self._running = True
  119.  
  120.     def on_event(self, event):
  121.         if event.type == pygame.QUIT:
  122.             self._running = False
  123.  
  124.     def on_loop(self):
  125.         pass
  126.  
  127.     def text_objects(self, text, font):
  128.         textSurface = font.render(text, True, (0, 0, 0))
  129.         return textSurface, textSurface.get_rect()
  130.  
  131.     def on_render(self):
  132.         x = 0
  133.         y = 0
  134.  
  135.         for i in range(self.tablero.n):
  136.             x = 0
  137.             for j in range(self.tablero.m):
  138.                 self.tablero.matriz[i][j].x = x
  139.                 self.tablero.matriz[i][j].y = y
  140.  
  141.                 if self.tablero.matriz[i][j].destapada:
  142.                     smallText = pygame.font.SysFont("comicsansms", 20)
  143.                     textSurf, textRect = self.text_objects(str(self.tablero.matriz[i][j].tipo), smallText)
  144.                     textRect.center = ((x + (40 / 2)), (y + (40 / 2)))
  145.                     self._display_surf.blit(textSurf, textRect)
  146.                 else:
  147.                     pygame.draw.rect(self._display_surf, (140, 240, 130), (x, y, 40, 40), 3)
  148.                 x += 40
  149.             y += 40
  150.  
  151.  
  152.     def on_cleanup(self):
  153.         pygame.quit()
  154.  
  155.     def destapar_celdas(self, i, j):
  156.         for k in range(8):
  157.             if i - 1 >= 0:
  158.                 if self.tablero.matriz[i - 1][j].tipo != VACIA and self.tablero.matriz[i - 1][j].tipo != MINA:
  159.                     self.tablero.matriz[i - 1][j].destapada = True
  160.                 elif self.tablero.matriz[i - 1][j].tipo == VACIA:
  161.                     self.destapar_celdas(i - 1, j)
  162.            
  163.             if i - 1 >= 0 and j + 1 < self.tablero.m:
  164.                 if self.tablero.matriz[i - 1][j + 1].tipo != VACIA and self.tablero.matriz[i - 1][j + 1].tipo != MINA:
  165.                     self.tablero.matriz[i - 1][j + 1].destapada = True
  166.                 elif self.tablero.matriz[i - 1][j + 1].tipo == VACIA:
  167.                     self.destapar_celdas(i - 1, j + 1)
  168.            
  169.             if j + 1 < self.tablero.m:
  170.                 if self.tablero.matriz[i][j + 1].tipo != VACIA and self.tablero.matriz[i][j + 1].tipo != MINA:
  171.                     self.tablero.matriz[i][j + 1].destapada = True
  172.                 elif self.tablero.matriz[i][j + 1].tipo == VACIA:
  173.                     self.destapar_celdas(i, j + 1)
  174.                    
  175.                
  176.  
  177.     def on_execute(self):
  178.         if self.on_init() == False:
  179.             self._running = False
  180.  
  181.         while (self._running):
  182.             event = pygame.event.poll()
  183.  
  184.             for event in pygame.event.get():
  185.                 self.on_event(event)
  186.  
  187.             if event.type == pygame.QUIT:
  188.                 pygame.quit()
  189.                 quit()
  190.  
  191.             if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
  192.                 print("You pressed the left mouse button at {}".format(event.pos))
  193.                 posicion = event.pos
  194.                 y = posicion[0] / 40
  195.                 x = posicion[1] / 40
  196.                 self.tablero.matriz[x][y].destapada = True
  197.             self.on_loop()
  198.             self.on_render()
  199.             pygame.display.flip()
  200.             self.clock.tick(60)
  201.  
  202.         self.on_cleanup()
  203.  
  204.  
  205. if __name__ == "__main__":
  206.     theApp = App()
  207.     theApp.on_execute()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement