python实现推箱子

python课程作业-推箱子

现在整理经常会用到的课程作业( python第三弹,持续更新中…)







简介

现在经常需要用到各种作业,网上的资料存在着各种各样的问题,因此我就为了大家的方便,将这些收集整理并在本地进行验证。对这些代码或资料中能缺少、错误的部分加以修正和补充,确保按照我的步骤可以很简单的把这个代码运行出来,供大家当平时作业或者练习使用。

保证可以运行!!!



运行结果

在这里插入图片描述



系统环境

windows 10
python 3.6、3.7、3.8



其他依赖

需要先在win+r 输入 cmd,使用:

pip install keyboard




运行说明

按键盘进行操作:

W\A\S\D 操作方向
T 退出




实现代码

新建“推箱子.py”文件,下列代码直接复制过去就可以使用:

# -*- coding: utf-8 -*-

# 初始化
# 墙:0    箱子:1   空:2    到达目的地:3   目的地: 4:   人:5

import keyboard
def Init():
    Interface = [
        [2, 0, 0, 0, 0, 2],
        [0, 0, 2, 2, 0, 2],
        [0, 5, 1, 2, 0, 2],
        [0, 0, 1, 2, 0, 0],
        [0, 0, 2, 1, 2, 0],
        [0, 4, 1, 2, 2, 0],
        [0, 4, 4, 3, 4, 0],
        [0, 0, 0, 0, 0, 0]
    ]  # 初始化界面
    return Interface

Interface = Init()

target = []  # 当人在目的地行走时,不会用空白将目的地覆盖
target_flag = 1
key = 0


# 显示界面
def show(Interface):
    global target_flag
    for i in range(len(Interface)):
        for j in range(len(Interface[i])):
            if ((j + 1) % len(Interface[i]) != 0):  # 没有一行不换行
                end = ''
            else:
                end = '\n'
            if Interface[i][j] == 0:
                print('■', end=end)
            if Interface[i][j] == 1:
                print('☆', end=end)
            if Interface[i][j] == 2:
                print('  ', end=end)
            if Interface[i][j] == 3:
                print('●', end=end)
                if target_flag == 1:
                    target.append([i, j])
            if Interface[i][j] == 4:
                print('★', end=end)
                if target_flag == 1:
                    target.append([i, j])
            if Interface[i][j] == 5:
                print('♀', end=end)
    print('Show complete!')
    target_flag += 1  # 只添加第一次到target


def get_key(key_):
    global key
    key = key_

def get_input(Interface):
    def tui(input_key):
        break_flag = False  # 若指定运行完,则break
        input_key = input_key.name
        if input_key == 't' or input_key=='T':
            exit()
        for i in range(len(Interface)):
            for j in range(len(Interface[i])):
                if input_key == 'a' or input_key == 'A':  # 向左移动
                    # 如果数组为人且左边不为墙 那么俩种情况
                    if Interface[i][j] == 5 and Interface[i][j - 1] != 0:
                        if Interface[i][j - 1] != 1 and Interface[i][j - 1] != 3:  # 左边不为箱子,到达目的地
                            Interface[i][j - 1] = 5
                            Interface[i][j] = 2
                            break
                        else:  # 左边为箱子或目的地到达
                            if Interface[i][j - 2] != 0:  # 箱子左边不是墙
                                if Interface[i][j - 2] != 4:  # 箱子左边不是目的地
                                    Interface[i][j] = 2
                                    Interface[i][j - 1] = 5
                                    Interface[i][j - 2] = 1
                                else:  # 箱子到达目的地
                                    Interface[i][j] = 2
                                    Interface[i][j - 1] = 5
                                    Interface[i][j - 2] = 3

                            else:
                                pass
                elif input_key == 'd' or input_key == 'D':  # 向右移动
                    # 如果数组为人且右边不为墙 那么俩种情况
                    if Interface[i][j] == 5 and Interface[i][j + 1] != 0:
                        if Interface[i][j + 1] != 1 and Interface[i][j + 1] != 3:  # 右边不为箱子,到达目的地
                            Interface[i][j + 1] = 5
                            Interface[i][j] = 2
                            break  # 如果不break且右边没有障碍物 它会一直右走 for自左向右 向左无影响
                        else:  # 右边为箱子或目的地到达
                            if Interface[i][j + 2] != 0:  # 箱子右边不是墙
                                if Interface[i][j + 2] != 4:  # 箱子右边不是目的地
                                    Interface[i][j] = 2
                                    Interface[i][j + 1] = 5
                                    Interface[i][j + 2] = 1
                                    break
                                else:  # 箱子到达目的地
                                    Interface[i][j] = 2
                                    Interface[i][j + 1] = 5
                                    Interface[i][j + 2] = 3
                                    break
                            else:
                                pass
                elif input_key == 'w' or input_key == 'W':  # 向上移动
                    # 如果数组为人且上边不为墙 那么俩种情况
                    if Interface[i][j] == 5 and Interface[i - 1][j] != 0:
                        if Interface[i - 1][j] != 1 and Interface[i - 1][j] != 3:  # 上边不为箱子,到达目的地
                            Interface[i - 1][j] = 5
                            Interface[i][j] = 2
                            break
                        else:  # 上边为箱子或目的地到达
                            if Interface[i - 2][j] != 0:  # 箱子上边不是墙
                                if Interface[i - 2][j] != 4:  # 箱子上边不是目的地
                                    Interface[i][j] = 2
                                    Interface[i - 1][j] = 5
                                    Interface[i - 2][j] = 1
                                else:  # 箱子到达目的地
                                    Interface[i][j] = 2
                                    Interface[i - 1][j] = 5
                                    Interface[i - 2][j] = 3
                            else:
                                pass

                elif input_key == 's' or input_key == 'S':  # 向下移动
                    # 如果数组为人且下边不为墙 那么俩种情况
                    if Interface[i][j] == 5 and Interface[i + 1][j] != 0:
                        if Interface[i + 1][j] != 1 and Interface[i + 1][j] != 3:  # 下边不为 箱子,到达目的地
                            Interface[i + 1][j] = 5
                            Interface[i][j] = 2
                            break_flag = True

                        else:  # 下边为箱子或目的地到达
                            if Interface[i + 2][j] != 0:  # 箱子下边不是墙
                                if Interface[i + 2][j] != 4:  # 箱子下边不是目的地
                                    Interface[i][j] = 2
                                    Interface[i + 1][j] = 5
                                    Interface[i + 2][j] = 1
                                    break_flag = True
                                else:  # 箱子到达目的地
                                    Interface[i][j] = 2
                                    Interface[i + 1][j] = 5
                                    Interface[i + 2][j] = 3
                                    break_flag = True

                            else:
                                pass
                    if break_flag == True:  # 跳出第二层循环,否则会一直向下走 向上不会出现这种情况,因为for自上而下
                        break
                else:
                    print('您的输入有误,请重新输入')
                    break_flag = True
                    break
            if break_flag == True:  # 跳出第二层循环,否则会一直向下走 向上不会出现这种情况,因为for自上而下
                break

        for i in range(len(target)):
            for j in range(1):
                # 如果不是到达目的地的情况和人在目的地的情况   目的地还是为'★'
                if Interface[target[i][j]][target[i][j + 1]] != 3 and Interface[target[i][j]][target[i][j + 1]] != 5:
                    Interface[target[i][j]][target[i][j + 1]] = 4

        show(Interface)
        count = judge(Interface)

        if count == len(target):
            print('恭喜您,通关了')
            return

    while True:
        keyboard.hook(tui)
        keyboard.wait()


