PyQt mining mine Game Study Notes (2), pyqt Study Notes
It is best to use auxiliary tools to analyze the source code composed of multiple files.
I have tried Source Navigator and Source Insight. I feel that the latter has more functions and is easy to use.
I. program components and other files
This mine mining game mainly consists of two types of files:
1. program module
Main Program main. py: responsible for setting the main window objects, buttons and menus, event processing, program initialization, and termination.
Game logic module game_scene.py: Responsible for game rules, board display, winning and losing referers, and recording scores.
Game setting module setup. py: the dialog box opened by the menu, which sets the size of the Board and the number of mines.
Game score module score. py: The game score is displayed in the pop-up dialog box after players dig out all mines to win the game.
The default setting module config. py is the size of the Board and the number of mines. It is saved as the default setting.
"Import" file lib. py: imports some common modules. For other files, you only need to import lib. py.
2. GUI blueprint
Main Window: mainwindow. ui
Score dialog box: score. ui
Setting Dialog Box: setup. ui
In addition, there is a default data file config. cfg.
Ii. Program GUI Initialization
This mine mining program directly uses the ui file instead of converting the ui blueprint ui file with pyuic4 into a py file.
The initial settings of the GUI have obvious characteristics, which are different from those of older PyQt programs. This is manifested in the following aspects:
1. First load the GUI blueprint
In the module, the class is in vitro and the loadUiType (ui_file) function is used to load the GUI blueprint into the memory,
To implement specific objects and mechanisms;
Each module does this before instantiating and initializing each class. For example:
Form, base = loadUiType ("mainwindow. ui") in the main. py Module ")
Form, base = loadUiType ("setup. ui") in the setup. py Module ")
Form, base = loadUiType ("score. ui") in the score. py Module ")
2. The method for handling "events" is simplified.
For example, mainwindow. ui has the following menu items:
<addaction name="actionAbout_Qt"/><addaction name="action_New"/><addaction name="action_Pause"/><addaction name="action_Full_Screen"/><addaction name="action_Score"/><addaction name="action_Setup"/>
Add the above name with the header, tail: on _ and _ triggered as the function name,
Modified with @ pyqtSlot () to form a "slot" for processing "signals" in main. py ":
@pyqtSlot()def on_actionAbout_Qt_triggered(self):@pyqtSlot()def on_action_New_triggered(self):@pyqtSlot()def on_action_Pause_triggered(self):@pyqtSlot()def on_action_Full_Screen_triggered(self):@pyqtSlot()def on_action_Score_triggered(self):@pyqtSlot()def on_action_Setup_triggered(self):
The "signal-slot" created by the above method does not involve the function connect,
The PyQt framework defaults to the "signal-slot" association.
In addition, for objects other than the ui file, the "Signal-slot" Association function connect only uses one parameter, that is, the "slot name ",
Compared with Qt, four parameters are much simpler. For example, in class Face of main. py:
scene.changeStatus.connect(self.changeStatus)self.button.clicked.connect(self.me)
In addition, a single signal can be associated with multiple slots. For example:
Signals defined in game_scene.py
changeStatus = pyqtSignal(int)
It is associated with the changeStatus of the two channels in main. py. First, in the above class Face,
The second is in the class MainWindow.
3. emit () triggers "event processing"
Translate emit directly into "launch", and translate it into "send mail" (send a signal ).
Using emit () to trigger event processing is an important method and feature of Qt control program processes.
For example, the class GameScene in the module game_scene.py defines two signals.
changeStatus = pyqtSignal(int) changeFlag = pyqtSignal(int, int)
There are signals:
def setStatus(self, status): ...... self.changeStatus.emit(status)#--------------------------------------def toggleFlag(self, pos): ...... self.changeFlag.emit(self.flag_map.sum(), self.mines) ......
Because both class Face and class MainWindow in main. py define the signal receiver changeStatus,
While MainWindow defines the signal receiver changeFlag, so they will receive and process the above signals separately.
We should also see that Qt's "signal-slot" mechanism does not have any "step-by-step upload" and other flowers, which is concise and practical.
Mining Game Design
I once wrote a mine clearance, but I should not use the mouse hardware parameters. I use the keyboard to input coordinates for each step.
Let's talk about the process.
1. First initialization: Create a 21*21 array, add a wall to the outermost side, enter-1, and then randomly place 10 mines.
2. Click the next part to focus on it!
Category (1). If it's a point to mine-> game over...
Classification (2), point to the number (there is a ray next to), is to judge the four sides of the grid there are several Ray, show
Classification (3). There are no mines around. Dynamic Planning is used here. It is difficult to extend it until it is surrounded by numbers (including-1 on the wall ).
3. It's easy to judge victory. Just keep recording the remaining mines.
What advanced skills are there for mining mini games in windows?
When you see 1, there is a bomb around the number. When you see 2, there are two bombs around the number.