1. Support Chinese display

Pygame cannot directly support Chinese display. If you use the system font library, there may be a problem that fonts do not exist on different machines. Therefore, it is better to put a font library in the package. Use and draw methods

# -*- coding=utf-8 -*- import pygame from pygame.locals import * pygame.init() screen = Pygame. Display. Set_mode ((400300)) myfont = pygame. The font. The font (u 'syht. Otf', 30) white = 255,255,255 textImage = myfont. Render (u' hello Pygame', True, white) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: Quit () exit() screen.fill((0,0,0)) screen.blit(textImage, (100,100)) pygame.display.update()Copy the code

Support for different environments of the system

Add these lines to avoid coding inconsistencies on Windows and Linux

import sys

reload(sys)
sys.setdefaultencoding('utf-8')
Copy the code

Multilingual design

Add a language.conf file that looks something like this (note that the node key must be lowercase here)

[common] default = en [supports] en = simplified Chinese en = English [en] Please select language = Cancel = cancel exit = Exit [en] please select language = Please select language cancel = Cancel exit = ExitCopy the code

There are four nodes in it: The supports node lists all supported languages. If you want to add language support, add the name of the supported language to the supports node and add the translation content to the end of the node. New language support is complete.

Two libraries are defined, one LanguageConfigParser class to help read and write the configuration file, and the other LanguageLib singleton class to do the language package work.

class LanguageConfigParser(object):
    def __init__(self):
        self.conf = ConfigParser()
        self.conf.read(LANGUAGE_CONF_FILE)

    def get_support_names(self):
        return self.conf.options('supports')

    def get_support_label(self, name):
        return self.conf.get('supports', name)

    def get_cut_language(self):
        return self.conf.get('common', 'default')

    def set_cut_language(self, name):
        self.conf.set('common', 'default', name)

    def get_words(self, language):
        words = self.conf.options(language)
        dic = {}
        for word in words:
            dic[word] = self.conf.get(language, word).decode('utf-8')
        return dic

    def save(self):
        self.conf.write(open(LANGUAGE_CONF_FILE, 'w+'))


class LanguageLib(object):
    _instance_lock = threading.Lock()
    def __init__(self):
        self.dic={}

    @classmethod
    def instance(cls, *args, **kwargs):
        if not hasattr(LanguageLib, "_instance"):
            LanguageLib._instance = LanguageLib(*args, **kwargs)
        return LanguageLib._instance

    def reload_language(self):
        lang_conf_parser = LanguageConfigParser()
        cur_lang = lang_conf_parser.get_cut_language()
        self.dic = lang_conf_parser.get_words(cur_lang)

    def get_text(self, str):
        return self.dic.get(str, str)
Copy the code

The set_cut_language function of LanguageConfigParser is called after the language is selected to save the selected language for the new Settings. Get_text of LanguageLib provides the function of retrieving text. It actually uses the internal dictionary for value. When switching languages, the system calls reload_language to reload the language dictionary, so that the dictionary will be refreshed.

The full tetris code has been uploaded to github: github.com/zhangenter/…