PyQt mining mine Game Study Notes (3), pyqt Study Notes

Source: Internet
Author: User

PyQt mining mine Game Study Notes (3), pyqt Study Notes

This program uses Python's distinctive syntax routines. First.

The class MainWindow function init () in main. py has the following statement:

self.scene.setMap((conf.w, conf.h), conf.mines)

The conf variable is defined in config. py and introduced by the following statement:

from config import *

What are conf. w, conf. h and conf. mines here? Therefore, transfer to config. py to find out.

#module: configimport json    DEFAULTS = {    'splash':False,    'w': 10,    'h': 10,    'mines': 1,    'scores': [],    }DEFAULT_SAVE_FILE = "config.cfg"class _Conf:    def __getattr__(self,name):        return DEFAULTS[name]    def __setattr__(self,name,value):        if type(value) != type(DEFAULTS[name]):            raise Exception("%s %s is not %s:" %                             (value, type(value), type(DEFAULTS[name])))        DEFAULTS[name] = value    def save(self,fileName=DEFAULT_SAVE_FILE):        with open(fileName, 'w+') as fp:            json.dump(DEFAULTS, fp, indent=True)                def load(self,fileName=DEFAULT_SAVE_FILE):        with open(fileName, 'r') as fp:            global DEFAULTS            DEFAULTS = json.load(fp)conf = _Conf()try:    conf.load()except:    pass

As you can see, importing main. py to this module may cause the following:

Create and initialize a module-level variable. The DEFAULTS dictionary is used;

Create and initialize a module-level variable, DEFAULT_SAVE_FILE = "config. cfg"

Create a module-level variable conf and use this variable to instantiate _ Conf;

Use conf to reference the function load () in _ Conf and read the data in the config. cfg file into DEFAULTS.

There are no member variables w, h, and mines in the _ Conf file. But why use conf. w, conf. h, and conf. mines?

The secret lies in the special function _ getattr _ (name) in _ Conf)

When reading data with conf. w, the value of the parameter name of __getattr _ is "w ",

The returned value is DEFAULTS ["w"]. The value is equal to 10.

This syntax routine of Python reads the DEFAULTS operation of the dictionary and forms a member accessing the class.


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.

Error Message

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.