Python game development, pyGame module, python implementation of minesweeping games

Cooci 2021-08-09 13:30:41
python game development pygame module


Today I'll share with you a minesweeping game , I don't say much nonsense , Let's start happily ~

Effect display

 Insert picture description here

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 :

 Mine clearance

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. :
 Insert picture description here

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
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)))
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

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):
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):
# Import image
self.images = images
self.image = self.images['face_normal']
self.rect = self.image.get_rect()
self.rect.left, = 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):
# Import image
self.images = images
self.image = self.images['blank']
self.rect = self.image.get_rect()
self.rect.left, = 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)
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))
# Random mine laying
for i in random.sample(range(cfg.GAME_MATRIX_SIZE[0]*cfg.GAME_MATRIX_SIZE[1]), cfg.NUM_MINES):
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:
# 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:
# 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:
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)
for (j, i) in coords_around:
if self.mines_matrix[j][i].status_code == 0:
# Mouse release
# -- 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):
# -- Right mouse button
elif self.mouse_pressed[2] and not self.mouse_pressed[0]:
if mine_clicked.status_code == 0:
elif mine_clicked.status_code == 2:
elif mine_clicked.status_code == 3:
# -- Press the left and right mouse buttons at the same time
elif self.mouse_pressed[0] and self.mouse_pressed[2]:
coords_around = self.getaround(coord_y, coord_x)
for (j, i) in coords_around:
if self.mines_matrix[j][i].status_code == 5:
''' 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:
elif item.is_mine_flag and item.status_code == 0:
return True
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)
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:
coords.append((j, i))
return coords
''' Whether it is in the game '''
def gaming(self):
return self.status_code == 0
''' Number of Mines marked as mines '''
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 '''
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:
# -- Key detection
for event in pygame.event.get():
if event.type == pygame.QUIT:
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:
if emoji_button.rect.collidepoint(pygame.mouse.get_pos()):
minesweeper_map = MinesweeperMap(cfg, images)
time_board.is_start = False
# -- 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)
# -- Update expression
if minesweeper_map.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
# -- Display the current game status map
# -- Update screen

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 Xiaole games

Python Real dinosaur jump games now

Python Realize the simple version of aircraft war games

Python Realize Tetris games

Python Alien invasion games

Python Realization “ Bunny and Bun” game

Python To achieve the eight character games

Python Realize the puzzle game

Python Realize skiing games

Python Achieve the classic 90 Tanks war

Python Realization FlappyBird The little game

Python Achieve tower defense games

Python Fruit and gold coins

Python Push box to achieve small games

Python Realization 24 A little game

Python Realize table tennis games

Python Realize the brick game

Python Realized the maze game

Python To achieve the hamster game


  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database