简单脚本分享--扫雷

import pygame,sys,random
from pygame.locals import *

# 默认窗口大小
windowWidth = 800
windowHeight = 800
#默认方格大小
rectSize = 39
#颜色常量
fontColor = pygame.Color("red")
inputBoxColor = pygame.Color("white")
backgroundColor = pygame.Color("black")
defaultColor = pygame.Color("gray")
promptColor = pygame.Color("pink")
warningColor = pygame.Color("red")
safeColor = pygame.Color("green")
#常量字符串
captionString = "扫雷游戏"
welcomeString = "请输入扫雷的宽,高,和雷的数量,用','将不同数据隔开"
failureString = "你踩到了雷,GAME OVER,按ENTER返回开始界面"
successString = "你已找出所有雷,游戏通关,按ENTER返回开始界面"
#输入框常量
inputSurfaceWidth = 600
#最大值限制
MaxNum = 20

#初始化
pygame.init()
#宽和高  雷的数量
width = 0
height = 0
mineNum = 0
# 初始化面板
screen = pygame.display.set_mode((windowWidth, windowHeight))
initSurface = pygame.Surface((windowWidth, windowHeight), flags=pygame.HWSURFACE)
pygame.display.set_caption(captionString)
# 游戏状态     0为欢迎界面,1为游戏界面,2为失败结束界面,3为成功结束界面
mode = 0
# 字体管理
font = pygame.font.SysFont("华文楷体", size=30, bold=True)
inputFont = pygame.font.SysFont("华文楷体", size=30, bold=False)
welcome = font.render(welcomeString, True, fontColor, backgroundColor)
failure = font.render(failureString, True, fontColor, backgroundColor)
success = font.render(successString, True, fontColor, backgroundColor)
# 获得渲染后的rect
welcome_rect = welcome.get_rect()
failure_rect = failure.get_rect()
success_rect = success.get_rect()
# rect居中
welcome_rect.center = (windowWidth // 2, windowHeight // 2)
failure_rect.center = (windowWidth // 2, windowHeight // 2)
success_rect.center = (windowWidth // 2, windowHeight // 2)
#输入框
inputSurface = pygame.Surface((inputSurfaceWidth, welcome_rect.height), flags=pygame.HWSURFACE)
inputSurface.fill(color = inputBoxColor)
#算的是左上角坐标
inputSurfacePos = (windowWidth // 2 - inputSurfaceWidth // 2, windowHeight // 2 - welcome_rect.height // 2 - welcome_rect.height)
inputText = ""
hintText = ""
#基础面板常量  默认展现的方块
defaultRect = pygame.Surface((rectSize, rectSize), flags=pygame.HWSURFACE)
defaultRect.fill(color = defaultColor)
#被标记为雷的方块
warningRect = pygame.Surface((rectSize, rectSize), flags=pygame.HWSURFACE)
warningRect.fill(color = warningColor)
resultRect = [0] * 10
#数量为0的贴图
resultRect[0] = pygame.Surface((rectSize, rectSize), flags=pygame.HWSURFACE)
resultRect[0].fill(color = safeColor)
#数量为1-9的贴图   所有展示为数字的方块必定已被确认为非雷
for i in range(9):
    resultRect[i + 1] = pygame.Surface((rectSize, rectSize), flags=pygame.HWSURFACE)
    resultRect[i + 1].fill(color = promptColor)
    numPrompt = inputFont.render(str(i + 1), True, fontColor, promptColor)
    numPromptRect = numPrompt.get_rect()
    numPromptRect.center = ((rectSize + 1) // 2, (rectSize + 1) // 2)
    resultRect[i + 1].blit(numPrompt, numPromptRect)
#关键全局变量
mineMap = {}
stateList = []#代表当前展示的状态 0为默认    1为被标记     2为已确认非雷
mineNumList = []
startPos = [0, 0]
changeMap = {}#代表当前展示的状态 0为默认    1为被标记     2为已确认非雷
init = False

#初始化雷的分布集合
def initMineMap():
    global mineMap, mineNum
    maxNum = height * width
    for i in range(mineNum):
        index = random.randrange(0, maxNum)
        while index in mineMap:
            index = random.randrange(0, maxNum)
        mineMap[index] = 1
#初始化雷的分布数组
def initMineNumList():
    global mineNumList,height,width
    for index in range(height * width):
        #计数时存在的情况  当位于四个角时
        if index == 0:#左上角
            mineNumList[0] = mineMap.get(0, 0) + mineMap.get(1, 0) + mineMap.get(width, 0) + mineMap.get(width + 1, 0)
        elif index == width - 1:#右上角
            mineNumList[width - 1] = mineMap.get(width - 2, 0) + mineMap.get(width - 1, 0) + mineMap.get(width * 2 - 2, 0) + mineMap.get(width * 2 - 1, 0)
        elif index == width * (height - 1):#左下角
            mineNumList[width * (height - 1)] = mineMap.get(width * (height - 1), 0) + mineMap.get(width * (height - 1) + 1, 0) + mineMap.get(width * (height - 2), 0) + mineMap.get(width * (height - 2) + 1, 0)
        elif index == width * height - 1:#右下角
            mineNumList[width * height - 1] = mineMap.get(width * height - 1, 0) + mineMap.get(width * height - 2, 0) + mineMap.get(width * (height - 1) - 1, 0) + mineMap.get(width * (height - 1) - 2, 0)
        elif index >0 and index < width - 1:#顶边  去掉-width部分
            mineNumList[index] = mineMap.get(index, 0) + mineMap.get(index + 1, 0) + mineMap.get(index - 1, 0) + \
                mineMap.get(index + width, 0) + mineMap.get(index + 1 + width, 0) + mineMap.get(index - 1 + width, 0)
        elif index > width * (height - 1) and  index < width * height - 1:#底边   去掉+width部分
            mineNumList[index] = mineMap.get(index, 0) + mineMap.get(index + 1, 0) + mineMap.get(index - 1, 0) + \
                mineMap.get(index - width, 0) + mineMap.get(index + 1 - width, 0) + mineMap.get(index - 1 - width, 0)
        elif index % width == 0 and index > 0 and index < width * (height - 1):#左侧边   去掉-1部分
            mineNumList[index] = mineMap.get(index, 0) + mineMap.get(index + 1, 0) + mineMap.get(index + width, 0) + \
                mineMap.get(index + 1 + width, 0) + mineMap.get(index - width, 0) + mineMap.get(index + 1 - width, 0)
        elif index % width == width - 1 and index > 0 and index < width * (height - 1):#右侧边   去掉+1部分
            mineNumList[index] = mineMap.get(index, 0) + mineMap.get(index - 1, 0) + mineMap.get(index + width, 0) + \
                mineMap.get(index - 1 + width, 0) + mineMap.get(index - width, 0) + mineMap.get(index - 1 - width, 0)
        else :#中间部分
            mineNumList[index] = mineMap.get(index, 0) + mineMap.get(index + 1, 0) + mineMap.get(index - 1, 0) + \
                mineMap.get(index + width, 0) + mineMap.get(index + 1 + width, 0) + mineMap.get(index - 1 + width, 0) + \
                mineMap.get(index - width, 0) + mineMap.get(index + 1 - width, 0) + mineMap.get(index - 1 - width, 0)

#初始化游戏
def initGame():
    global mineMap, stateList, mineNumList, startPos, height, width, mineNum, init
    mineMap.clear()
    changeMap.clear()
    startPos[0] = (windowWidth - width * 40) // 2
    startPos[1] = (windowHeight - height * 40) // 2
    stateList = [0] * height * width
    mineNumList = [0] * height * width
    initMineMap()
    initMineNumList()
    init = True
    print(mineMap)
    print(mineNumList)

def pushIndex(indexNum):
    #将它放入变化表中
    changeMap[indexNum] = 2
    #确认为非雷
    stateList[indexNum] = 2
    #print(indexNum)
    #要求 范围在[0, width * height]之间    雷的数量为0    且未被标记为已确认非雷(防止递归死循环)
    if indexNum >= 0 and indexNum < width * height and mineNumList[indexNum] == 0:
        if indexNum == 0:#左上角
            if stateList[indexNum + width] != 2:
                pushIndex(indexNum + width)
            if stateList[indexNum + 1] != 2:
                pushIndex(indexNum + 1)
            if stateList[indexNum + width + 1] != 2:
                pushIndex(indexNum + width + 1)
        elif indexNum == width - 1:#右上角
            if stateList[indexNum - 1] != 2:
                pushIndex(indexNum - 1)
            if stateList[indexNum + width - 1] != 2:
                pushIndex(indexNum + width - 1)
            if stateList[indexNum + width] != 2:
                pushIndex(indexNum + width)
        elif indexNum == width * (height - 1):#左下角
            if stateList[indexNum - width + 1] != 2:
                pushIndex(indexNum - width + 1)
            if stateList[indexNum + 1] != 2:
                pushIndex(indexNum + 1)
            if stateList[indexNum - width] != 2:
                pushIndex(indexNum - width)
        elif indexNum == width * height - 1:#右下角
            if stateList[indexNum - width - 1] != 2:
                pushIndex(indexNum - width - 1)
            if stateList[indexNum - 1] != 2:
                pushIndex(indexNum - 1)
            if stateList[indexNum - width] != 2:
                pushIndex(indexNum - width)
        elif indexNum >0 and indexNum < width - 1:#顶边  去掉-width部分
            if stateList[indexNum - 1] != 2:
                pushIndex(indexNum - 1)
            if stateList[indexNum + width - 1] != 2:
                pushIndex(indexNum + width - 1)
            if stateList[indexNum + width] != 2:
                pushIndex(indexNum + width)
            if stateList[indexNum + 1] != 2:
                pushIndex(indexNum + 1)
            if stateList[indexNum + width + 1] != 2:
                pushIndex(indexNum + width + 1)
        elif indexNum > width * (height - 1) and  indexNum < width * height - 1:#底边   去掉+width部分
            if stateList[indexNum - width - 1] != 2:
                pushIndex(indexNum - width - 1)
            if stateList[indexNum - 1] != 2:
                pushIndex(indexNum - 1)
            if stateList[indexNum - width] != 2:
                pushIndex(indexNum - width)
            if stateList[indexNum - width + 1] != 2:
                pushIndex(indexNum - width + 1)
            if stateList[indexNum + 1] != 2:
                pushIndex(indexNum + 1)
        elif indexNum % width == 0 and indexNum > 0 and indexNum < width * (height - 1):#左侧边   去掉-1部分
            if stateList[indexNum - width] != 2:
                pushIndex(indexNum - width)
            if stateList[indexNum + width] != 2:
                pushIndex(indexNum + width)
            if stateList[indexNum - width + 1] != 2:
                pushIndex(indexNum - width + 1)
            if stateList[indexNum + 1] != 2:
                pushIndex(indexNum + 1)
            if stateList[indexNum + width + 1] != 2:
                pushIndex(indexNum + width + 1)
        elif indexNum % width == width - 1 and indexNum > 0 and indexNum < width * (height - 1):#右侧边   去掉+1部分
            if stateList[indexNum - width - 1] != 2:
                pushIndex(indexNum - width - 1)
            if stateList[indexNum - 1] != 2:
                pushIndex(indexNum - 1)
            if stateList[indexNum + width - 1] != 2:
                pushIndex(indexNum + width - 1)
            if stateList[indexNum - width] != 2:
                pushIndex(indexNum - width)
            if stateList[indexNum + width] != 2:
                pushIndex(indexNum + width)
        else :#中间部分
            if stateList[indexNum - width - 1] != 2:
                pushIndex(indexNum - width - 1)
            if stateList[indexNum - 1] != 2:
                pushIndex(indexNum - 1)
            if stateList[indexNum + width - 1] != 2:
                pushIndex(indexNum + width - 1)
            if stateList[indexNum - width] != 2:
                pushIndex(indexNum - width)
            if stateList[indexNum + width] != 2:
                pushIndex(indexNum + width)
            if stateList[indexNum - width + 1] != 2:
                pushIndex(indexNum - width + 1)
            if stateList[indexNum + 1] != 2:
                pushIndex(indexNum + 1)
            if stateList[indexNum + width + 1] != 2:
                pushIndex(indexNum + width + 1)

def checkSuccess():
    print(len(mineMap), width * height)
    if stateList.count(2) + len(mineMap) == width * height:
        return True
    else:
        return False


#导入pygame 库
# python F:\游戏开发\扫雷\main.py
if __name__ == '__main__':
    mode = 0
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                # 卸载所有模块
                pygame.quit()
                # 终止程序,确保退出程序
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                #只有初始态支持输入
                if mode == 0 and ((event.unicode <= '9' and event.unicode >= '0') or event.unicode == ','):
                    inputText = inputText + event.unicode
                elif mode == 0 and event.unicode == '\b':#只有初始态支持输入
                    inputText = inputText[0:-1]
                elif event.key == pygame.K_RETURN:
                    if mode == 0:
                        if inputText == "":
                            hintText = "输入数据为空"
                        else :
                            inputData = inputText.split(',')
                            if len(inputData) != 3:
                                hintText = "输入数据数量不正确"
                            else :
                                try:
                                    height = int(inputData[0])
                                    width = int(inputData[1])
                                    mineNum = int(inputData[2])
                                    if width > MaxNum or  height > MaxNum or mineNum >= height * width - 1 or width < 3 or height < 3:
                                        hintText = "输入数据范围不正确,宽高,最小数量为3,最大数量均为" + str(MaxNum)
                                    else:
                                        hintText = ""
                                        mode = 1
                                        initGame()
                                except:
                                    hintText = "输入数据格式不正确"
                    else:
                        mode = 0
                #mode = (mode + 1) % 3
            #event.pos  事件坐标  event.button  左键为1,右键为3,中键为2
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if mode == 1:
                    index = ((event.pos[0] - startPos[0]) // 40, (event.pos[1] - startPos[1]) // 40)
                    indexNum = index[0] + index[1] * width
                    if event.button == 1:
                        #只有在默认状态下可踩雷
                        if stateList[indexNum] == 0:
                            #踩到雷,失败
                            if mineMap.get(indexNum, 0) == 1:
                                mode = 2
                            else:
                                pushIndex(indexNum)
                                if checkSuccess():
                                    mode = 3
                        else:
                            print("该点击无效")
                    elif event.button == 3:
                        #增加和删除标记
                        if stateList[indexNum] == 0:
                            changeMap[indexNum] = 1
                            stateList[indexNum] = 1
                        elif stateList[indexNum] == 1:
                            changeMap[indexNum] = 0
                            stateList[indexNum] = 0
                        else:
                            print("该点击无效")            
            if mode == 0:# 转为黑色背景
                initSurface.fill(color=backgroundColor)
                screen.blit(initSurface, (0, 0))
                hintTextSurf = font.render(hintText, True, fontColor, backgroundColor)
                inputTextSurf = inputFont.render(inputText, True, backgroundColor, inputBoxColor)
                hintTextRect = hintTextSurf.get_rect()
                hintTextRect.center = (windowWidth // 2 , windowHeight // 2 - welcome_rect.height // 2 - welcome_rect.height - hintTextRect.height // 2)
                screen.blit(hintTextSurf, hintTextRect)
                screen.blit(welcome, welcome_rect)
                screen.blit(inputSurface, inputSurfacePos)
                screen.blit(inputTextSurf, inputSurfacePos)
            elif mode == 1:
                if init:
                    initSurface.fill(color=backgroundColor)
                    screen.blit(initSurface, (0, 0))
                    for i in range(width):
                        for j in range(height):
                            screen.blit(defaultRect, (startPos[0] + i * 40, startPos[1] + j * 40))
                    init = False
                else :
                    for i in changeMap.keys():
                        x = i % width
                        y = i // width
                        if changeMap[i] == 1:
                            screen.blit(warningRect, (startPos[0] + x * 40, startPos[1] + y * 40))
                        elif changeMap[i] == 0:
                            screen.blit(defaultRect, (startPos[0] + x * 40, startPos[1] + y * 40))
                        elif changeMap[i] == 2:
                            screen.blit(resultRect[mineNumList[i]], (startPos[0] + x * 40, startPos[1] + y * 40))
                    changeMap.clear()
            elif mode == 2:
                initSurface.fill(color=backgroundColor)
                screen.blit(initSurface, (0, 0))
                screen.blit(failure, failure_rect)
            elif mode == 3:
                initSurface.fill(color=backgroundColor)
                screen.blit(initSurface, (0, 0))
                screen.blit(success, success_rect)
            pygame.display.update()

    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
### 回答1: SecureCRT脚本使用VBScript语法。VBScript是一种微软开发的脚本语言,它是Visual Basic的一部分,主要用于Windows操作系统上的脚本编写。在SecureCRT中,VBScript语法可以用于编写自动化脚本,实现自动化登录、执行命令、收集输出等功能。常用的VBScript语法包括变量定义、条件语句、循环语句、函数定义等。例如,以下是一个简单的VBScript脚本示例: ``` ' 定义变量 Dim strUsername, strPassword ' 输入用户名和密码 strUsername = "admin" strPassword = "password" ' 连接远程主机 crt.Session.Connect "/ssh2 /auth=password /user=" & strUsername & " /password=" & strPassword & " 192.168.1.1" ' 执行命令并收集输出 crt.Screen.Send "ls" & vbCr Do While crt.Screen.WaitForString("$") = 0 WScript.Sleep 100 Loop strOutput = crt.Screen.ReadString("$") ' 输出结果 MsgBox strOutput ``` 该脚本定义了两个变量strUsername和strPassword,然后使用这些变量连接到远程主机,并执行ls命令,最后将输出结果弹出消息框显示。 ### 回答2: SecureCRT 脚本可以使用多种编程语言编写,其中一种常用的是 VBScript(vbs)语言。VBScript 是一种微软开发的脚本语言,简单易学,同时也具有较强的处理能力。 SecureCRT 的 VBScript 常用语法包括: 1.创建一个新的 Session 对象 使用对象的 CreateObject 方法来创建一个新 Session 对象,语法如下: ``` Dim objSecureCRT, objSession Set objSecureCRT = CreateObject("SecureCRT.Application") Set objSession = objSecureCRT.Sessions.Add("/SSH2 /L user /PASSWORD password /C 3des /M hmac-sha1-96 /CN certificate /D ""directory"" hostname") ``` 其中 `/SSH2` 表示使用 SSH2 协议,`/L` 表示登录时使用的用户名,`/PASSWORD` 表示登录密码,`/C` 和 `/M` 分别表示加密算法和摘要算法, `/D` 表示工作目录,`hostname` 是要连接的主机名或IP地址。 2.向远程设备发送命令并获取返回结果 使用 Session 对象的 Send 和 WaitForString 方法向远程设备发送命令,并获取返回结果,语法如下: ``` objSession.Connect objSession.Send "command" & chr(13) strOutput = objSession.WaitForString("prompt") ``` 其中 `Connect` 方法用于连接远程设备,`Send` 方法用于向设备发送命令, `WaitForString` 方法用于等待设备返回结果,并将结果存储在 `strOutput` 变量中。 3.保存 SecureCRT 会话 使用 Session 对象的 Save 方法可以保存当前的 SecureCRT 会话,语法如下: ``` objSession.Save ``` 4.关闭 SecureCRT 会话 使用 Session 对象的 Close 方法可以关闭当前的 SecureCRT 会话,语法如下: ``` objSession.Close ``` 以上是 SecureCRT 脚本常用的 VBScript 语法,可以根据实际需求进行修改和扩展。同时,SecureCRT 还支持其他编程语言的脚本编写,例如 Perl、Python、JavaScript 等。 ### 回答3: SecureCRT是一种强大的终端模拟器,可以方便地与远程服务器进行通信。其中,SecureCRT脚本是一种用于自动化执行特定任务的代码,可以帮助用户提高工作效率。 SecureCRT脚本是使用VBScript语言编写的,VBScript是一种 Visual Basic语言的轻量级版本。在SecureCRT脚本中,VBScript可用于创建变量、条件语句、循环结构、函数、模块等元素,可实现自动登录、自动执行命令、修改配置等Automate脚本,大大提高日常工作效率。 SecureCRT脚本中的VBScript语法包括以下几个方面: 1.十六进制表示 在SecureCRT脚本中,可以使用VBScript中的类似"CInt"、"CLng"、"CDbl"、"Hex"等来实现十进制和十六进制之间的转换。 2.正则表达式 正则表达式是一种用于匹配特定模式的语法,SecureCRT脚本也支持正则表达式。可以使用VBScript中的"RegExp"函数来实现正则表达式功能。 3.文件操作 SecureCRT脚本也支持文件操作,包括打开、保存、读取、写入文件等功能。可以使用VBScript中的“FileSystemObject”对象来实现文件操作功能。 4.网络通信 SecureCRT脚本中也支持网络通信,包括使用Telnet、SSH等协议访问远程服务器,并可实现自动化登录、执行命令等操作。 总的来说,SecureCRT脚本中的VBScript语法具有灵活性、可扩展性和易用性,可帮助用户实现各种自动化操作,提高工作效率,减少重复性劳动。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

蔡海航

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

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

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

打赏作者

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

抵扣说明:

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

余额充值