3,423
社区成员
发帖
与我相关
我的任务
分享
@echo off
setlocal enabledelayedexpansion
title snake v1.0 by 夜行人 2013-04-21
:init
rem 去掉蛇身
for /l %%i in (1,1,%length%) do (
set frame.!body.%%i.x!.!body.%%i.y!=
)
rem 去掉食物
set frame.%food.x%.%food.y%=
rem 正方形框架的边长
set /a width=20
rem 初始蛇身长度
set /a length=3
rem 初始移动方向
set /a position=1
rem 初始运行状态
set run=1
call :createbody
call :createfood
:run
call :display
choice /c "15320" /n /t 1 /d "0">nul
set /a usrchoice=%errorlevel%
if 1 equ %usrchoice% goto horizontal
if 3 equ %usrchoice% goto horizontal
if 2 equ %usrchoice% goto vertical
if 4 equ %usrchoice% goto vertical
if 5 equ %usrchoice% goto default
:horizontal
if 1 equ %position% goto default
if 3 equ %position% goto default
goto changposition
:vertical
if 2 equ %position% goto default
if 4 equ %position% goto default
goto changposition
:changposition
set /a position=%usrchoice%
:default
call :goahead
if defined run (
goto run
)
echo 失败了
goto end
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::函数功能:生成蛇的身体
::函数描述:先生成蛇身第一个方块,再随机选一个方向生成其它方块
::其他方块只需将坐标偏移一个即可
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:createbody
rem 考虑到边界问题,所以将第一个随机数的范围缩小蛇身长度个方块
set /a newwidth=%width% - %length%
set /a body.1.x=%random%%%%newwidth% + 2
set /a body.1.y=%random%%%%newwidth% + 2
set frame.%body.1.x%.%body.1.y%=1
rem 从上下左右四个方向中选择一个方向生成蛇身的其余方块
set /a position=%random%%%4 + 1
for /l %%i in (2,1,%length%) do (
set /a oldindex=%%i - 1
call :otherbody %%i !oldindex!
set frame.!body.%%i.x!.!body.%%i.y!=1
)
goto :eof
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::函数功能:生成蛇头外的其余方块
::函数描述:
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:otherbody
if 1 equ %position% (set /a body.%1.x=!body.%2.x!&set /a body.%1.y=!body.%2.y! - 1)
if 2 equ %position% (set /a body.%1.x=!body.%2.x! - 1&set /a body.%1.y=!body.%2.y!)
if 3 equ %position% (set /a body.%1.x=!body.%2.x!&set /a body.%1.y=!body.%2.y! + 1)
if 4 equ %position% (set /a body.%1.x=!body.%2.x! + 1&set /a body.%1.y=!body.%2.y!)
goto :eof
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::函数功能:生成蛇的食物
::函数描述:随机生成一个方块,判断这个方块是否和蛇身重合,如果重合
::就递归,直到不重合时生成食物
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:createfood
set /a food.x=%random%%%%width% + 1
set /a food.y=%random%%%%width% + 1
for /l %%i in (1,1,%length%) do (
if %food.x% equ !body.%%i.x! (if %food.y% equ !body.%%i.y! goto createfood)
)
set frame.%food.x%.%food.y%=1
goto :eof
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::函数功能:显示整个坐标系
::函数描述:
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:display
cls
echo ┌─────────────────────┐
for /l %%i in (1,1,%width%) do (
set frame= │
for /l %%j in (1,1,%width%) do (
rem 下行最后有两个空格,不可少
if defined frame.%%i.%%j (set frame=!frame!■) else set frame=!frame!
)
set frame=!frame! │
echo !frame!
)
echo └─────────────────────┘
goto :eof
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::函数功能:向前移动一步
::函数描述:
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:goahead
rem 删除原蛇尾的定义
set frame.%body.1.x%.%body.1.y%=
rem 更改蛇身坐标
set /a sublength=%length%-1
for /l %%i in (1,1,%sublength%) do (
set /a new=%%i + 1
call :newbody %%i !new!
)
rem 蛇头新坐标
if 1 equ %position% set /a body.%length%.y-=1
if 2 equ %position% set /a body.%length%.x-=1
if 3 equ %position% set /a body.%length%.y+=1
if 4 equ %position% set /a body.%length%.x+=1
call :newhead
rem 增加蛇头的定义
set frame.!body.%length%.x!.!body.%length%.y!=1
goto :eof
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::函数功能:蛇身移动后的新蛇头
::函数描述:判断是否吃到食物和碰壁
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:newhead
rem 判断是否碰壁
if !body.%length%.x! lss 1 (
set run=
goto :eof
)
if !body.%length%.x! gtr %width% (
set run=
goto :eof
)
if !body.%length%.y! lss 1 (
set run=
goto :eof
)
if !body.%length%.y! gtr %width% (
set run=
goto :eof
)
rem 判断是否碰到自身,只判断蛇头是否与蛇身重合,所以将长度减去1
set /a nohead=%length%-1
for /l %%i in (1,1,%nohead%) do (
if !body.%%i.x! equ !body.%length%.x! (
if !body.%%i.y! equ !body.%length%.y! (
set run=
goto :eof
)
)
)
rem 判断是否吃到食物
if !body.%length%.x! equ %food.x% (
if !body.%length%.y! equ %food.y% (
set /a length+=1
set /a body.!length!.x=%food.x%
set /a body.!length!.y=%food.y%
if 1 equ %position% set /a body.!length!.y-=1
if 2 equ %position% set /a body.!length!.x-=1
if 3 equ %position% set /a body.!length!.y+=1
if 4 equ %position% set /a body.!length!.x+=1
call :createfood
)
)
goto :eof
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::函数功能:移动后的新蛇身
::函数描述:由于牵涉到变量嵌套问题,所以在生成新蛇身时要调用此函数
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:newbody
set /a body.%1.x=!body.%2.x!
set /a body.%1.y=!body.%2.y!
goto :eof
:end
echo again or exit?(y/n)
set /p next=
if y == %next% goto init