跳至正文
View Categories

< 1 min read

python开发打地鼠6 #

  1. pygame基础类1
  2. pygame基础类2

收获 #

掌握python类的使用方法,通过PyGame实践学会独立建立和更新项目,完成打地鼠游戏开发。

pygame基础类1 #

本小节为pygame基础类的构造函数定义。

class PygameBaseGame():
    def __init__(self, config, **kwargs):
        # 设置属性
        self.config = config
        # 初始化
        self.initialize()
        # 用户可以覆盖默认参数
        for key, value in kwargs.items():
            if hasattr(self, key): setattr(self, key, value)

pygame基础类2 #

本小节为pygame基础类中游戏运行函数、初始化函数的设置。

##############################在PygameBaseGame类下,__init__()函数后面,添加下列新的功能函数###################
'''运行游戏'''
def run(self):
    raise NotImplementedError('not to be implemented...')
'''初始化'''
def initialize(self):
    self.screen = InitPygame(screensize=self.config.SCREENSIZE, title=self.config.TITLE)
    bgm_path = self.config.BGM_PATH if hasattr(self.config, 'BGM_PATH') else None
    font_paths_dict = self.config.FONT_PATHS_DICT if hasattr(self.config, 'FONT_PATHS_DICT') else None
    image_paths_dict = self.config.IMAGE_PATHS_DICT if hasattr(self.config, 'IMAGE_PATHS_DICT') else None
    sound_paths_dict = self.config.SOUND_PATHS_DICT if hasattr(self.config, 'SOUND_PATHS_DICT') else None
    self.resource_loader = PygameResourceLoader(
        bgm_path=bgm_path,
        font_paths_dict=font_paths_dict,
        image_paths_dict=image_paths_dict,
        sound_paths_dict=sound_paths_dict,
    )

小结 #

本节介绍了pygame基础类的创建过程和功能函数的实现
需要掌握并实现如何使用pygame在项目中创建游戏基础类