Python實現(xiàn)消消樂小游戲
提到開心消消樂這款小游戲,相信大家都不陌生,其曾在 2015 年獲得過玩家最喜愛的移動單機游戲獎,受歡迎程度可見一斑,本文我們使用 Python 來做個簡單的消消樂小游戲。
實現(xiàn)
消消樂的構(gòu)成主要包括三部分:游戲主體、計分器、計時器,下面來看一下具體實現(xiàn)。
先來看一下游戲所需 Python 庫。
import os import sys import time import pygame import random
定義一些常量,比如:窗口寬高、網(wǎng)格行列數(shù)等,代碼如下:
WIDTH = 400 HEIGHT = 400 NUMGRID = 8 GRIDSIZE = 36 XMARGIN = (WIDTH - GRIDSIZE * NUMGRID) // 2 YMARGIN = (HEIGHT - GRIDSIZE * NUMGRID) // 2 ROOTDIR = os.getcwd() FPS = 30
接著創(chuàng)建一個主窗口,代碼如下:
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('消消樂')
看一下效果:

再接著在窗口中畫一個 8 x 8 的網(wǎng)格,代碼如下:
screen.fill((255, 255, 220)) # 游戲界面的網(wǎng)格繪制 def drawGrids(self): for x in range(NUMGRID): for y in range(NUMGRID): rect = pygame.Rect((XMARGIN+x*GRIDSIZE, YMARGIN+y*GRIDSIZE, GRIDSIZE, GRIDSIZE)) self.drawBlock(rect, color=(255, 165, 0), size=1 # 畫矩形 block 框 def drawBlock(self, block, color=(255, 0, 0), size=2): pygame.draw.rect(self.screen, color, block, size)
看一下效果:

再接著在網(wǎng)格中隨機放入各種拼圖塊,代碼如下:
while True: self.all_gems = [] self.gems_group = pygame.sprite.Group() for x in range(NUMGRID): self.all_gems.append([]) for y in range(NUMGRID): gem = Puzzle(img_path=random.choice(self.gem_imgs), size=(GRIDSIZE, GRIDSIZE), position=[XMARGIN+x*GRIDSIZE, YMARGIN+y*GRIDSIZE-NUMGRID*GRIDSIZE], downlen=NUMGRID*GRIDSIZE) self.all_gems[x].append(gem) self.gems_group.add(gem) if self.isMatch()[0] == 0: break
看一下效果:

再接著加入計分器和計時器,代碼如下:
# 顯示得分
def drawScore(self):
score_render = self.font.render('分數(shù):'+str(self.score), 1, (85, 65, 0))
rect = score_render.get_rect()
rect.left, rect.top = (55, 15)
self.screen.blit(score_render, rect)
# 顯示加分
def drawAddScore(self, add_score):
score_render = self.font.render('+'+str(add_score), 1, (255, 100, 100))
rect = score_render.get_rect()
rect.left, rect.top = (250, 250)
self.screen.blit(score_render, rect)
# 顯示剩余時間
def showRemainingTime(self):
remaining_time_render = self.font.render('倒計時: %ss' % str(self.remaining_time), 1, (85, 65, 0))
rect = remaining_time_render.get_rect()
rect.left, rect.top = (WIDTH-190, 15)
self.screen.blit(remaining_time_render, rect)
看一下效果:

當設(shè)置的游戲時間用盡時,我們可以生成一些提示信息,代碼如下:
while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYUP and event.key == pygame.K_r: flag = True if flag: break screen.fill((255, 255, 220)) text0 = '最終得分: %s' % score text1 = '按 R 鍵重新開始' y = 140 for idx, text in enumerate([text0, text1]): text_render = font.render(text, 1, (85, 65, 0)) rect = text_render.get_rect() if idx == 0: rect.left, rect.top = (100, y) elif idx == 1: rect.left, rect.top = (100, y) y += 60 screen.blit(text_render, rect) pygame.display.update()
看一下效果:

說完了游戲圖形化界面相關(guān)的部分,我們再看一下游戲的主要處理邏輯。
我們通過鼠標來操縱拼圖塊,因此程序需要檢查有無拼圖塊被選中,代碼實現(xiàn)如下:
def checkSelected(self, position): for x in range(NUMGRID): for y in range(NUMGRID): if self.getGemByPos(x, y).rect.collidepoint(*position): return [x, y] return None
我們需要將鼠標連續(xù)選擇的拼圖塊進行位置交換,代碼實現(xiàn)如下:
def swapGem(self, gem1_pos, gem2_pos): margin = gem1_pos[0] - gem2_pos[0] + gem1_pos[1] - gem2_pos[1] if abs(margin) != 1: return False gem1 = self.getGemByPos(*gem1_pos) gem2 = self.getGemByPos(*gem2_pos) if gem1_pos[0] - gem2_pos[0] == 1: gem1.direction = 'left' gem2.direction = 'right' elif gem1_pos[0] - gem2_pos[0] == -1: gem2.direction = 'left' gem1.direction = 'right' elif gem1_pos[1] - gem2_pos[1] == 1: gem1.direction = 'up' gem2.direction = 'down' elif gem1_pos[1] - gem2_pos[1] == -1: gem2.direction = 'up' gem1.direction = 'down' gem1.target_x = gem2.rect.left gem1.target_y = gem2.rect.top gem1.fixed = False gem2.target_x = gem1.rect.left gem2.target_y = gem1.rect.top gem2.fixed = False self.all_gems[gem2_pos[0]][gem2_pos[1]] = gem1 self.all_gems[gem1_pos[0]][gem1_pos[1]] = gem2 return True
每一次交換拼圖塊時,我們需要判斷是否有連續(xù)一樣的三個及以上拼圖塊,代碼實現(xiàn)如下:
def isMatch(self): for x in range(NUMGRID): for y in range(NUMGRID): if x + 2 < NUMGRID: if self.getGemByPos(x, y).type == self.getGemByPos(x+1, y).type == self.getGemByPos(x+2, y).type: return [1, x, y] if y + 2 < NUMGRID: if self.getGemByPos(x, y).type == self.getGemByPos(x, y+1).type == self.getGemByPos(x, y+2).type: return [2, x, y] return [0, x, y]
當出現(xiàn)三個及以上拼圖塊時,需要將這些拼圖塊消除,代碼實現(xiàn)如下:
def removeMatched(self, res_match): if res_match[0] > 0: self.generateNewGems(res_match) self.score += self.reward return self.reward return 0
將匹配的拼圖塊消除之后,我們還需要隨機生成新的拼圖塊,代碼實現(xiàn)如下:
def generateNewGems(self, res_match): if res_match[0] == 1: start = res_match[2] while start > -2: for each in [res_match[1], res_match[1]+1, res_match[1]+2]: gem = self.getGemByPos(*[each, start]) if start == res_match[2]: self.gems_group.remove(gem) self.all_gems[each][start] = None elif start >= 0: gem.target_y += GRIDSIZE gem.fixed = False gem.direction = 'down' self.all_gems[each][start+1] = gem else: gem = Puzzle(img_path=random.choice(self.gem_imgs), size=(GRIDSIZE, GRIDSIZE), position=[XMARGIN+each*GRIDSIZE, YMARGIN-GRIDSIZE], downlen=GRIDSIZE) self.gems_group.add(gem) self.all_gems[each][start+1] = gem start -= 1 elif res_match[0] == 2: start = res_match[2] while start > -4: if start == res_match[2]: for each in range(0, 3): gem = self.getGemByPos(*[res_match[1], start+each]) self.gems_group.remove(gem) self.all_gems[res_match[1]][start+each] = None elif start >= 0: gem = self.getGemByPos(*[res_match[1], start]) gem.target_y += GRIDSIZE * 3 gem.fixed = False gem.direction = 'down' self.all_gems[res_match[1]][start+3] = gem else: gem = Puzzle(img_path=random.choice(self.gem_imgs), size=(GRIDSIZE, GRIDSIZE), position=[XMARGIN+res_match[1]*GRIDSIZE, YMARGIN+start*GRIDSIZE], downlen=GRIDSIZE*3) self.gems_group.add(gem) self.all_gems[res_match[1]][start+3] = gem start -= 1
之后反復(fù)執(zhí)行這個過程,直至耗盡游戲時間,游戲結(jié)束。
最后,我們動態(tài)看一下游戲效果。

總結(jié)
本文我們使用 Python 實現(xiàn)了一個簡單的消消樂游戲,有興趣的可以對游戲做進一步擴展,比如增加關(guān)卡等。
到此這篇關(guān)于Python實現(xiàn)消消樂小游戲的文章就介紹到這了,更多相關(guān)Python 消消樂內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于python線程池的四種實現(xiàn)方式
這篇文章主要介紹了關(guān)于python線程池的四種實現(xiàn)方式,一個程序運行起來后,一定有一個執(zhí)行代碼的東西,這個東西就是線程,需要的朋友可以參考下2023-04-04
Python 根據(jù)日志級別打印不同顏色的日志的方法示例
這篇文章主要介紹了Python 根據(jù)日志級別打印不同顏色的日志的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08
Python?pandas?DataFrame基礎(chǔ)運算及空值填充詳解
pandas除了可以drop含有空值的數(shù)據(jù)之外,當然也可以用來填充空值,下面這篇文章主要給大家介紹了關(guān)于Python?pandas?DataFrame基礎(chǔ)運算及空值填充的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-07-07
Python+OpenCV實現(xiàn)相機標定的方法詳解
opencv中內(nèi)置了張正友的棋盤格標定法,通過一些姿態(tài)各異的棋盤格圖像,可以標定相機的內(nèi)外參數(shù),本文為大家介紹OpenCV進行相機標定的具體方法,希望對大家有所幫助2023-05-05
使用Pytorch訓練two-head網(wǎng)絡(luò)的操作
這篇文章主要介紹了使用Pytorch訓練two-head網(wǎng)絡(luò)的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
python實現(xiàn)apahce網(wǎng)站日志分析示例
這篇文章主要介紹了python實現(xiàn)apahce網(wǎng)站日志分析示例,需要的朋友可以參考下2014-04-04

