基于Python實(shí)現(xiàn)開心消消樂小游戲的示例代碼
穿過云朵升一級(jí)是要花6個(gè)金幣的,有的時(shí)候金幣真的很重要
前言
嗨嘍,大家好呀!這里是魔王~
一天晚上,天空中掉下一顆神奇的豌豆種子,正好落在了夢(mèng)之森林的村長(zhǎng)屋附近。
種子落地后吸收了池塘的水分,迅速成長(zhǎng),一夜之間變成參天大藤蔓……
第二天早上,村民們醒來后看到巨大的藤蔓都驚呆了,聚在一起議論紛紛。
有人說他似乎看到村長(zhǎng)的房子在高聳入云的藤蔓上,房子似乎還在上升,有人號(hào)召說應(yīng)該爬上去救村長(zhǎng),玩家需要破解難題,成功寫出代碼,通關(guān)游戲,救出村長(zhǎng)~
穿過云朵升一級(jí)是要花6個(gè)金幣的,有的時(shí)候金幣真的很重要
今天本博主就帶著大家自制一款開心消消樂,還原度超高哦~還在等什么動(dòng)動(dòng)手就能擁有屬于自己的”消消樂“小游戲呢,趕快學(xué)起來吧~

一、準(zhǔn)備
1.1 圖片素材


1.2 音頻素材


二、代碼
2.1 導(dǎo)入模塊
import pygame import random from pygame.locals import *
2.2 游戲音樂設(shè)置
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" # 點(diǎn)擊選中聲音
board_sound = 'sound/board.ogg' # 落板子聲音
click_button = 'sound/click_common_button.ogg' # 點(diǎn)擊按鈕聲音
money_sound = 'sound/money.ogg' # 點(diǎn)擊銀幣聲音
ice_break = 'sound/ice_break.ogg' # 冰消除聲音
def __init__(self, filename, loops=0):
self.sound = pygame.mixer.Sound(filename)
self.sound.play(loops)2.3 制作樹類
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' # 購(gòu)買精力
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]) # 果子坐標(biāo)組
energy_num_position = (15, 70) # 精力坐標(biāo)
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 # 左下角為坐標(biāo)
def draw(self, screen):
screen.blit(self.image, self.rect)
class ManagerTree:
"""管理樹類"""
__screen_size = (900, 600)
screen = pygame.display.set_mode(__screen_size, DOUBLEBUF, 32)
fruit_list = []
fruit_image = pygame.image.load(Tree.fruit).convert_alpha()
fruit_width = fruit_image.get_width()
fruit_height = fruit_image.get_height()
type = 0 # 0樹界面,1加精力界面
energy_full = False # 精力已滿標(biāo)志 初始未滿
money_empty = False # 銀幣不足
def load_text(self, text, position, txt_size=25, txt_color=(255, 255, 255)):
my_font = pygame.font.SysFont(None, txt_size)
text_screen = my_font.render(text, True, txt_color)
self.screen.blit(text_screen, position)
def draw_tree(self, energy_num, money_num):
"""畫tree"""
Tree(Tree.tree, (0, 600)).draw(self.screen) # 畫樹
Tree(Tree.energy_num, Tree.energy_num_position).draw(self.screen) # 畫精力
# print("energy", energy_num)
if energy_num > 30:
self.load_text(str(30) + '/30', (22, 55), 21)
else:
self.load_text(str(energy_num)+'/30', (22, 55), 21)
# print("money", money_num)
Tree(Tree.money, (15, 135)).draw(self.screen) # 畫銀幣
self.load_text(str(money_num), (32, 124), 21)
for i in range(0, 10): # 畫果子
Tree(Tree.fruit, Tree.position[i]).draw(self.screen)
self.load_text(str(i+1), (Tree.position[i][0]+15, Tree.position[i][1]-47))
if self.type == 1:
Tree(Tree.energy_buy, Tree.energy_buy_position).draw(self.screen)
if self.energy_full:
self.load_text("energy is full!", (430, 310), 30, (255, 0, 0))
pygame.display.flip()
pygame.time.delay(500)
self.energy_full = False
if self.money_empty:
self.load_text("money is not enough!", (410, 310), 30, (255, 0, 0))
pygame.display.flip()
pygame.time.delay(500)
self.money_empty = False
2.4 制作鼠標(biāo)點(diǎn)擊效果
def mouse_select(self, button, level, energy_num, money_num):
"""鼠標(biāo)點(diǎn)擊"""
if button.type == MOUSEBUTTONDOWN:
mouse_down_x, mouse_down_y = button.pos
print(button.pos)
if level == 0:
if self.type == 0: # 樹界面
for i in range(0, 10):
if Tree.position[i][0] < mouse_down_x < Tree.position[i][0] + self.fruit_width \
and Tree.position[i][1] - self.fruit_height < mouse_down_y < Tree.position[i][1]:
if energy_num <= 0:
self.type = 1
else:
level = i + 1
if Tree.energy_num_position[0] < mouse_down_x < Tree.energy_num_position[0]+60 \
and Tree.energy_num_position[1]-60 < mouse_down_y < Tree.energy_num_position[1]: # 精力60*60
SoundPlay(SoundPlay.click)
self.type = 1
else: # 加精力彈窗界面
if 408 < mouse_down_x < 600 and 263 < mouse_down_y < 313: # 點(diǎn)加精力按鈕
SoundPlay(SoundPlay.click_button)
if money_num < 50:
self.money_empty = True
if energy_num >= 30:
self.energy_full = True
elif energy_num < 30 and money_num >= 50:
energy_num += 5
money_num -= 50
elif 619 < mouse_down_x < 638 and 158 < mouse_down_y < 177: # 點(diǎn)叉號(hào)
self.type = 0
if button.type == MOUSEBUTTONUP:
pass
return level, energy_num, money_num2.5 制作出現(xiàn)元素
class Element(pygame.sprite.Sprite):
""" 元素類 """
# 圖標(biāo)元組,包括6個(gè)小動(dòng)物,
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") # 消除動(dòng)畫
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') # 消除冰塊動(dòng)畫
# 得分圖片
score_level = ('pic2/good.png', 'pic2/great.png', 'pic2/amazing.png', 'pic2/excellent.png', 'pic2/unbelievable.png')
none_animal = 'pic2/noneanimal.png' # 無可消除小動(dòng)物
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 # 左上角坐標(biāo)
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: # 如果左右移動(dòng)
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 Board(pygame.sprite.Sprite):
step_board = 'pic2/step.png' # 剩余步數(shù)板子
step = ('pic2/0.png', 'pic2/1.png', 'pic2/2.png', 'pic2/3.png', 'pic2/4.png', 'pic2/5.png',\
'pic2/6.png', 'pic2/7.png', 'pic2/8.png', 'pic2/9.png', )
task_board = 'pic2/task.png' # 任務(wù)板子
ok = 'pic2/ok.png' # ok勾
# 關(guān)數(shù)板子
levelBoard = ('pic2/level0.png', 'pic2/level1.png', 'pic2/level2.png', 'pic2/level3.png', 'pic2/level4.png', 'pic2/level5.png',
'pic2/level6.png', 'pic2/level7.png', 'pic2/level8.png', 'pic2/level9.png', 'pic2/level10.png')
# xxx = 'pic2/x.png' # 叉掉
test = 'pic2/test.png'
success_board = 'pic2/successtest1.png' # 過關(guān)成功板子
fail_board = 'pic2/failBoard.png' # 任務(wù)失敗
step_add = 'pic2/step_add.png' # 增加步數(shù)
next = "pic2/next.png" # 下一關(guān)按鈕
replay = "pic2/replay.png" # 重玩圖片
stars = 'pic2/startest.png' # 星星圖片
money = 'pic2/money.png' # 銀幣
energy = 'pic2/energ.png' # 精力
button_position = [[300, 465], [500, 465]]
starts_position = [[280+50, 340], [375+38, 340], [460+35, 340]]
def __init__(self, icon, position):
super().__init__()
self.image = pygame.image.load(icon).convert_alpha()
self.speed = [0, 45]
self.rect = self.image.get_rect()
self.rect.bottomleft = position # 左下角為坐標(biāo)值
def move(self):
self.rect = self.rect.move(self.speed)
if self.rect.bottom >= 543:
self.speed = [0, -45]
if self.speed == [0, -45] and self.rect.bottom <= 450:
self.speed = [0, 0]
def draw(self, screen):
screen.blit(self.image, self.rect)
2.6 數(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 # 未交換標(biāo)志
last_sel = [-1, -1] # 上一次選中[row, col]
change_value_sign = False # 是否交換值標(biāo)志,初始不交換
death_sign = True # 死圖標(biāo)志,初始不是死圖
boom_sel = [-1, -1] # 四連消特效小動(dòng)物所在位置 row,col
level = 0 # 當(dāng)前關(guān)卡數(shù) 初始第0關(guān)
money = 100 # 金幣
energy_num = 30 # 精力值
num_sign = True
type = 2 # 0代表游戲中; 1代表完成任務(wù),過關(guān); -1代表步數(shù)用完,任務(wù)未完成,過關(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 # 分?jǐn)?shù)中間值1
max = 50 # 分?jǐn)?shù)中間值2
animal_num = [0, 0, 0, 0, 0, 0] # 本關(guān)消除各小動(dòng)物的個(gè)數(shù)
ice_num = 0
success_board = Board(Board.success_board, [200, 0]) # 過關(guān)成功板
fail_board = Board(Board.fail_board, [200, 0]) # 任務(wù)失敗板
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小動(dòng)物
list_x, list_y = (__screen_size[0] - 11 * __brick_size) / 2, (__screen_size[1] - 11 * __brick_size) / 2 # 矩陣坐標(biāo)
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)
@staticmethod
def rc_xy(row, col):
"""row col 轉(zhuǎn) x,y坐標(biāo)"""
return int(Manager.list_x + (col-Manager.col)*Manager.__brick_size), int\
(Manager.list_y+(row-Manager.row)*Manager.__brick_size)
@staticmethod
def xy_rc(x, y):
"""x,y坐標(biāo)轉(zhuǎn)row col"""
return int((y-Manager.list_y)/Manager.__brick_size+Manager.row), int\
((x-Manager.list_x)/Manager.__brick_size+Manager.col)
@staticmethod
def draw_brick(x, y):
brick = Element(Element.brick, (x, y))
Manager.screen.blit(brick.image, brick.rect)
def draw_task(self, task_animal_num, which_animal, \
board_position=(400, 90), animal_position=(430, 35), txt_position=(455, 60)):
2.7 制作人物畫板
"""畫任務(wù)板子"""
txt_size = 24
txt_color = (0, 0, 0)
Board(Board.task_board, board_position).draw(self.screen)
if which_animal == 6:
task_animal = Element(Element.ice, animal_position)
else:
task_animal = Element(Element.animal[which_animal], animal_position)
task_animal.image = pygame.transform.smoothscale(task_animal.image, (40, 40))
task_animal.draw(self.screen)
if which_animal == 6:
if task_animal_num-self.ice_num <= 0:
Board(Board.ok, (txt_position[0], txt_position[1]+15)).draw(self.screen)
else:
self.load_text(str(task_animal_num-self.ice_num), txt_position, txt_size, txt_color)
else:
if task_animal_num - self.animal_num[which_animal] <= 0:
Board(Board.ok, (txt_position[0], txt_position[1]+15)).draw(self.screen)
else:
self.load_text(str(task_animal_num - self.animal_num[which_animal]), txt_position, txt_size, txt_color)
三、效果展示(僅部分)
3.1 初始頁(yè)面

3.2 第一關(guān)畫面

3.3 失敗畫面

3.4 第十關(guān)畫面

以上就是基于Python實(shí)現(xiàn)開心消消樂小游戲的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Python開心消消樂的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python單鏈表簡(jiǎn)單實(shí)現(xiàn)代碼
這篇文章主要介紹了Python單鏈表簡(jiǎn)單實(shí)現(xiàn)代碼,結(jié)合實(shí)例形式分析了Python單鏈表的具體定義與功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-04-04
使用pandas把某一列的字符值轉(zhuǎn)換為數(shù)字的實(shí)例
今天小編就為大家分享一篇使用pandas把某一列的字符值轉(zhuǎn)換為數(shù)字的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01
Python socket 套接字實(shí)現(xiàn)通信詳解
這篇文章主要介紹了Python socket 套接字實(shí)現(xiàn)通信詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
從入門到精通:玩轉(zhuǎn)Python?Fire庫(kù)
想快速打造屬于你的Python?GUI應(yīng)用嗎?拋開復(fù)雜的代碼,用Python?Fire庫(kù)就能輕松實(shí)現(xiàn)!本指南將引領(lǐng)你從零起步,駕馭Python?Fire的強(qiáng)大功能,讓編程既簡(jiǎn)單又高效,準(zhǔn)備好了嗎?讓我們開始玩轉(zhuǎn)Python?Fire,開啟你的編程冒險(xiǎn)吧!2024-02-02
總結(jié)Pyinstaller的坑及終極解決方法(小結(jié))
這篇文章主要介紹了總結(jié)Pyinstaller的坑及終極解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

