Python小游戏(消消乐)
Python 小游戏
2023-09-14 09:04:58 时间
玩法:三个相连就能消除
源码分享:
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))
# 主循环
game = gemGame(screen, sounds, font, gem_imgs, cfg)
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()
'''run'''
if __name__ == '__main__':
main()
祝大家玩得开心,欢迎点赞、关注、评论,欢迎一健三联。
相关文章
- Python 常用的标准库以及第三方库有哪些?
- LRU算法的Python实现
- python: 安装DeOldify库:黑白图片上色(Python 3.7.15)
- 30个Python小游戏,小白练手,我都能玩一天【内附源码】
- Python小游戏(打乒乓)
- Python小游戏(滑雪)
- Python小游戏(贪吃蛇)
- Python小游戏(俄罗斯方块)
- Python小游戏(打地鼠)
- Python小游戏(24点小游戏)
- Python小游戏(小蜜蜂)
- python小游戏
- 【路径规划】基于A*算法和Dijkstra算法的路径规划(Python代码实现)
- 用Python制造雪景图,体验 “ 人工下雪 ” 得快乐~
- 【python制作小游戏】大鼻子马里奥等你来挑战,还原度超高哦~
- 【python制作小游戏】能用工具尽量别耗时间,速战速决,见识新的天空——开心消消乐
- 用python写一个猜年龄的益智小游戏,适合初学者学习
- python executemany
- python中h5文件制作与读取
- 提高你的Python: 解释‘yield’和‘Generators(生成器)’
- Python—I/O多路复用
- Python数模笔记-NetworkX(3)条件最短路径
- 炸弹人小游戏代码开源(python)
- python小游戏——飞机大战代码开源
- python小游戏——塔防小游戏代码开源
- python小游戏——怀念经典坦克大战代码
- python小游戏————兔子
- 大揭秘!Python处理办公自动化的10大场景!
- python制作贪吃蛇小游戏,畅玩无限制
- python的pygame写贪吃蛇小游戏
- Python的学习心得和知识总结(十)|Python小游戏开发(2048-B站大佬版)
- Python定义函数及引用