Advertisement
Guest User

Minesweeper.py

a guest
Jun 19th, 2018
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.33 KB | None | 0 0
  1. import pygame, os, time, sys
  2. from pygame.locals import *
  3. from random import randint
  4. pygame.init()
  5.  
  6. YSIZE = int(input("Rows?  "))
  7. os.system("cls")
  8. XSIZE = int(input("Columns?  "))
  9. MINES = 0
  10. TILESIZE = 25
  11. while MINES == 0 or MINES >= YSIZE*XSIZE:
  12.     os.system("cls")
  13.     MINES = int(input("Mines?  "))
  14. os.system("cls")
  15.  
  16. screen = pygame.display.set_mode((XSIZE*TILESIZE,YSIZE*TILESIZE), pygame.NOFRAME)
  17. pygame.display.set_caption("Minesweeper")
  18.  
  19. class Tile(pygame.sprite.Sprite):
  20.     def __init__ (self,x,y): #sets all values
  21.         self.x = x
  22.         self.y = y
  23.         self.num = 0
  24.         self.state = 1
  25. ##        self.img = pygame.image.load("assets/u.png")
  26.         self.img = pygame.transform.scale(self.img, (TILESIZE, TILESIZE))
  27.         self.rect = pygame.Rect(TILESIZE*self.x, TILESIZE*self.y, TILESIZE, TILESIZE)
  28.     def check_num(self):
  29.         if self.num == 0:
  30.             n = 0
  31.             if not (self.x == 0 or self.y == 0):
  32.                 if grid[self.x-1][self.y-1].num == 9:
  33.                    n += 1
  34.             if not (self.x == 0):
  35.                 if grid[self.x-1][self.y].num == 9:
  36.                     n += 1
  37.             if not (self.y == 0):
  38.                 if grid[self.x][self.y-1].num == 9:
  39.                     n += 1
  40.             if not (self.x == XSIZE-1 or self.y == YSIZE-1):
  41.                 if grid[self.x+1][self.y+1].num == 9:
  42.                     n += 1
  43.             if not (self.y == YSIZE-1):
  44.                 if grid[self.x][self.y+1].num == 9:
  45.                     n += 1
  46.             if not (self.x == XSIZE-1 or self.y == 0):
  47.                 if grid[self.x+1][self.y-1].num == 9:
  48.                     n += 1
  49.             if not (self.x == 0 or self.y == YSIZE-1):
  50.                 if grid[self.x-1][self.y+1].num == 9:
  51.                     n += 1
  52.             if not (self.x == XSIZE-1):
  53.                 if grid[self.x+1][self.y].num == 9:
  54.                     n += 1
  55.             self.num = n
  56.         def check_mouse(self):
  57.             global reveal
  58.             if self.rect.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed() and self.state == 0:
  59.                 self.state == 1
  60.         def load_image(self, XSIZE, YSIZE):
  61. ##            if self.state == 1:
  62. ##                self.img = pygame.image.load("assets/" + str(self.num) + ".png")
  63. ##            else:
  64. ##                self.img = pygame.image.load("assets/u.png")
  65. ##            self.img = pygame.transform.scale(self.img, (TILESIZE, TILESIZE))
  66.             pass
  67.         def print_tile(self):
  68.             global screen
  69.             screen.blit(self.img, self.rect)
  70.  
  71. grid = []
  72. for i in range(XSIZE):
  73.     added = []
  74.     for j in range(YSIZE):
  75.         added.append(Tile(i,j))
  76.     grid.append(added)
  77.  
  78. def set_mines():
  79.     i = 0
  80.     while i < MINES:
  81.         x = randint(0, XSIZE-1)
  82.         y = randint(0, YSIZE-1)
  83.         if grid[x][y].num != 9:
  84.             grid[x][y].num = 9
  85.             i += 1
  86.  
  87. for i in range(XSIZE):
  88.     for j in range(YSIZE):
  89.         grid[i][j].check_num()
  90.  
  91. while True:
  92.     for i in pygame.event.get():
  93.         if i.type == QUIT:
  94.             sys.exit(0)
  95.     for i in range(XSIZE):
  96.         for j in range(YSIZE):
  97.             grid[i][j].check_mouse()
  98.     for i in range(XSIZE):
  99.         for j in range(YSIZE):
  100.             grid[i][j].load_image()
  101.     print_tile()
  102.     pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement