过年回家啦用python写一个宝石消消乐的游戏哄小朋友
生活随笔
收集整理的這篇文章主要介紹了
过年回家啦用python写一个宝石消消乐的游戏哄小朋友
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
開發工具
python版本:3.6.4
相關模塊:
pygame;以及一些python自帶的模塊。
環境搭建
安裝python并添加到環境變量,pip安裝需要的相關模塊即可。
原理簡介
游戲規則:
玩家通過鼠標交換相鄰的拼圖,若交換后水平/豎直方向存在連續三個相同的拼圖,則這些拼圖消失,玩家得分,同時生成新的拼圖以補充消失的部分,否則,交換失敗,玩家不得分。玩家需要在規定時間內獲取盡可能高的得分。
實現過程:
首先加載一些必要的游戲素材:
加載背景音樂:
pygame.mixer.init()pygame.mixer.music.load(os.path.join(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(ROOTDIR, 'resources/audios/badswap.wav'))sounds['match'] = []for i in range(6):sounds['match'].append(pygame.mixer.Sound(os.path.join(ROOTDIR, 'resources/audios/match%s.wav' % i)))加載字體:
font = pygame.font.Font(os.path.join(ROOTDIR, 'resources/font.TTF'), 25)圖片加載:
gem_imgs = []for i in range(1, 8):gem_imgs.append(os.path.join(ROOTDIR, 'resources/images/gem%s.png' % i))?接著我們就要設置一下游戲的主循環吧
主要循環:
game = gemGame(screen, sounds, font, gem_imgs)while True:score = game.start()flag = False?我給大家講一下原理:
邏輯其實很簡單,就是不斷檢測是否有鼠標點擊事件發生,如果有,則判斷鼠標點擊時的位置是否在某拼圖塊的位置區域內,若在,則選中該拼圖塊,否則不選中。
當有第二塊拼圖塊被選中時,則判斷兩個拼圖塊是否滿足拼圖交換的條件,若滿足,則交換拼圖塊,并獲得獎勵,否則不交換并取消選這兩個拼圖塊的選中狀態。
最后肯定就是設置游戲的結束和退出啦:
游戲倒計時結束后,進入游戲結束界面,界面顯示用戶當前得分。同時,若用戶鍵入R鍵則重新開始游戲,鍵入ESC鍵則退出游戲。
游戲結束后玩家選擇重開或退出:源碼如下
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 = Trueif flag:breakscreen.fill((135, 206, 235))text0 = 'Final score: %s' % scoretext1 = 'Press <R> to restart the game.'text2 = 'Press <Esc> to quit the game.'y = 150for 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 += 100screen.blit(text_render, rect)pygame.display.update()game.reset()?上面就是一步一步來講代碼思路理清楚的講解啦 下面我把源碼放到下面:
import os import pygame from utils import * from config import *'''游戲主程序''' def main():pygame.init()screen = pygame.display.set_mode((WIDTH, HEIGHT))pygame.display.set_caption('Gemgem-Python交流群:932574150)# 加載背景音樂pygame.mixer.init()pygame.mixer.music.load(os.path.join(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(ROOTDIR, 'resources/audios/badswap.wav'))sounds['match'] = []for i in range(6):sounds['match'].append(pygame.mixer.Sound(os.path.join(ROOTDIR, 'resources/audios/match%s.wav' % i)))# 加載字體font = pygame.font.Font(os.path.join(ROOTDIR, 'resources/font.TTF'), 25)# 圖片加載gem_imgs = []for i in range(1, 8):gem_imgs.append(os.path.join(ROOTDIR, 'resources/images/gem%s.png' % i))# 主循環game = gemGame(screen, sounds, font, gem_imgs)while True:score = game.start()flag = False# 一輪游戲結束后玩家選擇重玩或者退出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 = Trueif flag:breakscreen.fill((135, 206, 235))text0 = 'Final score: %s' % scoretext1 = 'Press <R> to restart the game.'text2 = 'Press <Esc> to quit the game.'y = 150for 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 += 100screen.blit(text_render, rect)pygame.display.update()game.reset()'''test''' if __name__ == '__main__':main()希望大家能用到,喜歡的話記得給我來個三連哦。
總結
以上是生活随笔為你收集整理的过年回家啦用python写一个宝石消消乐的游戏哄小朋友的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: osg关键内容学习
- 下一篇: websocket python爬虫_p