百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

适合Python初学者的推箱子教程(附源代码)

off999 2024-09-21 21:06 34 浏览 0 评论

推箱子相信大家都玩过,

今天用python自己实现一个,功能齐全,代码如下:

from tkinter import *
from tkinter.messagebox import *
import copy

root = Tk()
root.title("推箱子 更多请访问www.itprojects.cn")

imgs = [PhotoImage(file='./bmp/Wall.gif'),
        PhotoImage(file='./bmp/Worker.gif'),
        PhotoImage(file='./bmp/Box.gif'),
        PhotoImage(file='./bmp/Passageway.gif'),
        PhotoImage(file='./bmp/Destination.gif'),
        PhotoImage(file='./bmp/WorkerInDest.gif'),
        PhotoImage(file='./bmp/RedBox.gif')]
# 0代表墙,1代表人,2代表箱子,3代表路,4代表目的地
# 5代表人在目的地,6代表放到目的地的箱子
Wall = 0
Worker = 1
Box = 2
Passageway = 3
Destination = 4
WorkerInDest = 5
RedBox = 6
# 原始地图
myArray1 = [[0, 3, 1, 4, 3, 3, 3],
            [0, 3, 3, 2, 3, 3, 0],
            [0, 0, 3, 0, 3, 3, 0],
            [3, 3, 2, 3, 0, 0, 0],
            [3, 4, 3, 3, 3, 0, 0],
            [0, 0, 3, 3, 3, 3, 0],
            [0, 0, 0, 0, 0, 0, 0]]


# 绘制整个游戏区域图形
def drawGameImage():
    global x, y

    for i in range(0, 7):  # 0--6
        for j in range(0, 7):  # 0--6
            if myArray[i][j] == Worker:
                x = i  # 工人当前位置(x,y)
                y = j
                print("工人当前位置:", x, y)
            img1 = imgs[myArray[i][j]]
            cv.create_image((i * 32 + 20, j * 32 + 20), image=img1)
            cv.pack()
    # print (myArray)


def callback(event):  # 按键处理
    global x, y, myArray
    print("按下键:", event.char)
    KeyCode = event.keysym
    # 工人当前位置(x,y)
    if KeyCode == "Up":  # 分析按键消息
        # 向上
        x1 = x;
        y1 = y - 1;
        x2 = x;
        y2 = y - 2;
        # 将所有位置输入以判断并作地图更新
        MoveTo(x1, y1, x2, y2);
    # 向下
    elif KeyCode == "Down":
        x1 = x;
        y1 = y + 1;
        x2 = x;
        y2 = y + 2;
        MoveTo(x1, y1, x2, y2);
    # 向左
    elif KeyCode == "Left":
        x1 = x - 1;
        y1 = y;
        x2 = x - 2;
        y2 = y;
        MoveTo(x1, y1, x2, y2);
    # 向右
    elif KeyCode == "Right":
        x1 = x + 1;
        y1 = y;
        x2 = x + 2;
        y2 = y;
        MoveTo(x1, y1, x2, y2);
    elif KeyCode == "space":  # 空格键
        print("按下键:空格", event.char)
        myArray = copy.deepcopy(myArray1)  # 恢复原始地图
        drawGameImage()


# 判断是否在游戏区域
def IsInGameArea(row, col):
    return (row >= 0 and row < 7 and col >= 0 and col < 7)


def MoveTo(x1, y1, x2, y2):
    global x, y
    P1 = None
    P2 = None
    if IsInGameArea(x1, y1):  # 判断是否在游戏区域
        P1 = myArray[x1][y1];
    if IsInGameArea(x2, y2):
        P2 = myArray[x2][y2]
    if P1 == Passageway:  # P1处为通道
        MoveMan(x, y);
        x = x1;
        y = y1;
        myArray[x1][y1] = Worker;
    if P1 == Destination:  # P1处为目的地
        MoveMan(x, y);
        x = x1;
        y = y1;
        myArray[x1][y1] = WorkerInDest;
    if P1 == Wall or not IsInGameArea(x1, y1):
        # P1处为墙或出界
        return;
    if P1 == Box:  # P1处为箱子
        if P2 == Wall or not IsInGameArea(x1, y1) or P2 == Box:  ##P2处为墙或出界
            return;
    # 以下P1处为箱子

    # P1处为箱子,P2处为通道
    if P1 == Box and P2 == Passageway:
        MoveMan(x, y);
        x = x1;
        y = y1;
        myArray[x2][y2] = Box;
        myArray[x1][y1] = Worker;
    if P1 == Box and P2 == Destination:
        MoveMan(x, y);
        x = x1;
        y = y1;
        myArray[x2][y2] = RedBox;
        myArray[x1][y1] = Worker;
    # P1处为放到目的地的箱子,P2处为通道
    if P1 == RedBox and P2 == Passageway:
        MoveMan(x, y);
        x = x1;
        y = y1;
        myArray[x2][y2] = Box;
        myArray[x1][y1] = WorkerInDest;
    # P1处为放到目的地的箱子,P2处为目的地
    if P1 == RedBox and P2 == Destination:
        MoveMan(x, y);
        x = x1;
        y = y1;
        myArray[x2][y2] = RedBox;
        myArray[x1][y1] = WorkerInDest;
    drawGameImage()
    # 这里要验证是否过关
    if IsFinish():
        showinfo(title="提示", message=" 恭喜你顺利过关")
        print("下一关")


def MoveMan(x, y):
    if myArray[x][y] == Worker:
        myArray[x][y] = Passageway;
    elif myArray[x][y] == WorkerInDest:
        myArray[x][y] = Destination;


