Pyqt mining mine Game Study Notes (2)

Source: Internet
Author: User

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.

"Import" file Lib. py: imports some common modules. For other files, you only need to import Lib. py.

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.

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

Qt's "signal-slot" mechanism does not have any "step-by-step upload" and other flowers, which is concise and practical.

In addition, the correlated function connect of "signal-slot" only uses one parameter, that is, "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.

Pyqt mining mine Game Study Notes (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.