python圣诞节简单寻宝小游戏
叮咚~𖠋你的圣诞小游戏已经送达♪ ♪ ♫
𝗠𝙚𝗿𝗿𝘆 𝗖𝗵𝗿𝗶𝘀𝘁𝗺𝗮𝘀❄️🎁🌲+✨=🎄🎅🎀🦌
一个 Pygame 库构建的简单寻宝游戏。
0. 实现效果
每关敲出苹果后自动刷新进入下一关,直至锤子用完,锤子用完游戏结束。
找到苹果 | 锤子用光 |
---|---|
1. 环境配置
要运行游戏,您需要在系统上安装 Python 和 Pygame。以下是设置环境的步骤:
- 安装 Python:确保您已安装 Python 3.x。您可以从 Python 官方网站下载它。
- 安装 Pygame:使用 pip 安装 Pygame 库。打开终端或命令提示符并运行:
pip install pygame
- 素材准备:游戏需要 ITEMS 列表中的键所表示的项目的图像。确保将名为 a.jpg、w.jpg、t.jpg、s.jpg、f.jpg 和 g.jpg 的映像与 Python 脚本位于同一目录中。
2. 游戏简介
设计简单,主要目标是在网格上找到隐藏的项目,同时管理有限次数的尝试,点击网格单元格以显示物品,每次点击会消耗锤子。9×9 的格子,初始时拥有10个锤子,可以用一个锤子敲开一个格子,格子里面可能有苹果🍎,西瓜🍉,圣诞树🌳,星星⭐,花🌸和 雪花❄️,设置不同的积分规则,敲开的物体不同,获得的积分不同,且当敲开苹果时,获得锤子+2。
3. 代码实现
常量和初始化
# Constants
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 640
GRID_SIZE = 9
CELL_SIZE = SCREEN_WIDTH // GRID_SIZE
ITEMS = ['a', 'w', 't', 's', 'f', 'g'] # Item keys for apples, watermelons, etc.
SCORES = {'a': 5, 'w': 3, 't': 2, 's': 4, 'f': 1, 'g': 0}
RESET_SCORE = 10
# Colors
BACKGROUND_COLOR = (135, 206, 250) # Light sky blue
CELL_COLOR_HIDDEN = (220, 220, 220) # Light gray for hidden cells
CELL_COLOR_REVEALED = (255, 255, 255) # White for revealed cells
BORDER_COLOR = (169, 169, 169) # Dark gray for borders
BLACK = (0, 0, 0)
SUCCESS_COLOR = (0, 128, 0)
GAME_OVER_COLOR = (255, 0, 0)
# Pygame initialization
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Christmas Treasure Chest Game")
图片加载
游戏结束/下一关
if game.apples_found >= 1:
message = "Found an Apple! Next level...."
color = SUCCESS_COLOR
else:
message = "Game over! No hammers!"
color = GAME_OVER_COLOR
message_surface = large_font.render(message, True, color)
screen.blit(message_surface, (SCREEN_WIDTH // 2 - 250, SCREEN_HEIGHT // 2 - 20))
pygame.display.flip()
pygame.time.wait(2000)
if game.apples_found >= 1:
game.reset()
4. 完整代码
import pygame
import random
import sys
# Constants
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 640
GRID_SIZE = 9
CELL_SIZE = SCREEN_WIDTH // GRID_SIZE
ITEMS = ['a', 'w', 't', 's', 'f', 'g'] # Item keys for apples, watermelons, etc.
SCORES = {'a': 5, 'w': 3, 't': 2, 's': 4, 'f': 1, 'g': 0}
RESET_SCORE = 10
# Colors
BACKGROUND_COLOR = (135, 206, 250) # Light sky blue
CELL_COLOR_HIDDEN = (220, 220, 220) # Light gray for hidden cells
CELL_COLOR_REVEALED = (255, 255, 255) # White for revealed cells
BORDER_COLOR = (169, 169, 169) # Dark gray for borders
BLACK = (0, 0, 0)
SUCCESS_COLOR = (0, 128, 0)
GAME_OVER_COLOR = (255, 0, 0)
# Pygame initialization
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Christmas Treasure Chest Game")
# Load images and fonts
item_images = {item: pygame.transform.scale(pygame.image.load(f'{item}.jpg').convert_alpha(),
(int(CELL_SIZE * 0.9), int(CELL_SIZE * 0.9))) for item in ITEMS}
font = pygame.font.Font(pygame.font.get_default_font(), 30) # Use a default font of size 30
large_font = pygame.font.Font(pygame.font.get_default_font(), 20) # Larger size for messages
class Game:
def __init__(self):
self.score = self.hammers = 10
self.current_level = 0
self.apples_found = 0
self.revealed = [[False] * GRID_SIZE for _ in range(GRID_SIZE)]
self.board = self.generate_board()
def generate_board(self):
board = [[random.choice(ITEMS) for _ in range(GRID_SIZE)] for _ in range(GRID_SIZE)]
if self.current_level == 0: # First level, always add an apple
x, y = random.randint(0, GRID_SIZE - 1), random.randint(0, GRID_SIZE - 1)
board[x][y] = 'a' # Place an apple
return board
def reset(self):
self.score += RESET_SCORE
self.hammers += 5 # Refresh plus 5 hammers
self.current_level += 1
self.apples_found = 0
self.board = self.generate_board()
self.revealed = [[False] * GRID_SIZE for _ in range(GRID_SIZE)]
def reveal_item(self, x, y):
if not self.revealed[x][y]:
self.revealed[x][y] = True
self.hammers -= 1
item = self.board[x][y]
if item == 'a':
self.hammers += 2
self.apples_found += 1
self.score += SCORES[item]
def check_game_over(self):
return self.hammers <= 0 or self.apples_found >= 1
def draw_grid(game):
grid_x_offset = (SCREEN_WIDTH - CELL_SIZE * GRID_SIZE) // 2
grid_y_offset = (SCREEN_HEIGHT - (GRID_SIZE * CELL_SIZE + 40)) // 2
item_size = int(CELL_SIZE * 0.9) # 90% of the cell size
for i in range(GRID_SIZE):
for j in range(GRID_SIZE):
color = CELL_COLOR_HIDDEN if not game.revealed[i][j] else CELL_COLOR_REVEALED
pygame.draw.rect(screen, color,
(grid_x_offset + j * CELL_SIZE, grid_y_offset + i * CELL_SIZE, CELL_SIZE, CELL_SIZE))
pygame.draw.rect(screen, BORDER_COLOR,
(grid_x_offset + j * CELL_SIZE, grid_y_offset + i * CELL_SIZE, CELL_SIZE, CELL_SIZE),
2) # Border
if game.revealed[i][j]:
# Calculate the position to center the image
image_x = grid_x_offset + j * CELL_SIZE + (CELL_SIZE - item_size) // 2
image_y = grid_y_offset + i * CELL_SIZE + (CELL_SIZE - item_size) // 2
screen.blit(item_images[game.board[i][j]], (image_x, image_y))
def handle_game_over(game):
if game.apples_found >= 1:
message = "Found an Apple! Next level...."
color = SUCCESS_COLOR
else:
message = "Game over! No hammers!"
color = GAME_OVER_COLOR
message_surface = large_font.render(message, True, color)
screen.blit(message_surface, (SCREEN_WIDTH // 2 - 250, SCREEN_HEIGHT // 2 - 20))
pygame.display.flip()
pygame.time.wait(2000)
if game.apples_found >= 1:
game.reset()
def main():
game = Game()
clock = pygame.time.Clock()
while True:
screen.fill(BACKGROUND_COLOR) # Set background color
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN and game.hammers > 0:
mouse_x, mouse_y = pygame.mouse.get_pos()
grid_x = (mouse_y - (SCREEN_HEIGHT - (GRID_SIZE * CELL_SIZE + 40)) // 2) // CELL_SIZE
grid_y = (mouse_x - (SCREEN_WIDTH - CELL_SIZE * GRID_SIZE) // 2) // CELL_SIZE
if 0 <= grid_x < GRID_SIZE and 0 <= grid_y < GRID_SIZE:
game.reveal_item(grid_x, grid_y)
draw_grid(game)
score_surface = font.render(f"Scores: {game.score} Hammers: {game.hammers}", True, BLACK)
screen.blit(score_surface,
(10, GRID_SIZE * CELL_SIZE + (SCREEN_HEIGHT - (GRID_SIZE * CELL_SIZE + 40)) // 2 + 10))
if game.check_game_over():
handle_game_over(game)
pygame.display.flip()
clock.tick(30)
if __name__ == "__main__":
main()
5. 结语
后续可以通过其他功能进行扩展进行进一步完善,例如:增强的图形、声音效果、更多物品类型和分数逻辑,关卡控制,甚至多人游戏功能。