水果消消樂 - 困難模式
以下是一個基于Python和Pygame的水果消消樂游戲實現,包含困難模式的特點:
import pygame
import random
import sys
from pygame.locals import *# 初始化
pygame.init()
pygame.mixer.init()# 游戲常量
FPS = 60
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
GRID_SIZE = 8
CELL_SIZE = 60
GRID_OFFSET_X = (WINDOW_WIDTH - GRID_SIZE * CELL_SIZE) // 2
GRID_OFFSET_Y = (WINDOW_HEIGHT - GRID_SIZE * CELL_SIZE) // 2 + 20# 顏色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BACKGROUND_COLOR = (230, 230, 250)
GRID_COLOR = (200, 200, 220)# 水果類型
FRUITS = ['apple', 'banana', 'orange', 'pear', 'watermelon', 'strawberry', 'grape']
FRUIT_COLORS = {'apple': (255, 50, 50),'banana': (255, 255, 100),'orange': (255, 165, 0),'pear': (150, 255, 150),'watermelon': (100, 200, 100),'strawberry': (255, 100, 150),'grape': (150, 50, 200)
}# 困難模式設置
TIME_LIMIT = 90 # 90秒時間限制
MOVE_LIMIT = 25 # 25步限制
TARGET_SCORE = 2000 # 目標分數class Fruit:def __init__(self, x, y, type):self.x = xself.y = yself.type = typeself.color = FRUIT_COLORS[type]self.selected = Falseself.scale = 1.0self.scale_direction = 1def draw(self, surface):rect = pygame.Rect(GRID_OFFSET_X + self.x * CELL_SIZE + 5,GRID_OFFSET_Y + self.y * CELL_SIZE + 5,CELL_SIZE - 10,CELL_SIZE - 10)if self.selected:pygame.draw.rect(surface, WHITE, rect, 3)# 繪制水果(簡化版,實際游戲可以用圖片)pygame.draw.ellipse(surface, self.color, rect)# 水果動畫效果if self.scale < 0.95 or self.scale > 1.05:self.scale_direction *= -1self.scale += 0.005 * self.scale_directionclass Game:def __init__(self):self.clock = pygame.time.Clock()self.screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))pygame.display.set_caption('水果消消樂 - 困難模式')self.font = pygame.font.SysFont('Arial', 24)self.big_font = pygame.font.SysFont('Arial', 48)self.grid = [[None for _ in range(GRID_SIZE)] for _ in range(GRID_SIZE)]self.selected = Noneself.score = 0self.time_left = TIME_LIMITself.moves_left = MOVE_LIMITself.game_over = Falseself.win = Falseself.last_time = pygame.time.get_ticks()# 初始化網格self.initialize_grid()def initialize_grid(self):# 確保初始網格沒有匹配項while True:for y in range(GRID_SIZE):for x in range(GRID_SIZE):self.grid[y][x] = Fruit(x, y, random.choice(FRUITS))if not self.check_matches(False):breakdef draw(self):self.screen.fill(BACKGROUND_COLOR)# 繪制網格背景for y in range(GRID_SIZE):for x in range(GRID_SIZE):rect = pygame.Rect(GRID_OFFSET_X + x * CELL_SIZE,GRID_OFFSET_Y + y * CELL_SIZE,CELL_SIZE,CELL_SIZE)pygame.draw.rect(self.screen, GRID_COLOR, rect, 1)# 繪制水果for row in self.grid:for fruit in row:if fruit:fruit.draw(self.screen)# 繪制游戲信息score_text = self.font.render(f'分數: {self.score}', True, BLACK)time_text = self.font.render(f'時間: {int(self.time_left)}秒', True, BLACK)moves_text = self.font.render(f'剩余步數: {self.moves_left}', True, BLACK)target_text = self.font.render(f'目標: {TARGET_SCORE}', True, BLACK)self.screen.blit(score_text, (20, 20))self.screen.blit(time_text, (20, 50))self.screen.blit(moves_text, (20, 80))self.screen.blit(target_text, (20, 110))# 游戲結束提示if self.game_over:if self.win:text = self.big_font.render('恭喜獲勝!', True, (0, 200, 0))else:text = self.big_font.render('游戲結束!', True, (200, 0, 0))text_rect = text.get_rect(center=(WINDOW_WIDTH//2, WINDOW_HEIGHT//2))self.screen.blit(text, text_rect)restart_text = self.font.render('按R鍵重新開始', True, BLACK)restart_rect = restart_text.get_rect(center=(WINDOW_WIDTH//2, WINDOW_HEIGHT//2 + 50))self.screen.blit(restart_text, restart_rect)pygame.display.flip()def handle_events(self):for event in pygame.event.get():if event.type == QUIT:pygame.quit()sys.exit()if event.type == KEYDOWN:if event.key == K_r and self.game_over:self.__init__() # 重置游戲if not self.game_over and event.type == MOUSEBUTTONDOWN:x, y = event.posgrid_x = (x - GRID_OFFSET_X) // CELL_SIZEgrid_y = (y - GRID_OFFSET_Y) // CELL_SIZEif 0 <= grid_x < GRID_SIZE and 0 <= grid_y < GRID_SIZE:if self.selected is None:self.selected = (grid_x, grid_y)self.grid[grid_y][grid_x].selected = Trueelse:prev_x, prev_y = self.selected# 檢查是否是相鄰的格子if ((abs(grid_x - prev_x) == 1 and grid_y == prev_y) or (abs(grid_y - prev_y) == 1 and grid_x == prev_x)):# 交換水果self.swap_fruits(prev_x, prev_y, grid_x, grid_y)# 檢查是否有匹配matches = self.check_matches()if not matches:# 如果沒有匹配,交換回來self.swap_fruits(prev_x, prev_y, grid_x, grid_y)else:# 減少步數self.moves_left -= 1# 取消選擇self.grid[prev_y][prev_x].selected = Falseself.selected = Nonedef swap_fruits(self, x1, y1, x2, y2):self.grid[y1][x1].x, self.grid[y2][x2].x = self.grid[y2][x2].x, self.grid[y1][x1].xself.grid[y1][x1].y, self.grid[y2][x2].y = self.grid[y2][x2].y, self.grid[y1][x1].yself.grid[y1][x1], self.grid[y2][x2] = self.grid[y2][x2], self.grid[y1][x1]def check_matches(self, remove=True):matches = []# 檢查水平匹配for y in range(GRID_SIZE):for x in range(GRID_SIZE - 2):if (self.grid[y][x] and self.grid[y][x+1] and self.grid[y][x+2] andself.grid[y][x].type == self.grid[y][x+1].type == self.grid[y][x+2].type):match = [(x, y), (x+1, y), (x+2, y)]# 檢查更長的匹配for i in range(x+3, GRID_SIZE):if self.grid[y][i] and self.grid[y][i].type == self.grid[y][x].type:match.append((i, y))else:breakmatches.append(match)# 檢查垂直匹配for x in range(GRID_SIZE):for y in range(GRID_SIZE - 2):if (self.grid[y][x] and self.grid[y+1][x] and self.grid[y+2][x] andself.grid[y][x].type == self.grid[y+1][x].type == self.grid[y+2][x].type):match = [(x, y), (x, y+1), (x, y+2)]# 檢查更長的匹配for i in range(y+3, GRID_SIZE):if self.grid[i][x] and self.grid[i][x].type == self.grid[y][x].type:match.append((x, i))else:breakmatches.append(match)if remove and matches:# 移除匹配的水果并計算分數removed = set()for match in matches:for x, y in match:if (x, y) not in removed:removed.add((x, y))self.score += 10 * len(match) # 匹配越長分數越高# 移除水果并填充新水果for x, y in sorted(removed, key=lambda pos: pos[1]):# 從上方掉落水果for dy in range(y, 0, -1):self.grid[dy][x] = self.grid[dy-1][x]if self.grid[dy][x]:self.grid[dy][x].y = dy# 頂部添加新水果self.grid[0][x] = Fruit(x, 0, random.choice(FRUITS))# 遞歸檢查新的匹配self.check_matches()return len(matches) > 0def update(self):if self.game_over:return# 更新時間current_time = pygame.time.get_ticks()if current_time - self.last_time > 1000: # 每秒更新一次self.time_left -= 1self.last_time = current_time# 檢查游戲結束條件if self.time_left <= 0 or self.moves_left <= 0:self.game_over = Trueself.win = self.score >= TARGET_SCOREif self.score >= TARGET_SCORE:self.game_over = Trueself.win = Truedef run(self):while True:self.handle_events()self.update()self.draw()self.clock.tick(FPS)if __name__ == '__main__':game = Game()game.run()
困難模式特點
-
時間限制:玩家只有90秒時間來完成游戲
-
步數限制:最多只能進行25步操作
-
高目標分數:需要達到2000分才能獲勝
-
連鎖反應:消除水果后會自動檢查新的匹配,增加了策略性
-
懲罰機制:無效的交換不會消耗步數,但有效的交換會減少剩余步數
如何運行
-
確保安裝了Python和Pygame庫(
pip install pygame
) -
復制上面的代碼到一個.py文件中
-
運行該文件
游戲操作說明
-
點擊一個水果選中它(會顯示白色邊框)
-
點擊相鄰的水果進行交換
-
如果交換后形成3個或更多相同水果的直線,它們會被消除并獲得分數
-
無效的交換會自動回退
-
游戲目標是在限時和限步數內達到目標分數