python实现推箱子

python实现推箱子,第1张

python课程作业-推箱子

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




目录
  • 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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/570006.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存