Preface
Today I'll share with you a minesweeping game , I don't say much nonsense , Let's start happily ~
Effect display
development tool
Python edition : 3.6.4
Related modules :
pygame modular ;
As well as some python Built in modules .
Environment building
install Python And add to environment variable ,pip Install the relevant modules required .
Introduction of the principle
Here I briefly introduce the implementation idea of the game .
I believe everyone is interested in mine clearance windows Its own classic games are not strange , The rules of the game are simple :
The number in the upper left corner of the game interface represents the number of Mines buried in all squares , In the upper right corner is a timer . All you have to do is find all the thunder in the square according to the hint .
So what's the hint ? At the beginning of the game, you need to click any square , Just like this. :
The number above represents the number of Mines buried in the Jiugong grid centered on this number . So if you have a bad character , If you point to ray at the beginning , The game is over .
ok, After understanding the rules of the game , We can start writing code . First, initialize the game :
# Game initialization
pygame.init()
screen = pygame.display.set_mode(cfg.SCREENSIZE)
pygame.display.set_caption('mine sweeper —— ilove-python')
Then put the font you need , picture , Bring in music and everything :
# Import all pictures
images = {}
for key, value in cfg.IMAGE_PATHS.items():
if key in ['face_fail', 'face_normal', 'face_success']:
image = pygame.image.load(value)
images[key] = pygame.transform.smoothscale(image, (int(cfg.GRIDSIZE*1.25), int(cfg.GRIDSIZE*1.25)))
else:
image = pygame.image.load(value).convert()
images[key] = pygame.transform.smoothscale(image, (cfg.GRIDSIZE, cfg.GRIDSIZE))
# Load font
font = pygame.font.Font(cfg.FONT_PATH, cfg.FONT_SIZE)
# Import and play background music
pygame.mixer.music.load(cfg.BGM_PATH)
pygame.mixer.music.play(-1)
next , Let's define a WordPad , It is used to display the number of buried mines in the upper left corner and the game time in the upper right corner :
''' Text board '''
class TextBoard(pygame.sprite.Sprite):
def __init__(self, text, font, position, color, **kwargs):
pygame.sprite.Sprite.__init__(self)
self.text = text
self.font = font
self.position = position
self.color = color
def draw(self, screen):
text_render = self.font.render(self.text, True, self.color)
screen.blit(text_render, self.position)
def update(self, text):
self.text = text
It's very simple , Just bind the text object rendered with font to the screen , Then set up a update function , To update the text content in real time .
then , Let's define another expression button class :
''' Emoticon button '''
class EmojiButton(pygame.sprite.Sprite):
def __init__(self, images, position, status_code=0, **kwargs):
pygame.sprite.Sprite.__init__(self)
# Import image
self.images = images
self.image = self.images['face_normal']
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = position
# The current state of the emoticon button
self.status_code = status_code
''' Draw it on the screen '''
def draw(self, screen):
# Status code for 0, Represents a normal expression
if self.status_code == 0:
self.image = self.images['face_normal']
# Status code for 1, The expression of failure
elif self.status_code == 1:
self.image = self.images['face_fail']
# Status code for 2, The expression of success
elif self.status_code == 2:
self.image = self.images['face_success']
# Bind picture to screen
screen.blit(self.image, self.rect)
''' Set the status of the current button '''
def setstatus(self, status_code):
self.status_code = status_code
When the mouse clicks on this button , Start a new game ( Regardless of the current game state , Will start a new game ):
Next , What we need to define is the following grid class :
''' Thunder '''
class Mine(pygame.sprite.Sprite):
def __init__(self, images, position, status_code=0, **kwargs):
pygame.sprite.Sprite.__init__(self)
# Import image
self.images = images
self.image = self.images['blank']
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = position
# Ray's current state
self.status_code = status_code
# Real thunder or fake thunder ( The default is false thunder )
self.is_mine_flag = False
# The number of surrounding mines
self.num_mines_around = -1
''' Set the current status code '''
def setstatus(self, status_code):
self.status_code = status_code
''' Buried mine '''
def burymine(self):
self.is_mine_flag = True
''' Set the number of surrounding mines '''
def setnumminesaround(self, num_mines_around):
self.num_mines_around = num_mines_around
''' Draw it on the screen '''
def draw(self, screen):
# Status code for 0, It means that the mine has not been clicked
if self.status_code == 0:
self.image = self.images['blank']
# Status code for 1, It means that the mine has been opened
elif self.status_code == 1:
self.image = self.images['mine'] if self.is_mine_flag else self.images[str(self.num_mines_around)]
# Status code for 2, Indicates that the mine is marked as mine by the player
elif self.status_code == 2:
self.image = self.images['flag']
# Status code for 3, It means that the mine is marked as a question mark by the player
elif self.status_code == 3:
self.image = self.images['ask']
# Status code for 4, It means that the mine is being double clicked by the left and right mouse buttons
elif self.status_code == 4:
assert not self.is_mine_flag
self.image = self.images[str(self.num_mines_around)]
# Status code for 5, Represents that the mine is around the mine double clicked by the left and right mouse buttons
elif self.status_code == 5:
self.image = self.images['0']
# Status code for 6, It means that the mine was trampled
elif self.status_code == 6:
assert self.is_mine_flag
self.image = self.images['blood']
# Status code for 7, It means that the mine was wrongly marked
elif self.status_code == 7:
assert not self.is_mine_flag
self.image = self.images['error']
# Bind picture to screen
screen.blit(self.image, self.rect)
@property
def opened(self):
return self.status_code == 1
Its main function is to record the state of a square in the game map ( For example, did you bury thunder , Has it been turned on , Is it marked or not ).
Finally, define a game map class , To integrate all squares in the game map, so as to call and update in the main game loop :
''' Minesweeping map '''
class MinesweeperMap():
def __init__(self, cfg, images, **kwargs):
self.cfg = cfg
# Ray matrix
self.mines_matrix = []
for j in range(cfg.GAME_MATRIX_SIZE[1]):
mines_line = []
for i in range(cfg.GAME_MATRIX_SIZE[0]):
position = i * cfg.GRIDSIZE + cfg.BORDERSIZE, (j + 2) * cfg.GRIDSIZE
mines_line.append(Mine(images=images, position=position))
self.mines_matrix.append(mines_line)
# Random mine laying
for i in random.sample(range(cfg.GAME_MATRIX_SIZE[0]*cfg.GAME_MATRIX_SIZE[1]), cfg.NUM_MINES):
self.mines_matrix[i//cfg.GAME_MATRIX_SIZE[0]][i%cfg.GAME_MATRIX_SIZE[0]].burymine()
count = 0
for item in self.mines_matrix:
for i in item:
count += int(i.is_mine_flag)
# The current state of the game
self.status_code = -1
# Record the position when the mouse is pressed and the key pressed
self.mouse_pos = None
self.mouse_pressed = None
''' Draw the current game state diagram '''
def draw(self, screen):
for row in self.mines_matrix:
for item in row: item.draw(screen)
''' Set the current game state '''
def setstatus(self, status_code):
# 0: There's a game going on , 1: Game over , -1: The game hasn't started yet
self.status_code = status_code
''' Update the current game status map according to the player's mouse operation '''
def update(self, mouse_pressed=None, mouse_pos=None, type_='down'):
assert type_ in ['down', 'up']
# Record the position when the mouse is pressed and the key pressed
if type_ == 'down' and mouse_pos is not None and mouse_pressed is not None:
self.mouse_pos = mouse_pos
self.mouse_pressed = mouse_pressed
# The range of mouse click is not in the game map , No response
if self.mouse_pos[0] < self.cfg.BORDERSIZE or self.mouse_pos[0] > self.cfg.SCREENSIZE[0] - self.cfg.BORDERSIZE or \
self.mouse_pos[1] < self.cfg.GRIDSIZE * 2 or self.mouse_pos[1] > self.cfg.SCREENSIZE[1] - self.cfg.BORDERSIZE:
return
# Click the mouse in the game map , Represents the beginning of the game ( You can start timing )
if self.status_code == -1:
self.status_code = 0
# If it's not in the game , It's no use pressing the mouse
if self.status_code != 0:
return
# Mouse position turn matrix index
coord_x = (self.mouse_pos[0] - self.cfg.BORDERSIZE) // self.cfg.GRIDSIZE
coord_y = self.mouse_pos[1] // self.cfg.GRIDSIZE - 2
mine_clicked = self.mines_matrix[coord_y][coord_x]
# The mouse click
if type_ == 'down':
# -- Press the left and right mouse buttons at the same time
if self.mouse_pressed[0] and self.mouse_pressed[2]:
if mine_clicked.opened and mine_clicked.num_mines_around > 0:
mine_clicked.setstatus(status_code=4)
num_flags = 0
coords_around = self.getaround(coord_y, coord_x)
for (j, i) in coords_around:
if self.mines_matrix[j][i].status_code == 2:
num_flags += 1
if num_flags == mine_clicked.num_mines_around:
for (j, i) in coords_around:
if self.mines_matrix[j][i].status_code == 0:
self.openmine(i, j)
else:
for (j, i) in coords_around:
if self.mines_matrix[j][i].status_code == 0:
self.mines_matrix[j][i].setstatus(status_code=5)
# Mouse release
else:
# -- Left mouse button
if self.mouse_pressed[0] and not self.mouse_pressed[2]:
if not (mine_clicked.status_code == 2 or mine_clicked.status_code == 3):
if self.openmine(coord_x, coord_y):
self.setstatus(status_code=1)
# -- Right mouse button
elif self.mouse_pressed[2] and not self.mouse_pressed[0]:
if mine_clicked.status_code == 0:
mine_clicked.setstatus(status_code=2)
elif mine_clicked.status_code == 2:
mine_clicked.setstatus(status_code=3)
elif mine_clicked.status_code == 3:
mine_clicked.setstatus(status_code=0)
# -- Press the left and right mouse buttons at the same time
elif self.mouse_pressed[0] and self.mouse_pressed[2]:
mine_clicked.setstatus(status_code=1)
coords_around = self.getaround(coord_y, coord_x)
for (j, i) in coords_around:
if self.mines_matrix[j][i].status_code == 5:
self.mines_matrix[j][i].setstatus(status_code=0)
''' Open ray '''
def openmine(self, x, y):
mine_clicked = self.mines_matrix[y][x]
if mine_clicked.is_mine_flag:
for row in self.mines_matrix:
for item in row:
if not item.is_mine_flag and item.status_code == 2:
item.setstatus(status_code=7)
elif item.is_mine_flag and item.status_code == 0:
item.setstatus(status_code=1)
mine_clicked.setstatus(status_code=6)
return True
mine_clicked.setstatus(status_code=1)
coords_around = self.getaround(y, x)
num_mines = 0
for (j, i) in coords_around:
num_mines += int(self.mines_matrix[j][i].is_mine_flag)
mine_clicked.setnumminesaround(num_mines)
if num_mines == 0:
for (j, i) in coords_around:
if self.mines_matrix[j][i].num_mines_around == -1:
self.openmine(i, j)
return False
''' Get the surrounding coordinate points of the coordinate points '''
def getaround(self, row, col):
coords = []
for j in range(max(0, row-1), min(row+1, self.cfg.GAME_MATRIX_SIZE[1]-1)+1):
for i in range(max(0, col-1), min(col+1, self.cfg.GAME_MATRIX_SIZE[0]-1)+1):
if j == row and i == col:
continue
coords.append((j, i))
return coords
''' Whether it is in the game '''
@property
def gaming(self):
return self.status_code == 0
''' Number of Mines marked as mines '''
@property
def flags(self):
num_flags = 0
for row in self.mines_matrix:
for item in row: num_flags += int(item.status_code == 2)
return num_flags
''' The number of mines that have been opened '''
@property
def openeds(self):
num_openeds = 0
for row in self.mines_matrix:
for item in row: num_openeds += int(item.opened)
return num_openeds
Here are only a few places that may be incomprehensible to little friends :
When we opened ray, we used recursion , The function is when there is no thunder around the clicked square , The system will automatically open the grid around this grid , To achieve the effect that sometimes clicking a square can open a large square , The surroundings here refer specifically to all squares in the nine palace grid centered on the target grid ;
Press the left and right mouse buttons together on the open grid , If the square around this square has been marked as thunder, the number is consistent with the number displayed on this square , Just open the squares around this square that are not marked as Ray ( So if you mark it wrong , When you open it together, it will show that your game has GG 了 ).
After defining the necessary element classes in the game, instantiate them in the main function of the game :
# Instantiate the game map
minesweeper_map = MinesweeperMap(cfg, images)
position = (cfg.SCREENSIZE[0] - int(cfg.GRIDSIZE * 1.25)) // 2, (cfg.GRIDSIZE * 2 - int(cfg.GRIDSIZE * 1.25)) // 2
emoji_button = EmojiButton(images, position=position)
fontsize = font.size(str(cfg.NUM_MINES))
remaining_mine_board = TextBoard(str(cfg.NUM_MINES), font, (30, (cfg.GRIDSIZE*2-fontsize[1])//2-2), cfg.RED)
fontsize = font.size('000')
time_board = TextBoard('000', font, (cfg.SCREENSIZE[0]-30-fontsize[0], (cfg.GRIDSIZE*2-fontsize[1])//2-2), cfg.RED)
time_board.is_start = False
Then write a game main loop to update the current game state according to the user's operation :
# The main cycle of the game
clock = pygame.time.Clock()
while True:
screen.fill(cfg.BACKGROUND_COLOR)
# -- Key detection
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = event.pos
mouse_pressed = pygame.mouse.get_pressed()
minesweeper_map.update(mouse_pressed=mouse_pressed, mouse_pos=mouse_pos, type_='down')
elif event.type == pygame.MOUSEBUTTONUP:
minesweeper_map.update(type_='up')
if emoji_button.rect.collidepoint(pygame.mouse.get_pos()):
minesweeper_map = MinesweeperMap(cfg, images)
time_board.update('000')
time_board.is_start = False
remaining_mine_board.update(str(cfg.NUM_MINES))
emoji_button.setstatus(status_code=0)
# -- Update time display
if minesweeper_map.gaming:
if not time_board.is_start:
start_time = time.time()
time_board.is_start = True
time_board.update(str(int(time.time() - start_time)).zfill(3))
# -- Update the number of remaining mines display
remianing_mines = max(cfg.NUM_MINES - minesweeper_map.flags, 0)
remaining_mine_board.update(str(remianing_mines).zfill(2))
# -- Update expression
if minesweeper_map.status_code == 1:
emoji_button.setstatus(status_code=1)
if minesweeper_map.openeds + minesweeper_map.flags == cfg.GAME_MATRIX_SIZE[0] * cfg.GAME_MATRIX_SIZE[1]:
minesweeper_map.status_code = 1
emoji_button.setstatus(status_code=2)
# -- Display the current game status map
minesweeper_map.draw(screen)
emoji_button.draw(screen)
remaining_mine_board.draw(screen)
time_board.draw(screen)
# -- Update screen
pygame.display.update()
clock.tick(cfg.FPS)
This is the end of the article , Thanks for watching ,Python24 A series of small games , The next article shares Xiaole games
To thank readers , I want to share with you some of my recent collections of programming dry goods , Give back to every reader , I hope I can help you .
Dry goods mainly include :
① 2000 Multiple copies Python e-book ( There should be both mainstream and classic books )
② Python Standard library information ( The most complete Chinese version )
③ Project source code ( Forty or fifty interesting and classic training projects and source code )
④ Python Basic introduction 、 Reptiles 、web Development 、 Big data analysis video ( Suitable for Xiaobai to learn )
⑤ Python Learning Roadmap ( Farewell to bad learning )
⑥ Two days of Python Live access to reptile camp
All done~ Private letter to get the complete source code ..
Looking back
Python Realization 2048 Little games
Python Realize Gobang online game
Python Realize the Bomber game
Python Realize the classic bean eating games
Python Real dinosaur jump games now
Python Realize the simple version of aircraft war games
Python Realization “ Bunny and Bun” game
Python To achieve the eight character games
Python Realize the puzzle game
Python Achieve the classic 90 Tanks war
Python Realization FlappyBird The little game
Python Achieve tower defense games
Python Push box to achieve small games
Python Realization 24 A little game