日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

过年回家啦用python写一个宝石消消乐的游戏哄小朋友

發布時間:2024/1/1 python 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 过年回家啦用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写一个宝石消消乐的游戏哄小朋友的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。