在windows系统中有一个自带游戏,扫雷。今天我们来做一个一模一样的。
import pygame import cfg import sys import time from modules.gamemap import * from modules.emojibutton import * from modules.text import * def main(): pygame.init() screen = pygame.display.set_mode((610, 365)) pygame.display.set_caption('扫雷') # 导入所有图片 images = {} for key, value in cfg.IMAGE_PATHS.items(): if key in ['face_fail', 'face_normal', 'face_success']: image = pygame.image.load(value) # 加载png图片 images[key] = pygame.transform.smoothscale(image, (25, 25)) else: image = pygame.image.load(value).convert() # 加载bmp图片格式转化成png images[key] = pygame.transform.smoothscale(image, (20, 20)) font = pygame.font.Font(cfg.FONT_PATH, cfg.FONT_SIZE) # 载入字体 pygame.mixer.music.load(cfg.BGM_PATH) # pygame.mixer.music.play(-1) # 单曲循环播放音乐 # 创建地图 minesweeper_map = MinesweeperMap(cfg, images) # 创建表情按钮 emoji_button = EmojiButton(images, (292, 7)) # fontsize = font.size((str(cfg.NUM_MINES))) remaining_mine_board = TextBoard(str(cfg.NUM_MINES), font, (30, -6), cfg.RED) # 创建显示剩余雷数 time_board = TextBoard('000', font, (518, -6), cfg.RED) time_board.is_start = False # 时间开始标记 clock = pygame.time.Clock() while True: screen.fill(cfg.BACKGROUND_COLOR) # 背景色 for event in pygame.event.get(): # 获取鼠标或键盘动作 if event.type == pygame.QUIT: # 关闭系统 256 # print('quit:',pygame.QUIT) pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: # 1025 mouse_pos = event.pos # 获取按下坐标(x,y) mouse_pressed = pygame.mouse.get_pressed() # (左,中,右) minesweeper_map.update(mouse_pressed=mouse_pressed, mouse_pos=mouse_pos, type_='down') elif event.type == pygame.MOUSEBUTTONUP: minesweeper_map.update(type_='up') if emoji_button.rect.collidepoint(pygame.mouse.get_pos()): # print('表情按钮') # 创建地图 minesweeper_map = MinesweeperMap(cfg, images) # 创建表情按钮 emoji_button = EmojiButton(images, (292, 7)) time_board.update('000') time_board.is_start = False # 更新显示时间 if minesweeper_map.gaming: # 如果游戏正在进行中 if not time_board.is_start: start_time = time.time() time_board.is_start = True time_board.update(str(int(time.time() - start_time)).zfill(3)) # 更新显示时间的文字 if minesweeper_map.status_code == 1: # 失败状态 emoji_button.setstatus(status_code=1) # 状态码为1, 代表失败的表情 # 如果打开雷的数量 + 标记雷的数量 = 行数*列数 那么就胜利了 if minesweeper_map.openeds + minesweeper_map.flags == cfg.GAME_MATRIX_SIZE[0] * cfg.GAME_MATRIX_SIZE[1]: minesweeper_map.status_code = 1 # 游戏结状态 emoji_button.setstatus(status_code=2) # 胜利表情 remaining_mines = max(cfg.NUM_MINES - minesweeper_map.flags, 0) # 剩余雷数量 remaining_mine_board.update(str(remaining_mines).zfill(2)) remaining_mine_board.draw(screen) minesweeper_map.draw(screen) # 画地图 emoji_button.draw(screen) # 画表情按钮 time_board.draw(screen) # 画时间显示 pygame.display.update() # 刷新 clock.tick(60) # 刷新频率 if __name__ == '__main__': main()
这是主函数,接下来上另一个程序。
'''配置文件''' import os '''图片素材路径''' IMAGE_PATHS = { '0': os.path.join(os.getcwd(), 'resources/images/0.bmp'), '1': os.path.join(os.getcwd(), 'resources/images/1.bmp'), '2': os.path.join(os.getcwd(), 'resources/images/2.bmp'), '3': os.path.join(os.getcwd(), 'resources/images/3.bmp'), '4': os.path.join(os.getcwd(), 'resources/images/4.bmp'), '5': os.path.join(os.getcwd(), 'resources/images/5.bmp'), '6': os.path.join(os.getcwd(), 'resources/images/6.bmp'), '7': os.path.join(os.getcwd(), 'resources/images/7.bmp'), '8': os.path.join(os.getcwd(), 'resources/images/8.bmp'), 'ask': os.path.join(os.getcwd(), 'resources/images/ask.bmp'), 'blank': os.path.join(os.getcwd(), 'resources/images/blank.bmp'), 'blood': os.path.join(os.getcwd(), 'resources/images/blood.bmp'), 'error': os.path.join(os.getcwd(), 'resources/images/error.bmp'), 'face_fail': os.path.join(os.getcwd(), 'resources/images/face_fail.png'), 'face_normal': os.path.join(os.getcwd(), 'resources/images/face_normal.png'), 'face_success': os.path.join(os.getcwd(), 'resources/images/face_success.png'), 'flag': os.path.join(os.getcwd(), 'resources/images/flag.bmp'), 'mine': os.path.join(os.getcwd(), 'resources/images/mine.bmp') } '''字体路径''' FONT_PATH = os.path.join(os.getcwd(), 'resources/font/font.TTF') FONT_SIZE = 40 '''BGM路径''' BGM_PATH = os.path.join(os.getcwd(), 'resources/music/bgm.mp3') '''游戏相关参数''' FPS = 60 # 刷新事件 60毫秒 GRIDSIZE = 20 NUM_MINES = 99 # 99个地雷 GAME_MATRIX_SIZE = (30, 16) BORDERSIZE = 5 SCREENSIZE = (GAME_MATRIX_SIZE[0]*GRIDSIZE+BORDERSIZE*2, (GAME_MATRIX_SIZE[1]+2)*GRIDSIZE+BORDERSIZE) '''颜色''' BACKGROUND_COLOR = (225, 225, 225) RED = (200, 0, 0)
这就是所有代码,图片要自行查找,我发送不了图片。