新闻动态

Python实现消消乐小游戏

发布日期:2022-01-01 05:15 | 文章来源:源码之家

本文实例为大家分享了Python实现消消乐小游戏的具体代码,供大家参考,具体内容如下

玩法:三个相连就能消除

源码分享:

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('Gemgem —— 九歌')
 # 加载背景音乐
 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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。

版权声明:本站文章来源标注为YINGSOO的内容版权均为本站所有,欢迎引用、转载,请保持原文完整并注明来源及原文链接。禁止复制或仿造本网站,禁止在非www.yingsoo.com所属的服务器上建立镜像,否则将依法追究法律责任。本站部分内容来源于网友推荐、互联网收集整理而来,仅供学习参考,不代表本站立场,如有内容涉嫌侵权,请联系alex-e#qq.com处理。

相关文章

实时开通

自选配置、实时开通

免备案

全球线路精选!

全天候客户服务

7x24全年不间断在线

专属顾问服务

1对1客户咨询顾问

在线
客服

在线客服:7*24小时在线

客服
热线

400-630-3752
7*24小时客服服务热线

关注
微信

关注官方微信
顶部