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)


相关文章
|
2月前
|
存储 算法 调度
【复现】【遗传算法】考虑储能和可再生能源消纳责任制的售电公司购售电策略(Python代码实现)
【复现】【遗传算法】考虑储能和可再生能源消纳责任制的售电公司购售电策略(Python代码实现)
177 26
|
2月前
|
测试技术 开发者 Python
Python单元测试入门:3个核心断言方法,帮你快速定位代码bug
本文介绍Python单元测试基础,详解`unittest`框架中的三大核心断言方法:`assertEqual`验证值相等,`assertTrue`和`assertFalse`判断条件真假。通过实例演示其用法,帮助开发者自动化检测代码逻辑,提升测试效率与可靠性。
314 1
|
2月前
|
机器学习/深度学习 算法 调度
基于多动作深度强化学习的柔性车间调度研究(Python代码实现)
基于多动作深度强化学习的柔性车间调度研究(Python代码实现)
168 1
|
1月前
|
测试技术 Python
Python装饰器:为你的代码施展“魔法”
Python装饰器:为你的代码施展“魔法”
233 100
|
1月前
|
开发者 Python
Python列表推导式:一行代码的艺术与力量
Python列表推导式:一行代码的艺术与力量
355 95
|
2月前
|
Python
Python的简洁之道:5个让代码更优雅的技巧
Python的简洁之道:5个让代码更优雅的技巧
231 104
|
2月前
|
开发者 Python
Python神技:用列表推导式让你的代码更优雅
Python神技:用列表推导式让你的代码更优雅
428 99
|
1月前
|
缓存 Python
Python装饰器:为你的代码施展“魔法
Python装饰器:为你的代码施展“魔法
152 88
|
2月前
|
IDE 开发工具 开发者
Python类型注解:提升代码可读性与健壮性
Python类型注解:提升代码可读性与健壮性
262 102
|
1月前
|
监控 机器人 编译器
如何将python代码打包成exe文件---PyInstaller打包之神
PyInstaller可将Python程序打包为独立可执行文件,无需用户安装Python环境。它自动分析代码依赖,整合解释器、库及资源,支持一键生成exe,方便分发。使用pip安装后,通过简单命令即可完成打包,适合各类项目部署。

推荐镜像

更多
下一篇
oss云网关配置