消消乐 py小游戏

简介: 消消乐

消消乐

玩法 三个相连就能消除

源代码如下:

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()
目录
相关文章
|
5月前
|
Python
Python跨年烟花秀
Python跨年烟花秀
149 0
|
Python
python脚本的开心消消乐
python脚本的开心消消乐
276 2
|
12月前
|
Python
python开发消消乐
python开发消消乐
103 0
|
图形学
Unity 3D游戏-消消乐(三消类)教程和源码
Unity 消消乐教程和源码 本文提供全流程,中文翻译。Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例) ...
5508 0
|
Python
python实现一个简单的消消乐游戏
实现一个简单的消消乐游戏可以是一个有趣的编程项目。下面是一个使用Python和Pygame库来创建消消乐游戏的基本步骤
448 0
|
4月前
|
Python
小游戏实战丨基于PyGame的消消乐小游戏
小游戏实战丨基于PyGame的消消乐小游戏
50 4
|
5月前
|
前端开发 Python
python制作七夕音乐贺卡
本篇博文是一个关于制作音乐贺卡的教程。自己在去年的在七夕节期间创作了一个代码项目,允许用户自定义背景、音乐和祝福语,生成一个包含音乐的HTML贺卡。教程分为三个部分:前言、制作流程和具体代码。前言提到,由于找不到现成的音乐贺卡模板,我决定自己动手,制作的贺卡适用于各种节日。制作流程包括两个步骤,一是通过提供的Python代码工具选择背景图片、音乐文件和输入祝福语,生成HTML贺卡;二是提供了一个预打包的exe文件,用户可以直接运行并按照提示操作。最后,文章分享了生成贺卡的具体Python代码,并以一句鼓励的话语结尾,强调了努力和选择的重要性。
|
Python
python小游戏——乒乓球大战2.0版本の双人联机对战功能的实现
python小游戏——乒乓球大战2.0版本の双人联机对战功能的实现
267 0
|
Python
python植物大战僵尸十二之坚果摆放
python植物大战僵尸十二之坚果摆放
74 0
|
Python
python植物大战僵尸十九之僵尸吃坚果
python植物大战僵尸十九之僵尸吃坚果
75 0
下一篇
无影云桌面