使用Python语言写一个推箱子游戏

使用Python语言写一个推箱子游戏

本游戏旨在提供一个趣味性的益智游戏,玩家需要通过推动箱子到指定位置来过关。

游戏规则

  1. 玩家需要推动一个或多个箱子到指定位置,才能过关。
  2. 箱子只能向前推,不能拉回来。
  3. 箱子不允许被推到障碍物、墙壁或其他箱子上。
  4. 玩家可以通过 UNDO 按钮来撤回上一步操作,最多可以撤回10步。
  5. 箱子在指定位置时,会变成已完成状态。

游戏界面

游戏界面由以下元素组成:

  1. 地图:表示游戏地图,包含空地、障碍物、墙壁、箱子和目标位置。
  2. 玩家:表示游戏玩家,用于推动箱子。
  3. 箱子:表示游戏中的箱子,需要被推到目标位置。
  4. 目标位置:表示箱子需要被推到的位置。
  5. 按钮:包含开始、暂停、撤回、重新开始等功能按钮。

技术实现

本游戏使用 Python 语言进行开发,采用 Pygame 框架进行图形界面设计和实现游戏逻辑。

结语

推箱子游戏是一款非常经典的益智游戏,通过本项目的开发,我们不仅可以锻炼编程技能,还可以提高逻辑思维和创造力。

程序代码

import pygame
import sys

# 初始化 Pygame
pygame.init()

# 定义游戏界面大小
size = width, height = 800, 600

# 定义游戏界面背景颜色
bg = (255, 255, 255)

# 创建游戏界面
screen = pygame.display.set_mode(size)
pygame.display.set_caption("PushBox")

# 载入游戏所需资源图片
player = pygame.image.load("player.png").convert_alpha()
box = pygame.image.load("box.png").convert_alpha()
target = pygame.image.load("target.png").convert_alpha()
wall = pygame.image.load("wall.png").convert_alpha()

# 定义游戏地图
# 0 表示空地,1 表示障碍物,2 表示墙壁,3 表示箱子,4 表示目标位置,5 表示玩家
map_data = [
    [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
    [2, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 2, 2, 2],
    [2, 0, 1, 1, 0, 0, 1, 4, 4, 1, 1, 1, 2, 2, 2],
    [2, 0, 4, 3, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2],
    [2, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2],
    [2, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2],
    [2, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 2],
    [2, 0, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 1, 1, 2],
    [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
]

# 定义游戏元素大小
block_size = 50

# 定义玩家初始位置
player_pos = [5, 1]

# 定义推箱子游戏类
class PushBoxGame:
    def __init__(self):
        self.map_data = map_data
        self.block_size = block_size
        self.player_pos = player_pos

    # 绘制游戏地图
    def draw_map(self):
        for y in range(len(self.map_data)):
            for x in range(len(self.map_data[y])):
                if self.map_data[y][x] == 1:
                    screen.blit(wall, (x * self.block_size, y * self.block_size))
                elif self.map_data[y][x] == 2:
                    screen.blit(box, (x * self.block_size, y * self.block_size))
                elif self.map_data[y][x] == 3:
                    screen.blit(target, (x * self.block_size, y * self.block_size))
                elif self.map_data[y][x] == 4:
                    screen.blit(box, (x * self.block_size, y * self.block_size))
                    screen.blit(target, (x * self.block_size, y * self.block_size))
                elif self.map_data[y][x] == 5:
                    screen.blit(player, (x * self.block_size, y * self.block_size))

    # 检查游戏是否结束
    def is_game_over(self):
        for y in range(len(self.map_data)):
            for x in range(len(self.map_data[y])):
                if self.map_data[y][x] == 3:
                    return False
        return True

    # 检查玩家是否可以移动到目标位置
    def can_move_to_target(self, pos, target_pos):
        dx = abs(pos[0] - target_pos[0])
        dy = abs(pos[1] - target_pos[1])
        if dx + dy == 1:
            return True
        else:
            return False

    # 移动箱子
    def move_box(self, pos, direction):
        if self.map_data[pos[1] + direction[1]][pos[0] + direction[0]] == 0:
            self.map_data[pos[1]][pos[0]] = 0
            self.map_data[pos[1] + direction[1]][pos[0] + direction[0]] = 2
            return True
        elif self.map_data[pos[1] + direction[1]][pos[0] + direction[0]] == 3:
            self.map_data[pos[1]][pos[0]] = 0
            self.map_data[pos[1] + direction[1]][pos[0] + direction[0]] = 4
            return True
        else:
            return False

    # 移动玩家
    def move_player(self, direction):
        new_pos = [self.player_pos[0] + direction[0], self.player_pos[1] + direction[1]]
        if self.map_data[new_pos[1]][new_pos[0]] == 0:
            self.map_data[self.player_pos[1]][self.player_pos[0]] = 0
            self.map_data[new_pos[1]][new_pos[0]] = 5
            self.player_pos = new_pos
            return True
        elif self.map_data[new_pos[1]][new_pos[0]] == 3 or self.map_data[new_pos[1]][new_pos[0]] == 4:
            if self.move_box(new_pos, direction):
                self.map_data[self.player_pos[1]][self.player_pos[0]] = 0
                self.map_data[new_pos[1]][new_pos[0]] = 5
                self.player_pos = new_pos
                return True
            else:
                return False
        else:
            return False

# 创建推箱子游戏对象
game = PushBoxGame()

# 游戏主循环
while True:

    # 处理游戏事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                if game.move_player([0, -1]):
                    pass
            elif event.key == pygame.K_DOWN:
                if game.move_player([0, 1]):
                    pass
            elif event.key == pygame.K_LEFT:
                if game.move_player([-1, 0]):
                    pass
            elif event.key == pygame.K_RIGHT:
                if game.move_player([1, 0]):
                    pass
            elif event.key == pygame.K_ESCAPE:
                sys.exit()

    # 绘制游戏界面
    screen.fill(bg)
    game.draw_map()
    pygame.display.update()

    # 检查游戏是否结束
    if game.is_game_over():
        print("You win!")
        sys.exit()

猜你喜欢

转载自blog.csdn.net/qq_29785223/article/details/129622397
今日推荐