tft每日頭條

 > 生活

 > 傳統月餅對比當下流行月餅

傳統月餅對比當下流行月餅

生活 更新时间:2024-08-21 03:17:22
導語

每年的中秋節,

總會有一大批不明覺厲的月餅悄悄出現~

就口味而言,有甜味、鹹味、鹹甜味、麻辣味;從餡心來講,有五仁、豆沙、冰糖、芝麻、火腿月餅等。

傳統月餅對比當下流行月餅(萬萬沒想到一盒月餅火了)1

​​總有一款适合你

而今年這款月餅卻意外脫穎而出...

當當當當!

​你見過Python餡兒的月餅嘛?

沒錯,這就是今年的新口味月餅

Python牌月餅

傳統月餅對比當下流行月餅(萬萬沒想到一盒月餅火了)2

哈哈哈,前一篇文章就是python牌兒的巧克力月餅,歡迎閱讀哦~

【中秋來襲】卧槽!沒想到,用Python竟能做巧克力月餅![附源碼]

木木子幾乎可以想象,Python牌月餅将受到怎樣的熱捧,大約每個人都想入手一堆

一天吃不完?沒關系,你有中秋長假可以慢慢品嘗。

傳統月餅對比當下流行月餅(萬萬沒想到一盒月餅火了)3

​​假期這麼長、月亮這麼圓,記得要和愛的人團聚​。

而今天木木子想趁時光正好,帶你們去嘗一下自己秘制的中秋“月餅”!

傳統月餅對比當下流行月餅(萬萬沒想到一盒月餅火了)4

正文

本文是制作一款Python月餅牌兒的拼圖小遊戲哈!

Python餡兒的月餅制作就在之前寫過的記得閱讀啦!

(1)環境安裝。

Python版本:Python 3.6 、開發工具:PyCharm。部分Python自帶内置模塊。

第三方模塊:pygame。

注意:在使用第三方模塊時,首先需要使用pip install命令安裝該模塊,可以在命令窗口中執行以下命令:

pip install pygame

(2)月餅圖片素材。

傳統月餅對比當下流行月餅(萬萬沒想到一盒月餅火了)5

(3)遊戲開始界面設置,主要分三個等級的模式:H為5*5模式, M為4*4模式, L為3*3模式。