def IsFinish():  # 验证是否过关
    bFinish = True;
    for i in range(0, 7):  # 0--6
        for j in range(0, 7):  # 0--6
            if (myArray[i][j] == Destination
                    or myArray[i][j] == WorkerInDest):
                bFinish = False;
    return bFinish;


def drawQiPan():  # 画棋盘
    for i in range(0, 15):
        cv.create_line(20, 20 + 40 * i, 580, 20 + 40 * i, width=2)
    for i in range(0, 15):
        cv.create_line(20 + 40 * i, 20, 20 + 40 * i, 580, width=2)
    cv.pack()


def print_map():  # 输出map地图
    for i in range(0, 15):  # 0--14
        for j in range(0, 15):  # 0--14
            print(map[i][j], end=' ')
        print('w')


cv = Canvas(root, bg='green', width=226, height=226)
# drawQiPan( )
myArray = copy.deepcopy(myArray1)
drawGameImage()

cv.bind("<KeyPress>", callback)
cv.pack()
cv.focus_set()  # 将焦点设置到cv上
root.mainloop()

相关推荐

Alist 玩家请进:一键部署全新分支 Openlist,看看香不香!

Openlist(其前身是鼎鼎大名的Alist)是一款功能强大的开源文件列表程序。它能像“万能钥匙”一样,解锁并聚合你散落在各处的云盘资源——无论是阿里云盘、百度网盘、GoogleDrive还是...

白嫖SSL证书还自动续签?这个开源工具让我告别手动部署

你还在手动部署SSL证书?你是不是也遇到过这些问题:每3个月续一次Let'sEncrypt证书,忘了就翻车;手动配置Nginx,重启服务,搞一次SSL得花一下午;付费证书太贵,...

Docker Compose:让多容器应用一键起飞

CDockerCompose:让多容器应用一键起飞"曾经我也是一个手动启动容器的少年,直到我的膝盖中了一箭。"——某位忘记--link参数的运维工程师引言:容器化的烦恼与...

申请免费的SSL证书,到期一键续签

大家好,我是小悟。最近帮朋友配置网站HTTPS时发现,还有人对宝塔面板的SSL证书功能还不太熟悉。其实宝塔早就内置了免费的Let'sEncrypt证书申请和一键续签功能,操作简单到连新手都能...

飞牛NAS部署TVGate Docker项目,实现内网一键转发、代理、jx

前面分享了两期TVGate:Q大的转发代理工具TVGate升级了,操作更便捷,增加了新的功能跨平台内网转发神器TVGate部署与使用初体验现在项目已经开源,并支持Docker部署,本文介绍如何通...

Docker Compose 编排实战:一键部署多容器应用!

当项目变得越来越复杂,一个服务已经无法满足需求时,你可能需要同时部署数据库、后端服务、前端网页、缓存组件……这时,如果还一个一个手动dockerrun,简直是灾难这就是DockerCompo...

深度测评:Vue、React 一键部署的神器 PinMe

不知道大家有没有这种崩溃瞬间:领导突然要看项目Demo,客户临时要体验新功能,自己写的小案例想发朋友圈;找运维?排期?还要走工单;自己买服务器?域名、SSL、Nginx、防火墙;本地起服务?断电、关...

超简单!一键启动多容器,解锁 Docker Compose 极速编排秘籍

想要用最简单的方式在本地复刻一套完整的微服务环境?只需一个docker-compose.yml文件,你就能一键拉起N个容器,自动组网、挂载存储、环境隔离,全程无痛!下面这份终极指南,教你如何用...

日志文件转运工具Filebeat笔记_日志转发工具

一、概述与简介Filebeat是一个日志文件转运工具,在服务器上以轻量级代理的形式安装客户端后,Filebeat会监控日志目录或者指定的日志文件,追踪读取这些文件(追踪文件的变化,不停的读),并将来自...

K8s 日志高效查看神器,提升运维效率10倍!

通常情况下,在部署了K8S服务之后,为了更好地监控服务的运行情况,都会接入对应的日志系统来进行检测和分析,比如常见的Filebeat+ElasticSearch+Kibana这一套组合...

如何给网站添加 https_如何给网站添加证书

一、简介相信大家都知道https是更加安全的,特别是一些网站,有https的网站更能够让用户信任访问接下来以我的个人网站五岁小孩为例子,带大家一起从0到1配置网站https本次配置的...

10个Linux文件内容查看命令的实用示例

Linux文件内容查看命令30个实用示例详细介绍了10个Linux文件内容查看命令的30个实用示例,涵盖了从基本文本查看、分页浏览到二进制文件分析的各个方面。掌握这些命令帮助您:高效查看各种文本文件内...

第13章 工程化实践_第13章 工程化实践课

13.1ESLint+Prettier代码规范统一代码风格配置//.eslintrc.jsmodule.exports={root:true,env:{node...

龙建股份:工程项目中标_龙建股份有限公司招聘网

404NotFoundnginx/1.6.1【公告简述】2016年9月8日公告,公司于2016年9月6日收到苏丹共和国(简称“北苏丹”)喀土穆州基础设施与运输部公路、桥梁和排水公司出具的中标通知书...

福田汽车:获得政府补助_福田 补贴

404NotFoundnginx/1.6.1【公告简述】2016年9月1日公告,自2016年8月17日至今,公司共收到产业发展补助、支持资金等与收益相关的政府补助4笔,共计5429.08万元(不含...

取消回复欢迎 发表评论: