基于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,消消乐
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
oracle数据库导入导出命令使用方法
2023-07-22 19:06:39
python 把列表转化为字符串的方法
2023-06-14 20:35:48
CSS改变字体而不影响网页
2010-10-20 20:11:00
如何设计一个成功的网站
2007-09-07 10:33:00
使用IP地址来统计在线人数方法
2007-08-13 12:51:00
CSS3属性box-shadow图层阴影效果使用教程
2010-05-16 14:54:00
![](https://img.aspxhome.com/file/UploadPic/20105/16/01-89s.jpg)
PHP利用ChatGPT实现轻松创建用户注册页面
2023-05-25 09:22:16
discuz 2.0整合asp系统,用户添加函数
2011-04-02 11:08:00
详解ASP中断开记录集的使用方法
2008-02-13 08:35:00
泛泛而谈界面视觉效果的一致性
2010-01-05 17:05:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/0912-ui-conformity-5-22s.jpg)
PHP实现数组根据某个字段进行水平合并,横向合并案例分析
2023-10-04 04:55:53
Django+boostrap 美化admin后台的操作
2022-02-26 16:16:21
![](https://img.aspxhome.com/file/2023/3/71503_0s.jpg)
JavaScript 浮动定位提示效果实现代码第1/2页
2023-07-22 23:26:19
![](https://img.aspxhome.com/file/2023/5/55945_0s.jpg)
Sql server 2005 找出子表树
2008-11-24 15:23:00
asp error对象基础
2008-08-04 13:25:00
jQuery选择器 学习总结
2008-06-15 15:41:00
完美解决Python matplotlib绘图时汉字显示不正常的问题
2023-09-28 05:30:55
![](https://img.aspxhome.com/file/2023/1/62901_0s.jpg)
各种页面定时跳转(倒计时跳转)代码总结
2023-09-05 00:12:01
Oracle系统表外键的更名
2010-07-26 13:07:00
谷歌历年母亲节Logo一览
2009-05-11 12:17:00
![](https://img.aspxhome.com/file/UploadPic/20095/11/goog_md_aus-19s.jpg)