def ShowStartInterface(screen, width, height): screen.fill(cfg.BACKGROUNDCOLOR) tfont = pygame.font.Font(cfg.FONTPATH, width//4) cfont = pygame.font.Font(cfg.FONTPATH, width//20) title = tfont.render('拼圖遊戲', True, cfg.RED) content1 = cfont.render('按H或M或L鍵開始遊戲', True, cfg.BLUE) content2 = cfont.render('H為5*5模式, M為4*4模式, L為3*3模式', True, cfg.BLUE) trect = title.get_rect() trect.midtop = (width/2, height/10) crect1 = content1.get_rect() crect1.midtop = (width/2, height/2.2) crect2 = content2.get_rect() crect2.midtop = (width/2, height/1.8) screen.blit(title, trect) screen.blit(content1, crect1) screen.blit(content2, crect2) while True: for event in pygame.event.get(): if (event.type == pygame.QUIT) or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == ord('l'): return 3 elif event.key == ord('m'): return 4 elif event.key == ord('h'): return 5 pygame.display.update()

​​(4)準備了幾張月餅的素材圖片,是定義的随機生成,也可以指定哈。

def GetImagePath(rootdir): imagenames = os.listdir(rootdir) assert len(imagenames) > 0 return os.path.join(rootdir, random.choice(imagenames))

(5)遊戲結束界面,完成拼圖。

def ShowEndInterface(screen, width, height): screen.fill(cfg.BACKGROUNDCOLOR) font = pygame.font.Font(cfg.FONTPATH, width//15) title = font.render('恭喜! 你成功完成了拼圖!', True, (233, 150, 122)) rect = title.get_rect() rect.midtop = (width/2, height/2.5) screen.blit(title, rect) pygame.display.update() while True: for event in pygame.event.get(): if (event.type == pygame.QUIT) or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): pygame.quit() sys.exit() pygame.display.update()

遊戲主函數:

''主函數''' def main(): # 初始化 pygame.init() clock = pygame.time.Clock() # 加載圖片 game_img_used = pygame.image.load(GetImagePath(cfg.PICTURE_ROOT_DIR)) game_img_used = pygame.transform.scale(game_img_used, cfg.SCREENSIZE) game_img_used_rect = game_img_used.get_rect() # 設置窗口 screen = pygame.display.set_mode(cfg.SCREENSIZE) pygame.display.set_caption('拼圖遊戲 —— 源碼基地:959755565') # 遊戲開始界面 size = ShowStartInterface(screen, game_img_used_rect.width, game_img_used_rect.height) assert isinstance(size, int) num_rows, num_cols = size, size num_cells = size * size # 計算Cell大小 cell_width = game_img_used_rect.width // num_cols cell_height = game_img_used_rect.height // num_rows # 避免初始化為原圖 while True: game_board, blank_cell_idx = CreateBoard(num_rows, num_cols, num_cells) if not isGameOver(game_board, size): break # 遊戲主循環 is_running = True while is_running: # --事件捕獲 for event in pygame.event.get(): # ----退出遊戲 if (event.type == pygame.QUIT) or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): pygame.quit() sys.exit() # ----鍵盤操作 elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT or event.key == ord('a'): blank_cell_idx = moveL(game_board, blank_cell_idx, num_cols) elif event.key == pygame.K_RIGHT or event.key == ord('d'): blank_cell_idx = moveR(game_board, blank_cell_idx, num_cols) elif event.key == pygame.K_UP or event.key == ord('w'): blank_cell_idx = moveU(game_board, blank_cell_idx, num_rows, num_cols) elif event.key == pygame.K_DOWN or event.key == ord('s'): blank_cell_idx = moveD(game_board, blank_cell_idx, num_cols) # ----鼠标操作 elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: x, y = pygame.mouse.get_pos() x_pos = x // cell_width y_pos = y // cell_height idx = x_pos y_pos * num_cols if idx == blank_cell_idx-1: blank_cell_idx = moveR(game_board, blank_cell_idx, num_cols) elif idx == blank_cell_idx 1: blank_cell_idx = moveL(game_board, blank_cell_idx, num_cols) elif idx == blank_cell_idx num_cols: blank_cell_idx = moveU(game_board, blank_cell_idx, num_rows, num_cols) elif idx == blank_cell_idx-num_cols: blank_cell_idx = moveD(game_board, blank_cell_idx, num_cols) # --判斷遊戲是否結束 if isGameOver(game_board, size): game_board[blank_cell_idx] = num_cells - 1 is_running = False # --更新屏幕 screen.fill(cfg.BACKGROUNDCOLOR) for i in range(num_cells): if game_board[i] == -1: continue x_pos = i // num_cols y_pos = i % num_cols rect = pygame.Rect(y_pos*cell_width, x_pos*cell_height, cell_width, cell_height) img_area = pygame.Rect((game_board[i]%num_cols)*cell_width, (game_board[i]//num_cols)*cell_height, cell_width, cell_height) screen.blit(game_img_used, rect, img_area) for i in range(num_cols 1): pygame.draw.line(screen, cfg.BLACK, (i*cell_width, 0), (i*cell_width, game_img_used_rect.height)) for i in range(num_rows 1): pygame.draw.line(screen, cfg.BLACK, (0, i*cell_height), (game_img_used_rect.width, i*cell_height)) pygame.display.update() clock.tick(cfg.FPS) # 遊戲結束界面 ShowEndInterface(screen, game_img_used_rect.width, game_img_used_rect.height)

​遊戲效果:

傳統月餅對比當下流行月餅(萬萬沒想到一盒月餅火了)6

傳統月餅對比當下流行月餅(萬萬沒想到一盒月餅火了)7

​​​​​​​​​​總結

最難的那關,我圖都不敢放了,你們自己去試試吧??制作不易,記得一鍵三連哦!!

傳統月餅對比當下流行月餅(萬萬沒想到一盒月餅火了)8

如果需要本文完整的代碼 圖片素材。

Python新手安裝包、免費激活碼、等等更多Python資料

老規矩源碼基地見:【私信小編06】即可免費領取哦!!

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关生活资讯推荐

热门生活资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved