SNAKE.BAT 4.1 - An arcade style game using pure batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: SNAKE.BAT 3.4 - An arcade style game using pure batch

#76 Post by foxidrive » 28 Dec 2013 20:16

It gives us some understanding of the processes behind the scenes - thanks.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: SNAKE.BAT 3.4 - An arcade style game using pure batch

#77 Post by Squashman » 28 Dec 2013 20:20

would the command waitfor help with inter process communication. Not available for XP but if you had multiple batch files running on one computer you could send signals back and forth to each using the waitfor command.

I have to admit i have not taken the time to understand how snakey works.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: SNAKE.BAT 3.4 - An arcade style game using pure batch

#78 Post by dbenham » 28 Dec 2013 20:33

Squashman wrote:would the command waitfor help with inter process communication. Not available for XP but if you had multiple batch files running on one computer you could send signals back and forth to each using the waitfor command.

I have to admit i have not taken the time to understand how snakey works.

It wouldn't help for buffering key strokes, but it might be useful to synchronize communication (semaphore). I considered using it instead of a signal file, but I read that some people had problems getting it to behave properly, and also lack of XP support was a problem. NET SEND is available for XP, and I think that worked better.


Dave Benham

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: SNAKE.BAT 3.4 - An arcade style game using pure batch

#79 Post by carlos » 29 Dec 2013 06:29

@dbenham: very thanks for the explanation.

carlsomo
Posts: 91
Joined: 02 Oct 2012 17:21

Re: SNAKE.BAT 3.4 - An arcade style game using pure batch

#80 Post by carlsomo » 30 Dec 2013 02:30

It's amazing to me the optimization you have performed and how fast this thing can run in pure batch mode. All I can say is, "WOW". There is a lot of useful code that can apply to non-gaming situations as I see it. It is not that simple to program a little devil that can sit and wait for a key stroke without using a lot of CPU resources but you seem to have pulled it off, Kudos to you.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: SNAKE.BAT 3.4 - An arcade style game using pure batch

#81 Post by dbenham » 30 Dec 2013 09:15

carlsomo wrote:It's amazing to me the optimization you have performed and how fast this thing can run in pure batch mode. All I can say is, "WOW". There is a lot of useful code that can apply to non-gaming situations as I see it. It is not that simple to program a little devil that can sit and wait for a key stroke without using a lot of CPU resources but you seem to have pulled it off, Kudos to you.

Thanks for the high praise. I am proud of the work.

The controller process indeed uses negligible CPU resources since it spends most of its time within the XCOPY command waiting for a key press, consuming zero CPU.

But the game process does use a lot of CPU, I think necessarily. When a game is in progress, it is in an infinite FOR loop that never waits for input, so it consumes 100% of a CPU core. My machine is a quad core, and the task manager reports that the game process uses 25% of my CPU resources (100% of 1 core). All the other processes, including the console host, use negligible resources.

When in the menu system, waiting for user input, the game process uses between 6% and 9% of my CPU resources, and the console host uses between 7% and 9% of the game resources. All other processes use virtually 0%. I'm not sure why the console host behaves this way. The game process doesn't use 100% of a core while in the menu because it is in a GOTO loop. Significant time is spent performing file IO while reading nothing from the end of the key file, and while searching for the GOTO label.


Dave Benham

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: SNAKE.BAT 3.4 - An arcade style game using pure batch

#82 Post by einstein1969 » 30 Dec 2013 14:25

Dave,

in future you must insert a configuration menu for delay/sleep/passive_wait for timing parameter and leave the user choice what type use.

example:

Timing configuration:

Sleep control: <None> <comPort> <Router> <ExternalProgram> <Wscript/Cscript> <....>

At this time the pure dos is <None> (i think)

I think that every dos game engine must have a similar configuration for reducing cpu usage in loops.

einstein1969

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: SNAKE.BAT 3.4 - An arcade style game using pure batch

#83 Post by foxidrive » 30 Dec 2013 22:27

einstein1969 wrote:Dave,

in future you must insert a configuration menu for delay/sleep/passive_wait for timing parameter and leave the user choice what type use.

example:

Timing configuration:

Sleep control: <None> <comPort> <Router> <ExternalProgram> <Wscript/Cscript> <....>

At this time the pure dos is <None> (i think)

I think that every dos game engine must have a similar configuration for reducing cpu usage in loops.

einstein1969


Snake runs blindingly fast if you select that option, and it's not going to drag down any CPU with more than one core.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: SNAKE.BAT 3.4 - An arcade style game using pure batch

#84 Post by dbenham » 30 Dec 2013 23:21

einstein1969 wrote:Dave,

in future you must insert a configuration menu for delay/sleep/passive_wait for timing parameter and leave the user choice what type use.

example:

Timing configuration:

Sleep control: <None> <comPort> <Router> <ExternalProgram> <Wscript/Cscript> <....>

At this time the pure dos is <None> (i think)

I think that every dos game engine must have a similar configuration for reducing cpu usage in loops.

einstein1969


Inserting any type of sleep using standard commands available to batch would have a drastic negative impact on the game. The only universal delay I'm aware of is PING, and the minimum delay is ~1 second. But the slowest interval I use in snake is 0.2 second. The fastest interval is 0.03 second. It is also important for the timing to be precise in order to achieve smooth animation.

Feel free to modify SNAKE.BAT to make your own version with a sleep mechanism. But that is not something I will ever add.


Dave Benham

ashdnazg
Posts: 2
Joined: 02 Jan 2014 07:48

Re: SNAKE.BAT 3.4 - An arcade style game using pure batch

#85 Post by ashdnazg » 02 Jan 2014 08:16

Hey,
Been visiting these forums once in a while for quite some time, but never got around to post until now.

First of all, I must say this is certainly the most advanced Batch place I know. (Macros with arguments? Self running Labels? you guys are totally bonkers :D )
I got here after coming up with the idea of batch libraries (call :%*) and actually found out I'm waaay behind, so obviously, I learned a lot, and I think now I have the opportunity to contribute something back (that's why I had to post).

While this snake game is wonderfully coded, it has the drawback of the nasty flickering screen, but I think that can be remedied.

I also encountered the same issue when I participated in a game jam, and decided to use Batch as my language of choice.
The game itself is quite terrible, and not very fun, but it does use some interesting concepts, specifically:
1) Using pipes to run scripts in parallel (yep, multi-core batch programming).
2) Having a temporary file as a display buffer (and outputting it with 'type').

Github link: https://github.com/ashdnazg/ld26
Architecture post: http://www.ludumdare.com/compo/2013/05/ ... cture-101/

Many thanks!

einstein1969
Expert
Posts: 941
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: SNAKE.BAT 3.4 - An arcade style game using pure batch

#86 Post by einstein1969 » 02 Jan 2014 10:01

ashdnazg wrote:Hey,
Been visiting these forums once in a while for quite some time, but never got around to post until now.

First of all, I must say this is certainly the most advanced Batch place I know. (Macros with arguments? Self running Labels? you guys are totally bonkers :D )
I got here after coming up with the idea of batch libraries (call :%*) and actually found out I'm waaay behind, so obviously, I learned a lot, and I think now I have the opportunity to contribute something back (that's why I had to post).

While this snake game is wonderfully coded, it has the drawback of the nasty flickering screen, but I think that can be remedied.

I also encountered the same issue when I participated in a game jam, and decided to use Batch as my language of choice.
The game itself is quite terrible, and not very fun, but it does use some interesting concepts, specifically:
1) Using pipes to run scripts in parallel (yep, multi-core batch programming).
2) Having a temporary file as a display buffer (and outputting it with 'type').

Github link: https_//github.com/ashdnazg/ld26
Architecture post: http_//www.ludumdare.com/compo/2013/05/07/combat-debriefing-or-batch-game-architecture-101/

Many thanks!


I had the same idea too. But I did not put into practice.
I'm glad to know that someone had the same idea and has already worked in this direction!

einstein1969

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: SNAKE.BAT 3.4 - An arcade style game using pure batch

#87 Post by dbenham » 02 Jan 2014 11:50

ashdnazg wrote:Hey,
While this snake game is wonderfully coded, it has the drawback of the nasty flickering screen, but I think that can be remedied.

I also encountered the same issue when I participated in a game jam, and decided to use Batch as my language of choice.
The game itself is quite terrible, and not very fun, but it does use some interesting concepts, specifically:
1) Using pipes to run scripts in parallel (yep, multi-core batch programming).
2) Having a temporary file as a display buffer (and outputting it with 'type').

