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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
利用django創(chuàng)建一個簡易的博客網(wǎng)站的示例
這篇文章主要介紹了利用django創(chuàng)建一個簡易的博客網(wǎng)站的示例,幫助大家更好的學(xué)習(xí)和使用django框架,感興趣的朋友可以了解下2020-09-09
使用Python實現(xiàn)從麥克風(fēng)獲取音頻并識別
這篇文章主要為大家詳細(xì)介紹了如何使用Python實現(xiàn)從麥克風(fēng)獲取音頻并識別功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-02-02
基于Python實現(xiàn)Word轉(zhuǎn)HTML
將Word轉(zhuǎn)換為HTML能將文檔內(nèi)容發(fā)布在網(wǎng)頁上,這樣,用戶就可以通過瀏覽器直接查看或閱讀文檔而無需安裝特定的軟件,下面我們就來學(xué)習(xí)一下Python是如何實現(xiàn)Word轉(zhuǎn)HTML的吧2023-12-12
python?dowhy數(shù)據(jù)估計因果分析功能探索
這篇文章主要為大家介紹了python?dowhy數(shù)據(jù)估計因果分析功能實例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-02-02
淺析python打包工具distutils、setuptools
python包在開發(fā)中十分常見,一般的使用套路是所有的功能做一個python模塊包,打包模塊,然后發(fā)布,安裝使用。這篇文章給大家介紹了python打包工具distutils、setuptools的相關(guān)知識,感興趣的朋友一起看看吧2018-04-04
使用OpenCV circle函數(shù)圖像上畫圓的示例代碼
這篇文章主要介紹了使用OpenCV circle函數(shù)圖像上畫圓的示例代碼,本文內(nèi)容簡短,給大家突出重點內(nèi)容,需要的朋友可以參考下2019-12-12
Pandas使用Merge與Join和Concat分別進(jìn)行合并數(shù)據(jù)效率對比分析
這篇文章主要給大家介紹了關(guān)于pandas中DataFrame數(shù)據(jù)合并連接(merge、join、concat)的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12

