Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, os, time, sys
- from pygame.locals import *
- from random import randint
- pygame.init()
- YSIZE = int(input("Rows? "))
- os.system("cls")
- XSIZE = int(input("Columns? "))
- MINES = 0
- TILESIZE = 25
- while MINES == 0 or MINES >= YSIZE*XSIZE:
- os.system("cls")
- MINES = int(input("Mines? "))
- os.system("cls")
- screen = pygame.display.set_mode((XSIZE*TILESIZE,YSIZE*TILESIZE), pygame.NOFRAME)
- pygame.display.set_caption("Minesweeper")
- class Tile(pygame.sprite.Sprite):
- def __init__ (self,x,y): #sets all values
- self.x = x
- self.y = y
- self.num = 0
- self.state = 1
- ## self.img = pygame.image.load("assets/u.png")
- self.img = pygame.transform.scale(self.img, (TILESIZE, TILESIZE))
- self.rect = pygame.Rect(TILESIZE*self.x, TILESIZE*self.y, TILESIZE, TILESIZE)
- def check_num(self):
- if self.num == 0:
- n = 0
- if not (self.x == 0 or self.y == 0):
- if grid[self.x-1][self.y-1].num == 9:
- n += 1
- if not (self.x == 0):
- if grid[self.x-1][self.y].num == 9:
- n += 1
- if not (self.y == 0):
- if grid[self.x][self.y-1].num == 9:
- n += 1
- if not (self.x == XSIZE-1 or self.y == YSIZE-1):
- if grid[self.x+1][self.y+1].num == 9:
- n += 1
- if not (self.y == YSIZE-1):
- if grid[self.x][self.y+1].num == 9:
- n += 1
- if not (self.x == XSIZE-1 or self.y == 0):
- if grid[self.x+1][self.y-1].num == 9:
- n += 1
- if not (self.x == 0 or self.y == YSIZE-1):
- if grid[self.x-1][self.y+1].num == 9:
- n += 1
- if not (self.x == XSIZE-1):
- if grid[self.x+1][self.y].num == 9:
- n += 1
- self.num = n
- def check_mouse(self):
- global reveal
- if self.rect.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed() and self.state == 0:
- self.state == 1
- def load_image(self, XSIZE, YSIZE):
- ## if self.state == 1:
- ## self.img = pygame.image.load("assets/" + str(self.num) + ".png")
- ## else:
- ## self.img = pygame.image.load("assets/u.png")
- ## self.img = pygame.transform.scale(self.img, (TILESIZE, TILESIZE))
- pass
- def print_tile(self):
- global screen
- screen.blit(self.img, self.rect)
- grid = []
- for i in range(XSIZE):
- added = []
- for j in range(YSIZE):
- added.append(Tile(i,j))
- grid.append(added)
- def set_mines():
- i = 0
- while i < MINES:
- x = randint(0, XSIZE-1)
- y = randint(0, YSIZE-1)
- if grid[x][y].num != 9:
- grid[x][y].num = 9
- i += 1
- for i in range(XSIZE):
- for j in range(YSIZE):
- grid[i][j].check_num()
- while True:
- for i in pygame.event.get():
- if i.type == QUIT:
- sys.exit(0)
- for i in range(XSIZE):
- for j in range(YSIZE):
- grid[i][j].check_mouse()
- for i in range(XSIZE):
- for j in range(YSIZE):
- grid[i][j].load_image()
- print_tile()
- pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement