SNAKE.BAT 4.1 - An arcade style game using pure batch
Moderator: DosItHelp
Re: SNAKE.BAT - An arcade style game using pure batch
Thanks miskox and aGerman
I've updated the code in the initial post.
Dave Benham
I've updated the code in the initial post.
Dave Benham
Re: SNAKE.BAT - An arcade style game using pure batch
That's neat Dave. I jumped in and run it - picked crawl speed and started playing.
Snake is a favourite game of mine...
I struck something odd though. when I reached this score no more food appeared. I moved round for 30 seconds or so and I couldn't see a + anywhere on the screen.
Speed=Crawl
Score=6
Edit: I played some more and that didn't reoccur.
This speed is a good one on my I7 3.2 GHz CPU and is comfortable to play. Here's my current high score.
http://i39.tinypic.com/b6oj8g.png
Snake is a favourite game of mine...
I struck something odd though. when I reached this score no more food appeared. I moved round for 30 seconds or so and I couldn't see a + anywhere on the screen.
Speed=Crawl
Score=6
Edit: I played some more and that didn't reoccur.
This speed is a good one on my I7 3.2 GHz CPU and is comfortable to play. Here's my current high score.
http://i39.tinypic.com/b6oj8g.png
Re: SNAKE.BAT - An arcade style game using pure batch
Yea, crawl is a good speed for running up a high score. I cracked 100 once, though I have no screen shot to prove it
I too ran into disappearing food once, many plays ago. I've been through the code logic repeatedly and simply cannot find the source of that problem. I've played a great many times since without running into the problem. I'd love it if someone could discover the source of the bug, but I'm not very optimistic - it is so intermittent.
Dave Benham
I too ran into disappearing food once, many plays ago. I've been through the code logic repeatedly and simply cannot find the source of that problem. I've played a great many times since without running into the problem. I'd love it if someone could discover the source of the bug, but I'm not very optimistic - it is so intermittent.
Dave Benham
Re: SNAKE.BAT - An arcade style game using pure batch
I'm not sure, but i think the error is produced because you do that in the main loop:
So if F=(!random!%%emptyCnt)*6+1 points to the maximum empty element, there is none because you shortened the list by one while setting the new food.
The plot macro then operates on the variables part2 and line, so no other problem occurs as part2 is overwritten the next time plot is used, and line is never used.
penpen
Code: Select all
%=== remove the new head location from the empty list ===%
for %%X in ("!X!") do for %%Y in ("!Y!") do set "empty=!empty:#%%~X %%~Y=!"
:: ...
set /a "score+=1, F=(!random!%%emptyCnt)*6+1, emptyCnt-=1"
:: ...
%=== add the former tail position to the empty list ===%
set "empty=!empty!#!TX! !TY!"
The plot macro then operates on the variables part2 and line, so no other problem occurs as part2 is overwritten the next time plot is used, and line is never used.
penpen
Re: SNAKE.BAT - An arcade style game using pure batch
Nearly forgotten:
I downloaded the choice from http://winsupport.org/utilities/freedos-choice.html as it was not present on my xp home, and the game then prints:
The tests you do to check the existance of choice produces:
In the upper case /t 0 and /d are not supported and choice sets an errorlevel of 0, and in the lower case the -1 produces the error and the errorlevel is set to 1.
I changed the code to:
and it works fine.
penpen
I downloaded the choice from http://winsupport.org/utilities/freedos-choice.html as it was not present on my xp home, and the game then prints:
Code: Select all
ERROR: This game requires the CHOICE command, but it is missing.
Game aborted. :(
usw.
The tests you do to check the existance of choice produces:
Code: Select all
Z:\>choice /c:yn /t 0 /d y
CHOICE:Incorrect timeout syntax. Expected form Tc,nn or T:c,nn
Z:\>choice /c:yn /t:y,-1
CHOICE:Incorrect timeout syntax. Expected form Tc,nn or T:c,nn
In the upper case /t 0 and /d are not supported and choice sets an errorlevel of 0, and in the lower case the -1 produces the error and the errorlevel is set to 1.
I changed the code to:
Code: Select all
: ...
set "choice="
2>nul >nul choice /c:yn /t 0 /d y
if errorlevel 1 if not errorlevel 2 (
set "choice=choice /cs"
)
2>nul >nul choice /c:yn /t:y,1
if errorlevel 1 if not errorlevel 2 set "choice=choice /s"
if not defined choice (
: ...
and it works fine.
penpen
Re: SNAKE.BAT - An arcade style game using pure batch
I remember that SNAKE game uses just two control keys: turn right and turn left, and that the turn direction was relative to snake movement. As a matter of fact, this behaviour was one of the most funny details of SNAKE game. I used such control in my SNAKE.BAT program (posted more than a year ago, look for it below program #6), that I wrote mainly as an example program on how to use my Window.exe, GetKey.exe, CursorPos.exe and Show.exe auxiliary .exe programs.
Antonio
Antonio
Re: SNAKE.BAT - An arcade style game using pure batch
@penpen - Woohooo
You have killed the last known bug. I confirmed that my failure to decrement the emptyCnt prior to picking a random number was the source of the disappearing food. I fixed the code by using:
Also, thanks for reporting the problem with the downloaded 16 bit CHOICE version. The 32 bit version uses -1 for no wait, but I guess the 16 bit version doesn't have that option. I couldn't test because all I have are 64 bit machines. I've updated the 2nd CHOICE test to wait for 1 second, as you suggested.
Now I can dare to hope that there are no more bugs
Thanks again to penpen, miskox, and aGerman for help in squashing the bugs.
Dave Benham
You have killed the last known bug. I confirmed that my failure to decrement the emptyCnt prior to picking a random number was the source of the disappearing food. I fixed the code by using:
Code: Select all
set /a "score+=1, F=(!random!%%(emptyCnt-=1))*6+1"
Also, thanks for reporting the problem with the downloaded 16 bit CHOICE version. The 32 bit version uses -1 for no wait, but I guess the 16 bit version doesn't have that option. I couldn't test because all I have are 64 bit machines. I've updated the 2nd CHOICE test to wait for 1 second, as you suggested.
Now I can dare to hope that there are no more bugs
Thanks again to penpen, miskox, and aGerman for help in squashing the bugs.
Dave Benham
Re: SNAKE.BAT - An arcade style game using pure batch
I can't get it to work at all. Windows 7 32bit.
It gets stuck at Press any alpha-numeric key to start...
It gets stuck at Press any alpha-numeric key to start...
Re: SNAKE.BAT - An arcade style game using pure batch
Squashman wrote:I can't get it to work at all. Windows 7 32bit.
It gets stuck at Press any alpha-numeric key to start...
I may have spoken too soon about no bugs
I have seen some intermittent issues where the game and controller get out of synch in some way and I have no control. The rare times that happens, I simply kill the window and start afresh with a new console window.
But the program fails entirely as you describe when I attempt to run it at work on a shared network drive. At work, I must run it on a local hard drive.
Dave Benham
Re: SNAKE.BAT - An arcade style game using pure batch
Aacini wrote:I remember that SNAKE game uses just two control keys: turn right and turn left, and that the turn direction was relative to snake movement. As a matter of fact, this behaviour was one of the most funny details of SNAKE game. I used such control in my SNAKE.BAT program (posted more than a year ago, look for it below program #6), that I wrote mainly as an example program on how to use my Window.exe, GetKey.exe, CursorPos.exe and Show.exe auxiliary .exe programs.
Antonio
I remember scanning that post, but I generally avoid downloaded non-script based CMD extensions. Your utilities sure make development easier, but my goal was to build an arcade came using pure native batch commands.
I realize my program does not run on XP without downloading a non-standard CHOICE command, so I haven't met my goal for XP. But that is the best I could do. My SNAKE.BAT should work on any Windows platform from Vista onward, without any non-native extensions.
I might create a modified version of my SNAKE.BAT that uses only left and right controls. But I think I'm ready to give it a rest for now.
Dave Benham
Re: SNAKE.BAT - An arcade style game using pure batch
dave, thanks for share you game.
time ago i wrote some batch games and i remember that there are two syntax for choice.
the choice.com and choice.exe that runs in windows 98 and the choice.exe from windows 2003.
time ago i wrote some batch games and i remember that there are two syntax for choice.
the choice.com and choice.exe that runs in windows 98 and the choice.exe from windows 2003.
Re: SNAKE.BAT - An arcade style game using pure batch
Hi Dave,
there is an pure batch workaround for the choice command, it even works with XP.
It's even better than choice, as it accepts more characters.
The only drawback is the missing timeout possibility
hope it helps
jeb
there is an pure batch workaround for the choice command, it even works with XP.
It's even better than choice, as it accepts more characters.
The only drawback is the missing timeout possibility
Code: Select all
:GetKey
set "key="
for /F "usebackq delims=" %%L in (`xcopy /L /w "%~f0" "%~f0" 2^>NUL`) do (
if not defined key set "key=%%L"
)
set "key=%key:~-1%"
exit /b
hope it helps
jeb
Re: SNAKE.BAT - An arcade style game using pure batch
I remember this routine for detect choice and the switch for the different sintaxys:
for detect:
and for switch syntax:
the variable movement define the default key pressed, this variable can change every time.
you can see the difference, for example for show a message the 98 version only enclose a message in quotes, the 2003 version need the switch /m
for time and default keys 2003 version uses /d default /t seconds
98 version uses /t:default,seconds
and for specify valid keys: 98 version uses /c:keys and 2003 uses: /c keys
for detect:
Code: Select all
SETLOCAL
(SET EXISTCHOICE=)
SET "FOLDER_TO_SEARCH=.\;%PATH%"
FOR %%A IN (CHOICE.EXE) DO (
SET "EXISTCHOICE=%%~$FOLDER_TO_SEARCH:A"
)
IF NOT DEFINED EXISTCHOICE (
SET "FOLDER_TO_SEARCH=%PATH%"
FOR %%A IN (CHOICE.COM) DO (
SET "EXISTCHOICE=%%~$FOLDER_TO_SEARCH:A"
)
)
IF NOT DEFINED EXISTCHOICE (
ECHO CHOICE NOT EXIST
)
and for switch syntax:
Code: Select all
(SET KEYS=WASD)
(SET /A SECONDS=2)
CHOICE /C 0 /N /D 0 /T 1 <NUL >NUL 2>&1
IF NOT ERRORLEVEL 255 (
REM CHOICE VERSION 2003 .EXE
SET "ACTION1=CHOICE.EXE /C %KEYS% /N /D "
SET "ACTION2= /T %SECONDS%"
SET "AGAIN=CHOICE.EXE /C %KEYS%YN /N /M "PLAY AGAIN[Y,N]?""
) ELSE (
REM CHOICE VERSION 98 .COM AND .EXE
SET "ACTION1=CHOICE /C:%KEYS% /N /T:"
SET "ACTION2=,%SECONDS%"
SET "AGAIN=CHOICE /C:%KEYS%YN /N "PLAY AGAIN[Y,N]?""
)
:ACTION
%ACTION1%!MOVEMENT!%ACTION2% >NUL 2>&1
the variable movement define the default key pressed, this variable can change every time.
you can see the difference, for example for show a message the 98 version only enclose a message in quotes, the 2003 version need the switch /m
for time and default keys 2003 version uses /d default /t seconds
98 version uses /t:default,seconds
and for specify valid keys: 98 version uses /c:keys and 2003 uses: /c keys
Re: SNAKE.BAT - An arcade style game using pure batch
I realize my program does not run on XP without downloading a non-standard CHOICE command, so I haven't met my goal for XP.
I guess that could be solved. There we fiddled with Carlos' XCOPY solution for a hidden input. That method could be easily changed to achieve a CHOICE behaviour (without timeout of course).
Code: Select all
@echo off &setlocal
<nul set /p "=Yes or No [yn]: "
call :choice yn
echo Option #%errorlevel% was chosen.
pause
goto :eof
:choice
setlocal DisableDelayedExpansion
set "n=0" &set "c=" &set "e=" &set "map=%~1"
if not defined map endlocal &exit /b 0
for /f "delims=" %%i in ('2^>nul xcopy /lw "%~f0" "%~f0"') do if not defined c set "c=%%i"
set "c=%c:~-1%"
if defined c (
for /f delims^=^ eol^= %%i in ('cmd /von /u /c "echo(!map!"^|find /v ""^|findstr .') do (
set /a "n += 1" &set "e=%%i"
setlocal EnableDelayedExpansion
if /i "!e!"=="!c!" (
echo(!c!
for /f %%j in ("!n!") do endlocal &endlocal &exit /b %%j
)
endlocal
)
)
endlocal &goto choice
There is another interresting technique used in that code. FIND interprets the 0x00 character of a unicode string as linebreak. I used that behaviour to separate the allowed keys.
Regards
aGerman
EDIT Seems I'm late
Re: SNAKE.BAT - An arcade style game using pure batch
why is needed the /L parameter using /X parameter in xcopy?