基于Python编写一个宝石消消乐小游戏
作者:九叔敲代码 时间:2021-10-25 05:46:06
开发工具
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 = 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()
上面就是一步一步来讲代码思路理清楚的讲解啦 下面我把源码放到下面:
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 = 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()
'''test'''
if __name__ == '__main__':
main()
来源:https://blog.csdn.net/weixin_46661629/article/details/122323344
标签:Python,消消乐
0
投稿
猜你喜欢
Sql server 2005 找出子表树
2008-11-24 15:23:00
浅谈python中频繁的print到底能浪费多长时间
2022-01-31 06:24:34
PyTorch加载模型model.load_state_dict()问题及解决
2022-11-08 07:03:53
关于python pycharm中输出的内容不全的解决办法
2023-09-23 16:45:26
PyTorch策略梯度算法详情
2022-12-20 14:35:12
Golang字符串常用函数的使用
2024-02-11 03:57:35
django haystack实现全文检索的示例代码
2021-04-08 05:00:59
JavaScript数学对象Math操作数字的方法
2024-04-10 10:54:34
网页的栅格系统设计
2008-09-19 21:13:00
mysql主从服务器配置特殊问题
2011-01-04 19:56:00
MySQL DISTINCT 的基本实现原理详解
2024-01-15 17:21:29
ini_set的用法介绍
2023-11-15 07:31:56
浅谈JavaScript Date日期和时间对象
2024-05-03 15:58:01
Pycharm使用爬虫时遇到etree红线问题及解决
2021-09-04 19:07:06
vue中自定义指令(directive)的基本使用方法
2024-05-28 15:46:32
python访问mysql数据库的实现方法(2则示例)
2024-01-23 05:58:41
ASP与数据库应用(给初学者)
2009-03-09 18:32:00
go语言中排序sort的使用方法示例
2023-09-01 00:07:22
Python列表操作方法详解
2021-05-17 14:45:58
自定义PyCharm快捷键的设置方式
2023-11-20 08:28:08