python代码写开心消消乐

♥️作者:小刘在C站

♥️个人主页:小刘主页

♥️每天分享云计算网络运维课堂笔记,努力不一定有收获,但一定会有收获加油!一起努力,共赴美好人生!

♥️夕阳下,是最美的绽放,树高千尺,落叶归根人生不易,人间真情

目录


一.python是什么

Python由荷兰数学计算机科学研究学会的吉多·范罗苏姆于1990年代初设计,作为一门叫做ABC语言的替代品。 [1]  Python提供了高效的高级数据结构,还能简单有效地面向对象编程。Python语法和动态类型,以及解释型语言的本质,使它成为多数平台上写脚本和快速开发应用的编程语言, [2]  随着版本的不断更新和语言新功能的添加,逐渐被用于独立的、大型项目的开发。

二.游戏代码效果呈现

python代码写开心消消乐

 

三.主代码

'''
Function:
    消消乐小游戏
源码基地:#959755565#
'''
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('开心消消乐🎃')
    # 加载背景音乐
    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()

四.cfg

'''配置文件'''
import os


'''屏幕大小'''
SCREENSIZE = (600, 600)
'''游戏元素尺寸'''
NUMGRID = 8
GRIDSIZE = 64
XMARGIN = (SCREENSIZE[0] – GRIDSIZE * NUMGRID) // 2
YMARGIN = (SCREENSIZE[1] – GRIDSIZE * NUMGRID) // 2
'''根目录'''
ROOTDIR = os.getcwd()
'''FPS'''
FPS = 30

五.README

# Introduction
https://mp.weixin.qq.com/s/H0dFwoEcJT-JPKfNvPt2Kw

# Environment
“`
OS: Windows10
Python: Python3.5+(have installed necessary dependencies)
“`

# Usage
“`
Step1:
pip install -r requirements.txt
Step2:
run "python Game15.py"
“`

# Reference
http://inventwithpython.com/pygame

# Game Display
![giphy](demonstration/running.gif)

♥️关注,就是我创作的动力

♥️点赞,就是对我最大的认可

♥️这里是小刘,励志用心做好每一篇文章,谢谢大家

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2023年3月5日 下午4:12
下一篇 2023年3月5日 下午4:13

相关推荐