pygame8 Minesweeper game

1. Game rules:

1. Click on the square, if it is a mine, the game will fail, find all the mines and win the game

2. If a number appears on the square, it means how many mines there are in the eight surrounding squares

2. The main logic of the game:

The main logic is to call run_game, then loop to detect events and update the screen

There are two main types of detection event logic, one is whether to exit, and the other is whether there is a mouse click

Updating the screen is mainly divided into three parts, the first is to whiten the screen, then draw the overlay, and finally update the cache to the screen

At this point, the main logic of the game is completed

3. Game details

According to this main logic, we can design a game class Game and define three functions,

run_game()

_event_check()

_update_screen()

Corresponding to running the main logic of the game, event detection, and updating the screen

Now let's take a look at what the so-called covering is:

It can be seen from the figure that it is a list of 20*20 squares. We can define a class Cover to wrap how to draw these small squares.

It seems that we need to design a list with a length of 20 and a width of 20. Each square is a square of 25 pixels. When drawing, we can use a loop to draw the squares in the list with draw.rect in turn.

for cur in self.covers:
    pygame.draw.rect(self.screen, self.setting.cover_color, [cur[0] * 25, cur[1] * 25, 24, 24])

Note here that the width and height of the grid are both 25, but the width and height should be one pixel less when actually drawn. The reason is that if the grid is full, the grid boundaries will be mixed together and cannot be seen.

self.covers needs to be defined at the beginning:

    def __init__(self, setting, screen):  # 游戏参数设置和游戏主界面
        self.setting = setting
        self.screen = screen

        self.covers = []  # 存储未被点击过的方块的覆盖物的位置
        for i in range(20):
            for j in range(20):
                self.covers.append([i, j])  # 刚开始时整个界面都是被覆盖的

The implementation code of the main program is very simple:

if __name__ == '__main__':
    my_game = Game()
    my_game.run_game()

The definition of Game is mainly to implement the three functions defined above. Covers also need to be introduced during initialization:


class Game:
    def __init__(self):
        pygame.init()
        self.setting = setting()
        self.screen = pygame.display.set_mode(self.setting.screen_size)
        self.covers = Cover(self.setting, self.screen)  # 表面覆盖物

    def run_game(self):
        while True:
            self._event_check_()  # 检测事件
            self._update_screen_()  # 更新屏幕

    def _event_check_(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:  # 结束游戏
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                x, y = pygame.mouse.get_pos()  # 检测到单击鼠标事件,将鼠标的位置传入
                self.covers.delete(x, y)  # 删除对应方块上的覆盖物

    def _update_screen_(self):
        self.screen.fill(self.setting.background_color)  # 填充背景颜色
        self.covers.show()  # 将还没有被点击过的数字展现出来
        pygame.display.flip()  # 更新屏幕显示,将上面所做的工作展现在游戏界面上

Finally, attach all the code of this lesson:
 

import pygame
from settings import setting  # 游戏参数设置
from covers import Cover  # 未点击方块时表面的覆盖物
import sys


class Game:
    def __init__(self):
        pygame.init()
        self.setting = setting()
        self.screen = pygame.display.set_mode(self.setting.screen_size)
        self.covers = Cover(self.setting, self.screen)  # 表面覆盖物

    def run_game(self):
        while True:
            self._event_check_()  # 检测事件
            self._update_screen_()  # 更新屏幕

    def _event_check_(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:  # 结束游戏
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                x, y = pygame.mouse.get_pos()  # 检测到单击鼠标事件,将鼠标的位置传入
                self.covers.delete(x, y)  # 删除对应方块上的覆盖物

    def _update_screen_(self):
        self.screen.fill(self.setting.background_color)  # 填充背景颜色
        self.covers.show()  # 将还没有被点击过的数字展现出来
        self.covers.show_frame()

        pygame.display.flip()  # 更新屏幕显示,将上面所做的工作展现在游戏界面上


if __name__ == '__main__':
    my_game = Game()
    my_game.run_game()
"""
@funcs: 覆盖们
"""
import pygame


class Cover:
    """管理游戏覆盖物的类"""

    def __init__(self, setting, screen):  # 游戏参数设置和游戏主界面
        self.setting = setting
        self.screen = screen

        self.covers = []  # 存储未被点击过的方块的覆盖物的位置
        for i in range(20):
            for j in range(20):
                self.covers.append([i, j])  # 刚开始时整个界面都是被覆盖的

    def show_frame(self):
        for i in range(20):
            pygame.draw.line(self.screen, self.setting.frame_color, [0, i*25], [500, i*25])
            pygame.draw.line(self.screen, self.setting.frame_color, [i*25, 0], [i*25, 500])

    def show(self):  # 将所有未被点击过的方块展现出来
        for cur in self.covers:
            pygame.draw.rect(self.screen, self.setting.cover_color, [cur[0] * 25, cur[1] * 25, 24, 24])

    def delete(self, posx, posy):
        x = posx // 25
        y = posy // 25
        if [x, y] in self.covers:
            self.covers.remove([x, y])
class setting:
    """管理游戏中的参数的类"""

    def __init__(self):
        self.screen_size = [500, 500]  # 屏幕大小
        self.background_color = [255, 255, 255]  # 背景色
        self.frame_width = 1  # 线条粗细
        self.cover_color = [150, 150, 150]  # 展示界面的颜色
        self.frame_color = [120, 120, 120]

Guess you like

Origin blog.csdn.net/luhouxiang/article/details/129108001