Interesting implementation of a batch game. I'm impressed.

If you look at my SNAKE.BAT code (and the explanations I've already written describing how it works), then you will see that I am already addressing your concerns, although I've implemented it in a different way.

My code already is doing parallel processing. But I am using START instead of pipes. I have once process for input, and another for logic and display. I don't need parrallel processes to improve speed - I'm already getting 33 frames per second. The 2nd process is only needed to read key presses in a non blocking way.

I also have a screen buffer to minimize flicker. Instead of using a text file, I store the screen image in variables, one variable per line. If you look at my draw macro, you will see that it simply loops through the screen variables using a FOR /L loop and echos each line.

I went ahead and modified my draw macro to echo each screen to a text file, and then use TYPE to display the screen as you did. That actually made the flicker much worse.

When I play your game, I actually see flicker in the top wall.

I don't think it is possible to eliminate flicker if the display requires a CLS before each refresh. At some point the entire console will logically be entirely black, which will always contribute some amount of flicker.

The amount of flicker is a complex relationship between the frame rate of the game, coupled with the refresh rate of the display monitor.

The only way to eliminate flicker is to somehow reposition the cursor position to prior lines on the screen, and write new information without clearing the rest of the screen. But there is no cabability to do that with native batch on a Windows machine.


Dave Benham

ashdnazg
Posts: 2
Joined: 02 Jan 2014 07:48

Re: SNAKE.BAT 3.4 - An arcade style game using pure batch

#88 Post by ashdnazg » 02 Jan 2014 12:02

Seems like I should've read more of your code/this thread before posting... :oops:

Accept my sincere apologies!
(I'll make sure to actually test any further suggestions before posting them)

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: SNAKE.BAT 3.4 - An arcade style game using pure batch

#89 Post by dbenham » 03 Feb 2014 22:05

Version 3.5 is now available in the first post of this thread.

There are 2 major enhancements:

1) The growth rate of the snake is now configurable to any value from 1 to 10, with a dafault value of 1 segment growth per food eaten. The higher growth rates allow the game to get interesting much sooner. The growth rate option is available within the Graphic options menu. High scores are maintained independently for each growth rate played.

Addition of the growth rate option required a modification to the saved game format. SNAKE 3.5 will automatically update existing saved games to the new format when it is launched.

2) I've added support for Aacini's CursorPos.exe utility so as to eliminate the irritating screen flicker that results from the CLS during each screen refresh. CursorPos.exe allows the cursor position to be reset to the top left corner without having to clear the screen.

The game will continue to play as before with screen flicker if CursorPos.exe is not present. I preserve this functionality so that the game can truly operate as pure batch using only internal and native external commands. But if you are willing to "cheat" and use a non-native external utility, then the game play is dramatically improved by simply placing a copy of CursorPos.exe in the same folder as SNAKE.BAT.

The MakeCursorPos.bat script below can create the CursorPos.exe utility. The script combines code from two Aacini posts: Re: modern reply.exe created using only batch, and Advanced Batch features via auxiliary .exe programs.

Code: Select all

@echo off
call :ExtractBinaryFile CursorPos.exe
exit /b


rem Extract Binary File from hexadecimal digits placed in a "resource" in this .bat file

:ExtractBinaryFile filename.ext[.cab]
setlocal EnableDelayedExpansion
ECHO Creating %1, please wait...
set "start="
set "end="
for /F "tokens=1,3 delims=:=>" %%a in ('findstr /N /B "</*resource" "%~F0"') do (
   if not defined start (
      if "%%~b" equ "%~1" set start=%%a
   ) else if not defined end set end=%%a
)

(for /F "skip=%start% tokens=1* delims=:" %%a in ('findstr /N "^" "%~F0"') do (
   if "%%a" == "%end%" goto decodeHexFile
   echo %%b
)) > "%~1.hex"
:decodeHexFile