# 判断是否通关
def judge(Interface):
    count = 0  # 计数 如果都到达目的地 退出循环
    for i in range(len(Interface)):
        for j in range(len(Interface[i])):
            if Interface[i][j] == 3:
                count += 1
    return count


def main():
    show(Interface)
    get_input(Interface)


if __name__ == '__main__':
    main()





##




###

结语

和原来的差别,自动读取键盘,增加推出按键!

参考文献:

python推箱子:https://blog.csdn.net/weixin_43512511/article/details/91960530

  • 3
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
推箱子游戏是一种益智类游戏,玩家需要通过移动箱子来达到目标位置。以下是用Python实现推箱子游戏的步骤: 1. 定义游戏地图,可以使用二维列表来表示,其中0表示空地,1表示墙,2表示箱子,3表示目标点,4表示人物。 2. 定义游戏角色的移动方法,包括上下左右四个方向,需要判断移动后的位置是否合法,如果是箱子则需要判断箱子是否可以移动。 3. 定义游戏胜利条件,即所有箱子都被移动到目标点上。 4. 定义游戏主循环,不断接收玩家输入并更新游戏状态,直到游戏结束。 以下是一个简单的推箱子游戏的Python实现代码: ```python # -*- coding: utf-8 -*- import os # 定义地图 map_data = [ [1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 1], [1, 0, 2, 0, 3, 0, 1], [1, 0, 0, 4, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1] ] # 定义角色初始位置 player_x, player_y = 3, 3 # 定义移动方法 def move(x, y, dx, dy): global player_x, player_y if map_data[y+dy][x+dx] == 0: # 移动到空地 map_data[y][x] = 0 map_data[y+dy][x+dx] = 4 player_x, player_y = x+dx, y+dy elif map_data[y+dy][x+dx] == 2: # 移动到箱子 if map_data[y+dy*2][x+dx*2] == 0: map_data[y][x] = 0 map_data[y+dy][x+dx] = 4 map_data[y+dy*2][x+dx*2] = 2 player_x, player_y = x+dx, y+dy else: # 其他情况不能移动 pass # 定义胜利条件 def is_win(): for row in map_data: if 2 in row: return False return True # 游戏主循环 while True: # 清屏 os.system('cls' if os.name == 'nt' else 'clear') # 打印地图 for row in map_data: print(''.join(['{:3}'.format(item) for item in row])) # 判断胜利条件 if is_win(): print('You win!') break # 接收玩家输入 op = input('Please input your operation(w/s/a/d):') if op == 'w': move(player_x, player_y, 0, -1) elif op == 's': move(player_x, player_y, 0, 1) elif op == 'a': move(player_x, player_y, -1, 0) elif op == 'd': move(player_x, player_y, 1, 0) else: pass # 相关问题: --相关问题--:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_刘文凯_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值