Python實現(xiàn)消消樂小游戲
更新時間:2021年09月24日 14:29:30 作者:紅目香薰
這篇文章主要為大家詳細介紹了Python實現(xiàn)消消樂小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Python實現(xiàn)消消樂小游戲的具體代碼,供大家參考,具體內(nèi)容如下
玩法:三個相連就能消除
源碼分享:
import os import sys import cfg import pygame from modules import * '''游戲主程序''' def main(): pygame.init() screen = pygame.display.set_mode(cfg.SCREENSIZE) pygame.display.set_caption('Gemgem —— 九歌') # 加載背景音樂 pygame.mixer.init() pygame.mixer.music.load(os.path.join(cfg.ROOTDIR, "resources/audios/bg.mp3")) pygame.mixer.music.set_volume(0.6) pygame.mixer.music.play(-1) # 加載音效 sounds = {} sounds['mismatch'] = pygame.mixer.Sound(os.path.join(cfg.ROOTDIR, 'resources/audios/badswap.wav')) sounds['match'] = [] for i in range(6): sounds['match'].append(pygame.mixer.Sound(os.path.join(cfg.ROOTDIR, 'resources/audios/match%s.wav' % i))) # 加載字體 font = pygame.font.Font(os.path.join(cfg.ROOTDIR, 'resources/font/font.TTF'), 25) # 圖片加載 gem_imgs = [] for i in range(1, 8): gem_imgs.append(os.path.join(cfg.ROOTDIR, 'resources/images/gem%s.png' % i)) # 主循環(huán) game = gemGame(screen, sounds, font, gem_imgs, cfg) while True: score = game.start() flag = False # 一輪游戲結(jié)束后玩家選擇重玩或者退出 while True: for event in pygame.event.get(): if event.type == pygame.QUIT or (event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE): pygame.quit() sys.exit() elif event.type == pygame.KEYUP and event.key == pygame.K_r: flag = True if flag: break screen.fill((135, 206, 235)) text0 = 'Final score: %s' % score text1 = 'Press <R> to restart the game.' text2 = 'Press <Esc> to quit the game.' y = 150 for idx, text in enumerate([text0, text1, text2]): text_render = font.render(text, 1, (85, 65, 0)) rect = text_render.get_rect() if idx == 0: rect.left, rect.top = (212, y) elif idx == 1: rect.left, rect.top = (122.5, y) else: rect.left, rect.top = (126.5, y) y += 100 screen.blit(text_render, rect) pygame.display.update() game.reset() '''run''' if __name__ == '__main__': main()
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
windows server 2008 r2 標準版安裝python環(huán)境
本文主要介紹了windows server 2008 r2 標準版安裝python環(huán)境,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06python數(shù)據(jù)封裝json格式數(shù)據(jù)
本次內(nèi)容是小編在網(wǎng)上整理的關(guān)于如何python數(shù)據(jù)封裝json格式的內(nèi)容總結(jié),有興趣的讀者們參考下。2018-03-03Python異步編程之新舊協(xié)程的實現(xiàn)對比
Python中新舊協(xié)程的實現(xiàn)方式在協(xié)程發(fā)展史上有一段交集,并且舊協(xié)程基于生成器的協(xié)程語法讓生成器和協(xié)程兩個概念混淆,所以對學習者會造成一定的困擾,本文主要說明兩種協(xié)程的實現(xiàn)方式的差異,需要的可以了解下2024-01-01Python+PyQt5實現(xiàn)美劇爬蟲可視工具的方法
這篇文章主要介紹了Python+PyQt5實現(xiàn)美劇爬蟲可視工具的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04詳解基于python的全局與局部序列比對的實現(xiàn)(DNA)
這篇文章主要介紹了詳解基于python的全局與局部序列比對的實現(xiàn)(DNA).文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10