基于Python3-Pygame的推箱子游戏

发现一款不错的小游戏,玩起来挺有趣,学习的话也有源码可以研究,分享出来大家一起学习。
原项目地址:https://gitee.com/milowork/cangkufan?_from=gitee_search

试玩:
在这里插入图片描述
在这里插入图片描述
源码下载:
在这里插入图片描述
〓〓操作方法〓〓

  • (菜单中)

    ·方向键:选择/调整 ·Z:返回 ·X:确定

  • (游戏中)

    ·方向键:移动 ·Z:撤销一步 ·X/CTRL:拉箱子(Pukoban) ·C+方向键:移动视野 ·V/ESC:暂停
    Z与X键的作用可以在设置中调换。
    另外,将 XSB 文件放入 XSB 文件夹可以使本程序加载;也可以在 XSB 界面按 Ctrl+V 从剪贴板加载。

在这里插入图片描述

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Python推箱子游戏的实现需要使用pygame这个库,pygame是一个Python语言的多媒体处理库,可以用来制作游戏等多媒体应用程序。在Python推箱子游戏中,我们需要绘制地图、箱子、人物等图形,同时还需要响应键盘事件,对人物进行移动,并检测是否完成目标等等。 具体实现过程可以参考以下步骤: 1. 初始化pygame库,设置窗口大小和标题等参数。 2. 加载地图、箱子、人物等图片素材。 3. 绘制地图、箱子、人物等元素。 4. 监听键盘事件,对人物进行移动,并检测是否完成目标。 5. 更新游戏界面。 下面是一个简单的Python推箱子游戏的代码示例,仅供参考: ```python import pygame # 初始化pygame pygame.init() # 设置窗口大小和标题 screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption("推箱子游戏") # 加载图片素材 wall_img = pygame.image.load("wall.png") box_img = pygame.image.load("box.png") target_img = pygame.image.load("target.png") player_img = pygame.image.load("player.png") # 地图数据 map_data = [ "#####", "# #", "#o #", "# #", "#####" ] # 箱子和目标的坐标 boxes = [(2, 1)] targets = [(3, 1)] # 人物的初始坐标 player_pos = (2, 2) # 绘制地图、箱子、目标和人物 def draw_map(): for i in range(len(map_data)): for j in range(len(map_data[i])): if map_data[i][j] == "#": screen.blit(wall_img, (j * 50, i * 50)) elif (j, i) in boxes: screen.blit(box_img, (j * 50, i * 50)) elif (j, i) in targets: screen.blit(target_img, (j * 50, i * 50)) elif (j, i) == player_pos: screen.blit(player_img, (j * 50, i * 50)) # 判断是否完成目标 def check_win(): for box in boxes: if box not in targets: return False return True # 游戏循环 running = True while running: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: # 处理键盘事件,对人物进行移动,并检测是否完成目标 if event.key == pygame.K_LEFT: if (player_pos - 1, player_pos) not in boxes and map_data[player_pos][player_pos - 1] != "#": player_pos = (player_pos - 1, player_pos) elif (player_pos - 1, player_pos) in boxes and (player_pos - 2, player_pos) not in boxes and map_data[player_pos][player_pos - 2] != "#": index = boxes.index((player_pos - 1, player_pos)) boxes[index] = (boxes[index] - 1, boxes[index]) player_pos = (player_pos - 1, player_pos) elif event.key == pygame.K_RIGHT: if (player_pos + 1, player_pos) not in boxes and map_data[player_pos][player_pos + 1] != "#": player_pos = (player_pos + 1, player_pos) elif (player_pos + 1, player_pos) in boxes and (player_pos + 2, player_pos) not in boxes and map_data[player_pos][player_pos + 2] != "#": index = boxes.index((player_pos + 1, player_pos)) boxes[index] = (boxes[index] + 1, boxes[index]) player_pos = (player_pos + 1, player_pos) elif event.key == pygame.K_UP: if (player_pos, player_pos - 1) not in boxes and map_data[player_pos - 1][player_pos] != "#": player_pos = (player_pos, player_pos - 1) elif (player_pos, player_pos - 1) in boxes and (player_pos[0], player_pos - 2) not in boxes and map_data[player_pos[1] - 2][player_pos] != "#": index = boxes.index((player_pos, player_pos - 1)) boxes[index] = (boxes[index], boxes[index] - 1) player_pos = (player_pos, player_pos - 1) elif event.key == pygame.K_DOWN: if (player_pos, player_pos + 1) not in boxes and map_data[player_pos + 1][player_pos] != "#": player_pos = (player_pos, player_pos + 1) elif (player_pos, player_pos + 1) in boxes and (player_pos, player_pos + 2) not in boxes and map_data[player_pos + 2][player_pos] != "#": index = boxes.index((player_pos, player_pos + 1)) boxes[index] = (boxes[index], boxes[index] + 1) player_pos = (player_pos, player_pos + 1) # 绘制游戏界面 screen.fill((255,255,255)) draw_map() pygame.display.update() # 判断是否完成目标,如果完成则弹出提示框并结束游戏 if check_win(): pygame.time.delay(500) pygame.quit() exit() ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值