rem Modified code based on :genchr subroutine
type nul > t.tmp
(for /F "usebackq" %%a in ("%~1.hex") do (
   set input=%%a
   set i=0
   for /L %%I in (0,2,120) do for %%i in (!i!) do if "!input:~%%i,1!" neq "" (
      set hex=!input:~%%i,2!
      set /A i+=2
      if "!hex:~0,1!" neq "[" (
         set /A chr=0x!hex!
         if not exist !chr!.chr call :genchr !chr!
         type !chr!.chr
      ) else (
         for /L %%J in (1,1,5) do for %%i in (!i!) do if "!input:~%%i,1!" neq "]" (
            set "hex=!hex!!input:~%%i,1!"
            set /A i+=1
         )
         if not exist 0.chr call :genchr 0
         for /L %%J in (1,1,!hex:~1!) do type 0.chr
         set /A i+=1
      )
   )
)) > "%~1"
del *.chr
del t.tmp temp.tmp
del "%~1.hex"

rem Expand created file if extension is .cab
set "filename=%~1"
if /I "%filename:~-4%" equ ".cab" (
   expand "%filename%" "%filename:~0,-4%" > NUL
   del "%filename%"
)

exit /B


:genchr
REM This code creates one single byte. Parameter: int
REM Teamwork of carlos, penpen, aGerman, dbenham
REM Tested under Win2000, XP, Win7, Win8
set "options=/d compress=off /d reserveperdatablocksize=26"
if %~1 neq 26 (
   makecab %options% /d reserveperfoldersize=%~1 t.tmp %~1.chr > nul
   type %~1.chr | ( (for /l %%N in (1,1,38) do pause)>nul & findstr "^" > temp.tmp )
   >nul copy /y temp.tmp /a %~1.chr /b
) else (
   copy /y nul + nul /a 26.chr /a >nul
)
exit /B


<resource id="CursorPos.exe">
4D5A900003[3]04[3]FFFF[2]B8[7]40[35]B0[3]0E1FBA0E00B409CD21B8014CCD21546869732070726F6772616D2063616E6E6F74
2062652072756E20696E20444F53206D6F64652E0D0D0A24[7]55B5B8FD11D4D6AE11D4D6AE11D4D6AE9FCBC5AE18D4D6AEED
F4C4AE13D4D6AE5269636811D4D6AE[8]5045[2]4C010200EB84E24F[8]E0000F010B01050C0002[3]02[7]10[3]10[3]20[4]40[2]10
[3]02[2]04[7]04[8]30[3]02[6]03[5]10[2]10[4]10[2]10[6]10[11]1C20[2]28[84]20[2]1C[27]2E74657874[3]4201[3]10[3]02[3]02[14]20[2]60
2E7264617461[2]F6[4]20[3]02[3]04[14]40[2]40[8]E806[3]50E81301[2]558BEC83C4E06AF5E81201[2]8945FC8D45E650FF75FCE8
FD[3]668B45EC668945E4E8BC[3]E8DB[3]803E0075058B45EAEB5C803E3D750646E8C6[3]668B4DEAE84A[3]8945EAE8B5[3]803E
007418803E2C750646E8A5[3]668B4DE4E829[3]668945EC8B5DEA53FF75FCE8AE[3]8D45E650536A018D45E350FF75FCE895[3]0F
B645E3C9C333C032DB33D28A164680FA2B740880FA2D750980CB0280CB018A164680FA30720F80FA39770A80EA306BC00A03
C2EBE9F6C301740BF6C302740366F7D86603C14EC3CCCCCCCCCCCCCCCCCCCCCCCCCCE847[3]8BF08A06463C2275098A06463C
2275F9EB0C8A06463C20740484C075F54EC38A06463C2074F94EC3CCFF2514204000FF2500204000FF2504204000FF250820
4000FF250C204000FF25102040[191]6E20[2]8C20[2]9C20[2]BA20[2]D620[2]6020[6]4420[10]E820[3]20[22]6E20[2]8C20[2]9C20[2]BA
20[2]D620[2]6020[6]9B004578697450726F6365737300F500476574436F6E736F6C6553637265656E427566666572496E666F
[2]6A0147657453746448616E646C65[2]380252656164436F6E736F6C654F757470757443686172616374657241006D025365
74436F6E736F6C65437572736F72506F736974696F6E[2]E600476574436F6D6D616E644C696E6541006B65726E656C33322E
646C6C[268]
</resource>


Dave Benham

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: SNAKE.BAT 3.5 - An arcade style game using pure batch

#90 Post by carlos » 04 Feb 2014 00:19

I test the showcursor and works very well using windows 7.
Please, @Aacini: you can post your utilities with source code and a license for use it?

Post Reply