目录
前言
开发工具
环境搭建
效果展示
选择关卡首页
游戏界面
过关
代码展示
模块导入
主函数
声音类
树类
元素类
数组类
今天主要是给大家拿牌一个小游戏,开心消消乐
看看有没有小伙伴能够通过呀
Python版本:3.7.8
相关模块:
pygame模块;
manager模块;
sys模块;
以及一些python自带的模块。
安装Python并添加到环境变量,pip安装需要的相关模块即可。
import pygame
from pygame.locals import *
import sys
import manager
pygame.init() # 初始化
pygame.mixer.init()
pygame.display.set_caption('开心消消乐 公众号:Python日志 学习解答加群:494958217 ')
tree = manager.ManagerTree()
m = manager.Manager(0, 0)
sound_sign = 0
world_bgm = pygame.mixer.Sound(manager.SoundPlay.world_bgm)
game_bgm = pygame.mixer.Sound(manager.SoundPlay.game_bgm)
while True:
if m.level == 0:
if sound_sign == 0:
game_bgm.stop()
world_bgm.play(-1)
sound_sign = 1
else:
if sound_sign == 1:
world_bgm.stop()
game_bgm.play(-1)
sound_sign = 0
if m.level == 0:
tree.draw_tree(m.energy_num, m.money)
else:
m.set_level_mode(m.level)
sprite_group = m.draw()
if m.type == 0:
m.eliminate_animal()
m.death_map()
m.exchange(sprite_group)
m.judge_level()
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == pygame.K_q or event.key == pygame.K_ESCAPE:
exit()
if event.type == QUIT:
sys.exit()
m.level, m.energy_num, m.money = tree.mouse_select(event, m.level, m.energy_num, m.money)
m.mouse_select(event)
m.mouse_image()
pygame.display.flip()
class SoundPlay:
game_bgm = "sound/GameSceneBGM.ogg"
world_bgm = 'sound/WorldSceneBGM.ogg'
eliminate = ('sound/eliminate1.ogg', 'sound/eliminate2.ogg', 'sound/eliminate3.ogg', 'sound/eliminate4.ogg',\
'sound/eliminate5.ogg') # 消除声音
score_level = ('sound/good.ogg', 'sound/great.ogg', 'sound/amazing.ogg', 'sound/excellent.ogg',\
'sound/unbelievable.ogg') # 得分声音
click = "sound/click.bubble.ogg" # 点击选中声音
board_sound = 'sound/board.ogg' # 落板子声音
click_button = 'sound/click_common_button.ogg' # 点击按钮声音
money_sound = 'sound/money.ogg' # 点击银币声音
ice_break = 'sound/ice_break.ogg' # 冰消除声音
def __init__(self, filename, loops=0):
self.sound = pygame.mixer.Sound(filename)
self.sound.play(loops)
class Tree(pygame.sprite.Sprite):
"""树类"""
tree = 'pic2/tree.png' # 树
fruit = 'pic2/fruit.png' # 果子
energy_num = 'pic2/energy_num.png' # 精力
money = 'pic2/money.png' # 银币
energy_buy = 'pic2/energy_buy.png' # 购买精力
x, y = 340, 510
h = 90
position = ([x, y], [x+50, y-25], [x+105, y-45], [x-5, y-h-5], [x+55, y-25-h+10], [x+105, y-45-h], \
[x, y-h*2], [x+50+10, y-25-h*2-5], [x+105+25, y-45-h*2-14], [x+30, y-h*3-30]) # 果子坐标组
energy_num_position = (15, 70) # 精力坐标
energy_buy_position = (250, 400)
def __init__(self, icon, position):
super().__init__()
self.image = pygame.image.load(icon).convert_alpha()
self.rect = self.image.get_rect()
self.rect.bottomleft = position # 左下角为坐标
def draw(self, screen):
screen.blit(self.image, self.rect)
class Element(pygame.sprite.Sprite):
""" 元素类 """
# 图标元组,包括6个小动物,
animal = ('pic2/fox.png', 'pic2/bear.png', 'pic2/chick.png', 'pic2/eagle.png', 'pic2/frog.png', 'pic2/cow.png')
ice = 'pic2/ice.png' # 冰层
brick = 'pic2/brick.png' # 砖
frame = 'pic2/frame.png' # 选中框
bling = ("pic2/bling1.png", "pic2/bling2.png", "pic2/bling3.png", "pic2/bling4.png", "pic2/bling5.png",\
"pic2/bling6.png", "pic2/bling7.png", "pic2/bling8.png", "pic2/bling9.png") # 消除动画
ice_eli = ('pic2/ice0.png', 'pic2/ice1.png', 'pic2/ice2.png', 'pic2/ice3.png', 'pic2/ice4.png', 'pic2/ice5.png',\
'pic2/ice6.png', 'pic2/ice7.png', 'pic2/ice8.png') # 消除冰块动画
# 得分图片
score_level = ('pic2/good.png', 'pic2/great.png', 'pic2/amazing.png', 'pic2/excellent.png', 'pic2/unbelievable.png')
none_animal = 'pic2/noneanimal.png' # 无可消除小动物
stop = 'pic2/exit.png' # 暂停键
stop_position = (20, 530)
def __init__(self, icon, position):
super().__init__()
self.image = pygame.image.load(icon).convert_alpha()
self.rect = self.image.get_rect()
self.rect.topleft = position # 左上角坐标
self.speed = [0, 0]
self.init_position = position
def move(self, speed):
self.speed = speed
self.rect = self.rect.move(self.speed)
if self.speed[0] != 0: # 如果左右移动
if abs(self.rect.left-self.init_position[0]) == self.rect[2]:
self.init_position = self.rect.topleft
self.speed = [0, 0]
else:
if abs(self.rect.top-self.init_position[1]) == self.rect[3]:
self.init_position = self.rect.topleft
self.speed = [0, 0]
def draw(self, screen):
screen.blit(self.image, self.rect)
class Manager:
""" 数组类 """
__screen_size = (900, 600)
screen = pygame.display.set_mode(__screen_size, DOUBLEBUF, 32)
__brick_size = 50
__bg = pygame.image.load('pic2/bg.png').convert()
stop_width = 63
selected = [-1, -1] # 现选中[row, col]
exchange_sign = -1 # 未交换标志
last_sel = [-1, -1] # 上一次选中[row, col]
change_value_sign = False # 是否交换值标志,初始不交换
death_sign = True # 死图标志,初始不是死图
boom_sel = [-1, -1] # 四连消特效小动物所在位置 row,col
level = 0 # 当前关卡数 初始第0关
money = 100 # 金币
energy_num = 30 # 精力值
num_sign = True
type = 2 # 0代表游戏中; 1代表完成任务,过关; -1代表步数用完,任务未完成,过关失败; 2代表未游戏状态,板子界面
reset_mode = True # 是否重新布局(每关布局)
init_step = 15 # 每关规定步数
step = init_step # 代表游戏所剩余的步数
score = 0 # 得数
min = 20 # 分数中间值1
max = 50 # 分数中间值2
animal_num = [0, 0, 0, 0, 0, 0] # 本关消除各小动物的个数
ice_num = 0
success_board = Board(Board.success_board, [200, 0]) # 过关成功板
fail_board = Board(Board.fail_board, [200, 0]) # 任务失败板
height, width = 9, 9
row, col = 5, 5
ice_list = [[-1 for col in range(21)]for row in range(21)] # -1不画,1画冰
animal = [[-1 for col in range(21)]for row in range(21)] # -2消除的,-1不画,0-4小动物
list_x, list_y = (__screen_size[0] - 11 * __brick_size) / 2, (__screen_size[1] - 11 * __brick_size) / 2 # 矩阵坐标
def __init__(self, width, height):
self.height = height
self.width = width
self.list_x = (Manager.__screen_size[0] - self.width * Manager.__brick_size) / 2
self.list_y = (Manager.__screen_size[1] - self.height * Manager.__brick_size) / 2
self.row, self.col = Manager.xy_rc(self.list_x, self.list_y)
self.list_x, self.list_y = Manager.rc_xy(self.row, self.col)
self.ice_list = [[-1 for col in range(21)]for row in range(21)]
self.animal = [[-1 for col in range(21)]for row in range(21)]
self.reset_animal()
def reset_animal(self):
for row in range(self.row, self.row + self.height):
for col in range(self.col, self.col + self.width):
self.animal[row][col] = random.randint(0, 5)