{"id":13634923,"url":"https://github.com/JackHCC/Elimination-Game","last_synced_at":"2025-04-19T03:34:05.085Z","repository":{"id":112237197,"uuid":"182959444","full_name":"JackHCC/Elimination-Game","owner":"JackHCC","description":"利用pygame实现消消乐小游戏GUI界面(Use pygame to eliminate the GUI interface of music game)","archived":false,"fork":false,"pushed_at":"2019-04-23T13:17:53.000Z","size":4410,"stargazers_count":20,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-09T05:33:46.750Z","etag":null,"topics":["gui","mini-project","pygame"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JackHCC.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-04-23T07:30:43.000Z","updated_at":"2024-04-26T15:13:58.000Z","dependencies_parsed_at":"2023-05-11T21:30:31.926Z","dependency_job_id":null,"html_url":"https://github.com/JackHCC/Elimination-Game","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JackHCC%2FElimination-Game","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JackHCC%2FElimination-Game/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JackHCC%2FElimination-Game/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JackHCC%2FElimination-Game/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JackHCC","download_url":"https://codeload.github.com/JackHCC/Elimination-Game/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249600500,"owners_count":21297705,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gui","mini-project","pygame"],"created_at":"2024-08-02T00:00:37.732Z","updated_at":"2025-04-19T03:34:04.569Z","avatar_url":"https://github.com/JackHCC.png","language":"Python","funding_links":[],"categories":["Uncategorized"],"sub_categories":["Uncategorized"],"readme":"# 消消乐记分小游戏GUI界面\n\n### 文件结构规划\n1. 定义config.py文件存储相关参数：包括界面的宽高，整个方格行列个数，总格数等等。\n2. 定义utils.py文件用于存放基础的类和函数：包括整个消除拼图类，游戏类，拼图块移动函数，坐标设置与获取函数，开始游戏主函数，初始化随机生成拼图函数，时间倒计时展示函数，显示得分函数，加分函数，消除函数以及消除后新拼图块生成函数，拼图交换位置函数等等。\n3. 定义main.py主函数：主要用于界面初始化开启游戏主程序\n\n### 实现方法\n1. config参数设置：\n```\nWIDTH = 600\nHEIGHT = 600\nNUMGRID = 8\nGRIDSIZE = 64\nXMARGIN = (WIDTH - GRIDSIZE * NUMGRID) // 2\nYMARGIN = (HEIGHT - GRIDSIZE * NUMGRID) // 2\nROOTDIR = os.getcwd()\nFPS = 30\n```\n2. utils类与函数设置\n\n1） 导入库:time计时，random生成随机数，pygame跨平台Python模块主要用于游戏图形化界面生成以及音频播放，config自己定义的相关变量\n```\nimport time\nimport random\nimport pygame\nfrom config import *\n```\n2） 定义拼图类\n```\nclass gemSprite(pygame.sprite.Sprite):\n```\n主要有函数：\n+ 初始化函数：self=类具体化对象本身，img_path=图片文件所在路径，size=整个界面大小，position=拼图块位置，downlen=消除拼图块后下降格数\n```\ndef __init__(self, img_path, size, position, downlen, **kwargs):\n```\n+ 移动函数：主要通过鼠标控制拼图块交换方向，有上下左右四个方向\n```\ndef move(self):\n```\n+ 获取坐标函数:获取当前拼图块坐标，主要通过左（left）和上（top）两个方向定义坐标距离\n```\ndef getPosition(self):\n```\n+ 设置坐标函数：设置拼图块坐标\n```\ndef setPosition(self, position):\n```\n3） 定义游戏类：\n```\nclass gemGame():\n```\n主要函数有：\n+ 初始化函数：self=类具体化对象本身,screen=屏幕，sounds=音频，font=字体，gem_imgs=拼图块图片\n```\ndef __init__(self, screen, sounds, font, gem_imgs, **kwargs):\n```\n+ 开始游戏函数：游戏开始主循环\n```\ndef start(self):\n```\n+ 随机拼图块生成函数：消除后随机生成新的拼图模块\n```\ndef reset(self):\n```\n+ 显示游戏倒计时时间函数：\n```\ndef showRemainingTime(self):\n```\n+ 显示得分函数：\n```\ndef drawScore(self):\n```\n+ 分数计算函数：\n```\ndef drawAddScore(self, add_score):\n```\n+ 新拼图块生成函数：\n```\ndef generateNewGems(self, res_match):\n```\n+ 消除匹配成功的拼图块：行或列三个一样的拼图块出现即消除\n```\ndef removeMatched(self, res_match):\n```\n+ 界面网格绘制：\n```\ndef drawGrids(self):\n```\n+ 画矩形框：\n```\ndef drawBlock(self, block, color=(255, 0, 255), size=4):\n```\n+ 新的拼图块出现下落特效：\n```\ndef dropGems(self, x, y):\n```\n+ 检查有无拼图块被选中：\n```\ndef checkSelected(self, position):\n```\n+ 有无行列三个拼图块相同判断：\n```\ndef isMatch(self):\n```\n+ 交换拼图函数：\n```\ndef swapGem(self, gem1_pos, gem2_pos):\n```\n3. 主函数Main配置：\n\n1） 导入库\n```\nimport os\nimport pygame\nfrom utils import *\nfrom config import *\n```\n2） 游戏主程序定义：\n```\ndef main():\n```\n主要功能：\n+ 游戏界面初始化：\n```\npygame.init()\n\tscreen = pygame.display.set_mode((WIDTH, HEIGHT))\n\tpygame.display.set_caption('消消乐')\n```\n+ 加载音效资源：\n```\n# 加载背景音乐\n\tpygame.mixer.init()\n\tpygame.mixer.music.load(os.path.join(ROOTDIR, \"resources/audios/bg.mp3\"))\n\tpygame.mixer.music.set_volume(0.6)\n\tpygame.mixer.music.play(-1)\n\t# 加载音效\n\tsounds = {}\n\tsounds['mismatch'] = pygame.mixer.Sound(os.path.join(ROOTDIR, 'resources/audios/badswap.wav'))\n\tsounds['match'] = []\n\tfor i in range(6):\n\t\tsounds['match'].append(pygame.mixer.Sound(os.path.join(ROOTDIR, 'resources/audios/match%s.wav' % i)))\n```\n+ 加载字体资源：\n```\nfont = pygame.font.Font(os.path.join(ROOTDIR, 'resources/font.TTF'), 25)\n```\n+ 加载图片资源：\n```\ngem_imgs = []\n\tfor i in range(1, 8):\n\t\tgem_imgs.append(os.path.join(ROOTDIR, 'resources/images/gem%s.png' % i))\n```\n+ 游戏主循环：\n```\ngame = gemGame(screen, sounds, font, gem_imgs)\n\twhile True:\n\t\tscore = game.start()\n\t\tflag = False\n\t\t# 一轮游戏结束后玩家选择重玩或者退出\n\t\twhile True:\n\t\t\tfor event in pygame.event.get():\n\t\t\t\tif event.type == pygame.QUIT or (event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE):\n\t\t\t\t\tpygame.quit()\n\t\t\t\t\tsys.exit()\n\t\t\t\telif event.type == pygame.KEYUP and event.key == pygame.K_r:\n\t\t\t\t\tflag = True\n\t\t\tif flag:\n\t\t\t\tbreak\n\t\t\tscreen.fill((135, 206, 235))\n\t\t\ttext0 = 'Final score: %s' % score\n\t\t\ttext1 = 'Press \u003cR\u003e to restart the game.'\n\t\t\ttext2 = 'Press \u003cEsc\u003e to quit the game.'\n\t\t\ty = 150\n\t\t\tfor idx, text in enumerate([text0, text1, text2]):\n\t\t\t\ttext_render = font.render(text, 1, (85, 65, 0))\n\t\t\t\trect = text_render.get_rect()\n\t\t\t\tif idx == 0:\n\t\t\t\t\trect.left, rect.top = (212, y)\n\t\t\t\telif idx == 1:\n\t\t\t\t\trect.left, rect.top = (122.5, y)\n\t\t\t\telse:\n\t\t\t\t\trect.left, rect.top = (126.5, y)\n\t\t\t\ty += 100\n\t\t\t\tscreen.blit(text_render, rect)\n\t\t\tpygame.display.update()\n\t\tgame.reset()\n```\n\n+ 主程序：\n```\nif __name__ == '__main__':\n\tmain()\n```\n### 运行结果图\n+ 初始化界面\n\n![1](/1.jpg)\n\n+ 消除几个后的界面\n\n![2](/2.jpg)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJackHCC%2FElimination-Game","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FJackHCC%2FElimination-Game","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJackHCC%2FElimination-Game/lists"}