目錄
前言
開發(fā)工具
環(huán)境搭建
效果展示
選擇關(guān)卡首頁
游戲界面?
過關(guān)?
?代碼展示
模塊導入
主函數(shù)
聲音類
樹類
元素類
數(shù)組類
前言
今天主要是給大家拿牌一個小游戲,開心消消樂
看看有沒有小伙伴能夠通過呀
開發(fā)工具
Python版本:3.7.8
相關(guān)模塊:
pygame模塊;
manager模塊;
sys模塊;
以及一些python自帶的模塊。
環(huán)境搭建
安裝Python并添加到環(huán)境變量,pip安裝需要的相關(guān)模塊即可。
效果展示
選擇關(guān)卡首頁
游戲界面?
文章來源:http://www.zghlxwxcb.cn/news/detail-513643.html
過關(guān)?
文章來源地址http://www.zghlxwxcb.cn/news/detail-513643.html
?代碼展示
模塊導入
import pygame
from pygame.locals import *
import sys
import manager
主函數(shù)
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)
數(shù)組類
class Manager:
""" 數(shù)組類 """
__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] # 現(xiàn)選中[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 # 當前關(guān)卡數(shù) 初始第0關(guān)
money = 100 # 金幣
energy_num = 30 # 精力值
num_sign = True
type = 2 # 0代表游戲中; 1代表完成任務,過關(guān); -1代表步數(shù)用完,任務未完成,過關(guān)失敗; 2代表未游戲狀態(tài),板子界面
reset_mode = True # 是否重新布局(每關(guān)布局)
init_step = 15 # 每關(guān)規(guī)定步數(shù)
step = init_step # 代表游戲所剩余的步數(shù)
score = 0 # 得數(shù)
min = 20 # 分數(shù)中間值1
max = 50 # 分數(shù)中間值2
animal_num = [0, 0, 0, 0, 0, 0] # 本關(guān)消除各小動物的個數(shù)
ice_num = 0
success_board = Board(Board.success_board, [200, 0]) # 過關(guān)成功板
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)
到了這里,關(guān)于【附源碼】Python小游戲 ——開心消消樂的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!