python开发打地鼠4 #
- 游戏初始化
- 游戏退出函数
- 游戏配置类
收获 #
掌握python类的使用方法,通过PyGame实践学会独立建立和更新项目,完成打地鼠游戏开发。
游戏初始化 #
本小节为游戏构造函数的定义。
def InitPygame(screensize, title='打地鼠', init_mixer=True):
pygame.init()
if init_mixer: pygame.mixer.init()
screen = pygame.display.set_mode(screensize)
pygame.display.set_caption(title)
return screen
游戏退出函数 #
本小节为游戏退出函数的定义。
def QuitGame(use_pygame=True):
if use_pygame:
pygame.quit()
sys.exit()
游戏配置类 #
本小节为游戏配置类的实现过程。
class Config():
# 根目录
rootdir = os.path.split(os.path.abspath(__file__))[0]
# FPS
FPS = 100
# 屏幕大小
SCREENSIZE = (993, 477)
# 标题
TITLE = '打地鼠'
# 游戏常量参数
HOLE_POSITIONS = [(90, -20), (405, -20), (720, -20), (90, 140), (405, 140), (720, 140), (90, 290), (405, 290), (720, 290)]
BROWN = (150, 75, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
RECORD_PATH = os.path.join(rootdir, 'score.rec')
# 背景音乐路径
BGM_PATH = os.path.join(rootdir, 'resources/audios/bgm.mp3')
# 游戏声音路径
SOUND_PATHS_DICT = {
'count_down': os.path.join(rootdir, 'resources/audios/count_down.wav'),
'hammering': os.path.join(rootdir, 'resources/audios/hammering.wav'),
}
# 字体路径
FONT_PATH = os.path.join(rootdir, 'resources/fonts/Gabriola.ttf')
# 游戏图片路径
IMAGE_PATHS_DICT = {
'hammer': [os.path.join(rootdir, 'resources/images/hammer0.png'), os.path.join(rootdir, 'resources/images/hammer1.png')],
'begin': [os.path.join(rootdir, 'resources/images/begin.png'), os.path.join(rootdir, 'resources/images/begin1.png')],
'again': [os.path.join(rootdir, 'resources/images/again1.png'), os.path.join(rootdir, 'resources/images/again2.png')],
'background': os.path.join(rootdir, 'resources/images/background.png'),
'end': os.path.join(rootdir, 'resources/images/end.png'),
'mole': [
os.path.join(rootdir, 'resources/images/mole_1.png'), os.path.join(rootdir, 'resources/images/mole_laugh1.png'),
os.path.join(rootdir, 'resources/images/mole_laugh2.png'), os.path.join(rootdir, 'resources/images/mole_laugh3.png')
]
}
小结 #
本节介绍了游戏初始化函数、退出函数,并介绍了游戏配置类的实现方法
需要掌握并实现游戏初始化退出的方法