PyQt mining mine Game Study Notes (7), pyqt Study Notes
1. Game Rules
Mines are randomly buried in the "checkerboard" square. When mines are dug to defeat, all the mines are dug to win.
2. Game Space Representation
The game occurs on the board. The scene and rules of the game are reflected on the board.
The Board consists of a scenario disk and a logic disk.
The "scene disk" is the game board for players to mine.
"Logical Disk" is the "Minefield disk", "EMPTY area disk", and "prompt disk" required to implement game rules ".
The Logical Disk is represented by an array.
The Board, game rules, and so on are set and represented by the game_scene.py module.
3. starting point of the game
The mining game started with start () of class GameScene in game_scene.py ().
Each new game starts by calling this function. Including:
The call in init () of MainWindow of main. py at the beginning of the program;
Choose on_action_New_triggered () when you start a new game;
Set the call in on_action_Setup_triggered () when playing the game;
When the button at the top of the board is pressed, it is called in the me () function.
Def start (self): self. setStatus (RUNNING) # Set the game status self. mine_map = random_map (self. map_size, self. mines) # deploy mine self. hint_map = hint_map (self. mine_map) # prompt review self. flag_map = np. zeros (self. map_size, dtype = np. bool) # delete self. open_map = np. zeros (self. map_size, dtype = np. bool) # Empty Area self. update () # scenario disk update
Note! The following is a harmful bug.
Self. flag_map = np. zeros (self. map_size, dtype = np. bool)
All statements related to self. flag_map in the program should be deleted.
In the first sentence, set the game status, and in the last sentence, "scenario disk" is updated, which will be described later.
In addition to the deletion bug, the three sentences in the middle are placed on the "Logical Disk", marked with thunder, and blank areas.
4. Randomly deploy mines on the Logical Disk
def random_map(size, count): mine_map = np.zeros(size, dtype=np.bool) x, y = size while count > 0: rx = random.randint(0, x-1) ry = random.randint(0, y-1) if not mine_map[rx, ry]: count -= 1 mine_map[rx, ry] = True return mine_map
Random_map is a module-level global function. Size indicates the size and width of the chessboard, and count indicates the number of mines.
The np imported from lib. py, that is, numpy. Np. zeros returns a two-dimensional array,
The size of the array is determined by the size. For example, (3, 3); All nine members of the array are False. For example:
>>> mine_map = np.zeros((3,3), dtype=np.bool)>>> mine_maparray([[False, False, False], [False, False, False], [False, False, False]], dtype=bool)>>> mine_map[1,1]False>>>
Rx and ry are random integers. 0 <= rx <= X-1, ry is similar.
The while LOOP means that if the number of mines is count> 0,
Generate rx and ry randomly as coordinates, and check whether there are mines in the mine_map [rx, ry] Logical Disk;
If there is no mine, that is, the value of this field is equal to False, the value is granted to True, and the mine counter is reduced by 1;
If there is a ray here, that is, the value is equal to True, the loop continues, and a new coordinate mine is generated until all the tasks are completed.
5. Mark the prompt on the Logical Disk
def hint_map(mine_map): x, y = mine_map.shape hint_map = np.zeros((x+2, y+2), dtype=np.uint8) for dx in range(x): for dy in range(y): if mine_map[dx, dy]: for tx in range(3): for ty in range(3): hint_map[dx+tx, dy+ty] += 1 return hint_map[1:x+1,1:y+1]
The labeling prompt on the Logical Disk refers to the number of mines buried around the blank mark.
The algorithm of this function is clever and worth your reference.
Note! Mine_map is an array created by numpy and has the shape attribute. This attribute is not available in the Python list.
The prompt disk hint_map is a whole circle larger than that of the mine disk mine_map. If mine_map = (10, 10), hint_map = (12, 12)
This is equivalent to placing mine_map In The Middle Of hint_map, which avoids the trouble of checking and determining the array boundary and simplifies the specific algorithm.
The data type of the hint_map member is an integer, and its values are all initialized to zero.
The outer two for loops are used to traverse the entire Logical Disk mine_map;
If a mine is found at disk space mine_map [dx, dy], that is, if the value is True, the inner loop starts,
With dx and dy as the origin, add the number of mines marked in 9 cells to the row and column on the right, and the row on the bottom to 1.
This return hint_map [1: x +: y + 1] cleverly moves hint_map up a row and shifts one column to the left through data cutting,
The actual situation is exactly the same as that of mine_map.
6. blank area of the Logical Disk
This cannot be set in advance. This is because it is the result of mouse clicks during a player's game.
How can I mine ordinary computer games?
Change the game to 480 blocks and 10 mines. This way, you can win the game if you are lucky ..
Thank you!