python端午dragboat消消乐 美轮美奂的界面效果

Posted 程序媛一枚~

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python端午dragboat消消乐 美轮美奂的界面效果相关的知识,希望对你有一定的参考价值。

跟着大佬的步伐,消消乐俩步走~~

  1. 找素材,做消消乐图,找bg音乐🎵,成功消除🎵,失败的🎵;
  2. 上代码,调试~~

1. 效果图

原始图:
在这里插入图片描述使用数组截取得到消消乐的效果图如下,粽子图同理
在这里插入图片描述
消消乐游戏效果图gif如下~
在这里插入图片描述

2. 源码

2.1 素材准备源码(消消乐图、bg音乐)

import cv2
import imutils
import numpy as np
from imutils import paths

image = cv2.imread('images/xx2.jpg')
width = image.shape[0]
print(image.shape)
image = imutils.resize(image, width=400)
cv2.imshow("origin", image)
image = image[89:, 17:]
print(image.shape)
image = image[:70, :278]
print('--: ', image.shape)
img = imutils.resize(image, width=400)
cv2.imshow("slice", img)

print(image.shape[0], image.shape[1])
y = [int(j) for j in np.linspace(0, image.shape[1], 6)]
print(y)

for i in range(1, 6):
    cv2.imshow(str(i), imutils.resize(image[0:image.shape[0], y[i-1]:y[i]], width=200))
    cv2.imwrite("imgs/pacer"+str(i-1)+".png",imutils.resize(image[0:image.shape[0], y[i-1]:y[i]], width=80))

imagePaths = list(paths.list_images("images/"))
print('len: ', len(imagePaths))

x = [imagePath for imagePath in imagePaths if (imagePath.endswith(".jpg") and imagePath.__contains__('zz'))]
print(x)
for i, imgpath in enumerate(x):
    print(i + 5, imgpath)
    cv2.imshow(str(i + 6), imutils.resize(cv2.imread(imgpath), width=200))
    cv2.imwrite("imgs/pacer" + str(i + 5) + ".png", imutils.resize(cv2.imread(imgpath), width=80))

cv2.waitKey(0)

bg音乐及成功失败的🎵,都选自己喜欢的mp3格式就可以

2.2 消消乐源码

# USAGE
# python .\\xxl.py
'''
Function:
    消消乐
'''
import os
import sys

import cfg
import pygame
from modules.game import pacerGame

'''主程序'''


def main():
    pygame.init()
    screen = pygame.display.set_mode(cfg.SCREENSIZE)
    pygame.display.set_caption('dragonboat xxl')
    # 加载背景音乐
    pygame.mixer.init()
    pygame.mixer.music.load(os.path.join(cfg.ROOTDIR, "res/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, 'res/audios/badswap.wav'))
    sounds['mismatch'] = pygame.mixer.Sound(os.path.join(cfg.ROOTDIR, 'res/audios/badswap.mp3'))
    sounds['match'] = []
    for i in range(6):
        sounds['match'].append(pygame.mixer.Sound(os.path.join(cfg.ROOTDIR, 'res/audios/match%s.mp3' % i)))
        # sounds['match'].append(pygame.mixer.Sound(os.path.join(cfg.ROOTDIR, 'res/audios/match%s.wav' % i)))

    # 字体显示
    font = pygame.font.Font(os.path.join(cfg.ROOTDIR, 'res/font/font.TTF'), 25)
    # 星星
    pacer_imgs = []
    for i in range(1, 8):
        pacer_imgs.append(os.path.join(cfg.ROOTDIR, 'res/imgs/pacer%s.png' % i))
    # 循环
    game = pacerGame(screen, sounds, font, pacer_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((136, 207, 236))
            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 = (223, y)
                elif idx == 1:
                    rect.left, rect.top = (133.5, y)
                else:
                    rect.left, rect.top = (126.5, y)
                y += 99
                screen.blit(text_render, rect)
            pygame.display.update()
        game.reset()


'''游戏运行'''
if __name__ == '__main__':
    main()

参考

以上是关于python端午dragboat消消乐 美轮美奂的界面效果的主要内容,如果未能解决你的问题,请参考以下文章

程序媛过中秋的正确打开方式——使用Python绘制月饼消消乐,素描图,词云图,字符画图及提取轮廓

python消消乐 美轮美奂的界面效果完整源码+详细流程

python消消乐 美轮美奂的界面效果完整源码+详细流程

Python基础——用 Python 写个消消乐小游戏

Python实现消消乐小游戏

华为OD机试 - 字母消消乐(Python)| 真题+思路+代码