python代码写开心消消乐

简介: python代码写开心消消乐

一.python是什么

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

二.游戏代码效果呈现

 

三.主代码

'''

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  to restart the game.'

           text2 = 'Press  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()

四.cfg


'''run'''

if __name__ == '__main__':

   main()



'''配置文件'''

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)


相关文章
|
20天前
|
算法 关系型数据库 Python
配电网中考虑需求响应(Python代码实现)【硕士论文复现】
配电网中考虑需求响应(Python代码实现)【硕士论文复现】
|
17天前
|
机器学习/深度学习 算法 安全
【PSO-LSTM】基于PSO优化LSTM网络的电力负荷预测(Python代码实现)
【PSO-LSTM】基于PSO优化LSTM网络的电力负荷预测(Python代码实现)
|
19天前
|
调度 Python
微电网两阶段鲁棒优化经济调度方法(Python代码实现)
微电网两阶段鲁棒优化经济调度方法(Python代码实现)
|
19天前
|
供应链 新能源 调度
微电网调度(风、光、储能、电网交互)(Matlab&Python代码实现)
微电网调度(风、光、储能、电网交互)(Matlab&Python代码实现)
|
20天前
|
安全 数据处理 Python
Python 函数式编程:让代码更简洁高效
Python 函数式编程:让代码更简洁高效
337 107
|
14天前
|
程序员 测试技术 开发者
Python装饰器:简化代码的强大工具
Python装饰器:简化代码的强大工具
139 92
|
18天前
|
机器学习/深度学习 数据采集 算法
【CNN-BiLSTM-attention】基于高斯混合模型聚类的风电场短期功率预测方法(Python&matlab代码实现)
【CNN-BiLSTM-attention】基于高斯混合模型聚类的风电场短期功率预测方法(Python&matlab代码实现)
|
1月前
|
程序员 数据安全/隐私保护 Python
1行Python代码,实现PDF的加密、解密
程序员晚枫分享使用python-office库实现PDF批量加密与解密的新方法。只需一行代码,即可完成单个或多个PDF文件的加密、解密操作,支持文件路径与正则筛选,适合自动化办公需求。更新至最新版,适配性更佳,操作更简单。
1行Python代码,实现PDF的加密、解密
|
15天前
|
运维 算法 新能源
基于风光储能和需求响应的微电网日前经济调度(Python代码实现)
基于风光储能和需求响应的微电网日前经济调度(Python代码实现)

热门文章

最新文章

推荐镜像

更多