Advertisement
Matthew_Horvath

Pong.bat v2.5 by Matthew Horvath

May 12th, 2016
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 14.64 KB | None | 0 0
  1. :::::::::::::::::::::::::::::::
  2. ::                           ::
  3. :: PONG in pure Batch Script ::
  4. ::     Save as Pong.bat      ::
  5. ::                           ::
  6. ::        Created by:        ::
  7. ::      Matthew Horvath      ::
  8. ::                           ::
  9. ::           v2.5            ::
  10. ::                           ::
  11. :::::::::::::::::::::::::::::::
  12.  
  13.  
  14. :: NOTE: During gameplay, if any key is held for too long, the game will
  15. :: no longer work. The simple solution to fix this is to hit the ENTER
  16. :: key.
  17.  
  18. :: ANOTHER NOTE: It is possible to beat the CP. You just have to get the
  19. :: ball go right above/below a corner on the CP's side. The ball does
  20. :: need to bounce off of the wall closest to the 'chosen' corner.
  21.  
  22. :: Basic set up for every batch file
  23.     @echo off
  24.     setlocal enableextensions enabledelayedexpansion
  25.  
  26. :: Set window title
  27.     title Pong
  28.  
  29. :: Finds the set color (see end of batch file) and displays help message for "color" command if invalid color has been set
  30.     for /f "delims=" %%p in (Pong.bat) do set "color=%%p"
  31.     color !color!
  32.     mode con cols=28 lines=13
  33.  
  34. :: Checks if it started up for the game (see ":setup" and ":delete" for more)
  35.     if /i "%3"=="new" set "game=new"
  36.     if /i "%2"=="pvp" set "mode=%2"
  37.     if /i "%2"=="pvcp" set "mode=%2"
  38.     if /i "%2"=="cpvcp" set "mode=%2"
  39.     if /i "%1"=="play" goto %1
  40.     set "credit=By:"
  41.     set banner1=Matthew
  42.     set banner2=Horvath
  43.  
  44. :: Sets up different coloring (see batch label ":colorful" for the rest)
  45.     for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  46.         set "DEL=%%a"
  47.     )
  48.  
  49. :: Go to the main menu
  50. goto menu
  51.  
  52. :menu
  53. :: Clear screen and show options, deletes input.txt if it exists
  54.     if exist input.txt del input.txt
  55.     cls
  56.         echo     ___   __  _   _  __
  57.         echo     ^|__) /  \ ^|\  ^| /  \
  58.         echo     ^|   ^|    ^|^| \ ^|^|  __
  59.         echo     ^|    \__/ ^|  \^| \__/^|
  60.         echo/
  61.         echo          (1) PvP
  62.         echo          (2) PvCP
  63.         echo          (3) CPvCP
  64.         echo          (4) Help
  65.         echo          (5) Color
  66.         echo/
  67.         echo     !credit! !banner1! !banner2!
  68.  
  69. :: Read user input
  70.     choice /c 12345 /n >nul
  71.  
  72. :: Send user to correct part of file
  73.         if !errorlevel! equ 1 (
  74.             set "mode=pvp"
  75.             goto setup
  76.         )
  77.         if !errorlevel! equ 2 (
  78.             set "mode=pvcp"
  79.             goto setup
  80.         )
  81.         if !errorlevel! equ 3 (
  82.             set "mode=cpvcp"
  83.             goto setup
  84.         )
  85.         if !errorlevel! equ 4 goto help
  86.         if !errorlevel! equ 5 goto color
  87.  
  88. :: Go back to top if invalid key is pressed
  89. goto menu
  90.  
  91. :help
  92. :: Clear screen and display help menu
  93.     cls
  94.         echo/
  95.         echo   To play Pong, Player 1
  96.         echo   should use the W (up)
  97.         echo   and S (down) keys to
  98.         echo   move. Player 2 should
  99.         echo   use O (up) and L
  100.         echo   (down) to move. If a
  101.         echo   key is held down too
  102.         echo   long, the game will
  103.         echo   break. If that occurs,
  104.         echo   just press ENTER. X
  105.         echo   quits the game.
  106.  
  107. :: Pauses and waits for any keypress
  108.     pause >nul
  109.  
  110. :: Go back to the main menu
  111. goto menu
  112.  
  113. :color
  114. :: Clear Screen and show color options, in their correct colors
  115.     cls
  116.         echo  Use two keys to choose
  117.         echo  a color. Order: BG/FG
  118.         call :colorful 70 " 0=Black" && call :colorful 08 "  8=Gray" && echo/
  119.         call :colorful 01 " 1=Blue" && call :colorful 09 "   9=Light Blue" && echo/
  120.         call :colorful 02 " 2=Green" && call :colorful 0a "  A=Light Green" && echo/
  121.         call :colorful 03 " 3=Aqua" && call :colorful 0b "   B=Light Aqua" && echo/
  122.         call :colorful 04 " 4=Red" && call :colorful 0c "    C=Light Red" && echo/
  123.         call :colorful 05 " 5=Purple" && call :colorful 0d " D=Light Purple" && echo/
  124.         call :colorful 06 " 6=Yellow" && call :colorful 0e " E=Light Yellow" && echo/
  125.         call :colorful 07 " 7=White" && call :colorful 0f "  F=Bright White" && echo/
  126.  
  127. :: Read user input
  128.     set /p "color=Color: "
  129.  
  130. :: Sets new color and displays help message for "color" command if invalid color has been set (and does it with the correct window size, too!)
  131.     mode con cols=80 lines=25
  132.     color !color!
  133.     mode con cols=28 lines=13
  134.  
  135. :: Save new color for future use
  136.     echo !color!>>Pong.bat
  137.  
  138. :: Return to main menu
  139. goto menu
  140.  
  141. :colorful
  142. :: Too hard to explain, changes the color of specific text (used in color change menu; see ":color")
  143.     echo off
  144.         <nul set /p ".=%DEL%" > "%~2"
  145.         findstr /v /a:%1 /R "^$" "%~2" nul
  146.         del "%~2" > nul 2>&1
  147. exit /b
  148.  
  149. :play
  150. :: Set up the game's basic starting variables
  151. :: Sets players', ball's, and walls' appearences
  152.     set "p1=["
  153.     set "p2=]"
  154.     set "b="
  155.     set "bor=#"
  156.  
  157. :: Sets win limit
  158.     set "win=5"
  159.  
  160. :: 's' sets the players' score
  161.     if /i "!winner!"=="p1" set /a "p1s=!p1s!+1"
  162.     if /i "!winner!"=="p2" set /a "p2s=!p2s!+1"
  163.  
  164. :: 'b' is ball, 'x' and 'y' set its position, 'y' is random
  165.     set /a "by=!random!*8/32767"
  166.     set "bx=12"
  167.  
  168. :: 'y' sets the players' position
  169.     set "p1y=4"
  170.     set "p2y=4"
  171.  
  172. :: Sets player scores to 0 if the game is new
  173.     if /i "!game!"=="new" (
  174.         set "p1s=0"
  175.         set "p2s=0"
  176.     )
  177.  
  178. :: Says the ball can move
  179.     set "move=yes"
  180.  
  181. :: Choose random direction for the ball to go in
  182.     set /a "randver=!random! %%2"
  183.     set /a "randhor=!random! %%2"
  184.         if !randhor! equ 0 set "hor=left"
  185.         if !randhor! equ 1 set "hor=right"
  186.         if !randver! equ 0 set "ver=up"
  187.         if !randver! equ 1 set "ver=down"
  188.  
  189.     for /l %%x in (0 1 23) do (
  190.         for /l %%y in (0 1 7) do (
  191.             set "%%x,%%y= "
  192.         )
  193.     )
  194.     set "0,!p1y!=!p1!"
  195.     set "23,!p2y!=!p2!"
  196.     set "!bx!,!by!=!b!"
  197.         set "goback=yes"
  198.         goto field
  199.  
  200. :back
  201. set "goback=no"
  202.  
  203. :: Pause for a little bit more than one second
  204. ping localhost -n 1 >nul
  205. ping localhost -n 1 >nul
  206. ping localhost -n 1 >nul
  207. ping localhost -n 1 >nul
  208. ping localhost -n 1 >nul
  209. ping localhost -n 1 >nul
  210. ping localhost -n 1 >nul
  211. ping localhost -n 1 >nul
  212. ping localhost -n 1 >nul
  213. ping localhost -n 1 >nul
  214. ping localhost -n 1 >nul
  215. ping localhost -n 1 >nul
  216. ping localhost -n 1 >nul
  217. ping localhost -n 1 >nul
  218. ping localhost -n 1 >nul
  219. ping localhost -n 1 >nul
  220. ping localhost -n 1 >nul
  221. ping localhost -n 1 >nul
  222. ping localhost -n 1 >nul
  223. ping localhost -n 1 >nul
  224. ping localhost -n 1 >nul
  225. ping localhost -n 1 >nul
  226. ping localhost -n 1 >nul
  227. ping localhost -n 1 >nul
  228. ping localhost -n 1 >nul
  229. ping localhost -n 1 >nul
  230. ping localhost -n 1 >nul
  231. ping localhost -n 1 >nul
  232. ping localhost -n 1 >nul
  233. ping localhost -n 1 >nul
  234. ping localhost -n 1 >nul
  235. ping localhost -n 1 >nul
  236.  
  237. :: Check if a player has won
  238. if !p1s! equ !win! goto win
  239. if /i "!mode!"=="pvp" if !p2s! equ !win! goto win
  240. if /i "!mode!"=="pvcp" if !p2s! equ !win! goto lose
  241. if /i "!mode!"=="cpvcp" if !p2s! equ !win! goto win
  242.  
  243. :: START THE LOOP
  244. :loop
  245. :: Check for existence of input file, if it doesn't exist go to the error message. (See ":error" for more.)
  246.     if not exist input.txt goto error
  247.  
  248. :: Resets the playing field
  249.     for /l %%x in (0 1 23) do (
  250.         for /l %%y in (0 1 7) do (
  251.             set "%%x,%%y= "
  252.         )
  253.     )
  254.     if /i "!game!"=="new" (
  255.         set "game=old"
  256.         goto ballpos
  257.     )
  258.  
  259. :: Reads user input and resets player input to void so that players don't move any more than they mean to, also allows pause feature
  260.     if exist input.txt (
  261.         for /f "delims=" %%d in (input.txt) do set "inp=%%d"
  262.         set "inp=!inp:~-1!"
  263.     )
  264.  
  265. :: Tests for X key, which quits the game
  266.     if /i "!inp!"=="x" (
  267.         del input.txt
  268.         taskkill /T /F /FI "WINDOWTITLE eq Pong"
  269.     )
  270.  
  271. :: PvCP
  272.             if /i "!mode!"=="pvcp" (
  273.                 if !bx! gtr 12 (
  274.                     if /i "!hor!"=="right" (
  275.                         if !p2y! gtr !by! set /a "p2y=!p2y!-1"
  276.                         if !p2y! lss !by! set /a "p2y=!p2y!+1"
  277.                         if !bx! lss 23 (
  278.                             if !p2y! equ !by! (
  279.                                 if /i "!ver!"=="up" set /a "p2y=!p2y!+1"
  280.                                 if /i "!ver!"=="down" set /a "p2y=!p2y!-1"
  281.                             )
  282.                         )
  283.                     )
  284.                 )
  285.                 if /i "!hor!"=="left" (
  286.                     if !bx! lss 22 (
  287.                         if !p2y! gtr 4 set /a "p2y=!p2y!-1"
  288.                         if !p2y! lss 4 set /a "p2y=!p2y!+1"
  289.                     )
  290.                 )
  291.             )
  292.  
  293. :: CPvCP
  294.             if /i "!mode!"=="cpvcp" (
  295.                 if !bx! gtr 11 (
  296.                     if /i "!hor!"=="right" (
  297.                         if !p2y! gtr !by! set /a "p2y=!p2y!-1"
  298.                         if !p2y! lss !by! set /a "p2y=!p2y!+1"
  299.                         if !bx! lss 23 (
  300.                             if !p2y! equ !by! (
  301.                                 if /i "!ver!"=="up" set /a "p2y=!p2y!+1"
  302.                                 if /i "!ver!"=="down" set /a "p2y=!p2y!-1"
  303.                             )
  304.                         )
  305.                     )
  306.                 )
  307.                 if /i "!hor!"=="left" (
  308.                     if !bx! lss 22 (
  309.                         if !p2y! gtr 4 set /a "p2y=!p2y!-1"
  310.                         if !p2y! lss 4 set /a "p2y=!p2y!+1"
  311.                     )
  312.                 )
  313.                 if !bx! lss 12 (
  314.                     if /i "!hor!"=="left" (
  315.                         if !p1y! gtr !by! set /a "p1y=!p1y!-1"
  316.                         if !p1y! lss !by! set /a "p1y=!p1y!+1"
  317.                         if !bx! gtr 0 (
  318.                             if !p1y! equ !by! (
  319.                                 if /i "!ver!"=="up" set /a "p1y=!p1y!+1"
  320.                                 if /i "!ver!"=="down" set /a "p1y=!p1y!-1"
  321.                             )
  322.                         )
  323.                     )
  324.                 )
  325.                 if /i "!hor!"=="right" (
  326.                     if !bx! gtr 1 (
  327.                         if !p1y! gtr 4 set /a "p1y=!p1y!-1"
  328.                         if !p1y! lss 4 set /a "p1y=!p1y!+1"
  329.                     )
  330.                 )
  331.             )
  332.  
  333. :: PvP
  334.             if /i "!mode!"=="pvp" (
  335.                 if /i "!inp!"=="o" set /a "p2y=!p2y!+1"
  336.                 if /i "!inp!"=="l" set /a "p2y=!p2y!-1"
  337.             )
  338.             if /i "!inp!"=="w" set /a "p1y=!p1y!+1"
  339.             if /i "!inp!"=="s" set /a "p1y=!p1y!-1"
  340.         echo v>>input.txt
  341.     ) else (goto error)
  342.  
  343. :: Tests if players are out of bounds, sets new positions of players
  344.     if !p1y! gtr 7 set "p1y=7"
  345.     if !p1y! lss 0 set "p1y=0"
  346.     if !p2y! gtr 7 set "p2y=7"
  347.     if !p2y! lss 0 set "p2y=0"
  348.  
  349.     set "0,!p1y!=!p1!"
  350.     set "23,!p2y!=!p2!"
  351.  
  352. :: Check if ball hit walls/players or went into walls; if applicable change ball direction
  353.     if !move! == yes (
  354.         if !ver! == up set /a "by=!by!+1"
  355.         if !ver! == down set /a "by=!by!-1"
  356.         if !hor! == right set /a "bx=!bx!+1"
  357.         if !hor! == left set /a "bx=!bx!-1"
  358.     )
  359.  
  360.     if !by! lss 0 (
  361.         set /a "by=!by!+2"
  362.         set "ver=up"
  363.     )
  364.     if !by! gtr 7 (
  365.         set /a "by=!by!-2"
  366.         set "ver=down"
  367.     )
  368.     if /i "23,!p2y!"=="!bx!,!by!" set "hor=left"
  369.     if /i "0,!p1y!"=="!bx!,!by!" set "hor=right"
  370.  
  371. :ballpos
  372. :: Set ball's position
  373.     set "!bx!,!by!=!b!"
  374.  
  375. :field
  376. :: Clear screen and draw playing field
  377. :: Reminder: cols=28 lines=13
  378.     cls
  379.         echo/
  380.         echo/  Player 1: !p1s!  Player 2: !p2s!
  381.         echo/ !bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!
  382.         echo/ !bor!!0,7!!1,7!!2,7!!3,7!!4,7!!5,7!!6,7!!7,7!!8,7!!9,7!!10,7!!11,7!!12,7!!13,7!!14,7!!15,7!!16,7!!17,7!!18,7!!19,7!!20,7!!21,7!!22,7!!23,7!!bor!
  383.         echo/ !bor!!0,6!!1,6!!2,6!!3,6!!4,6!!5,6!!6,6!!7,6!!8,6!!9,6!!10,6!!11,6!!12,6!!13,6!!14,6!!15,6!!16,6!!17,6!!18,6!!19,6!!20,6!!21,6!!22,6!!23,6!!bor!
  384.         echo/ !bor!!0,5!!1,5!!2,5!!3,5!!4,5!!5,5!!6,5!!7,5!!8,5!!9,5!!10,5!!11,5!!12,5!!13,5!!14,5!!15,5!!16,5!!17,5!!18,5!!19,5!!20,5!!21,5!!22,5!!23,5!!bor!
  385.         echo/ !bor!!0,4!!1,4!!2,4!!3,4!!4,4!!5,4!!6,4!!7,4!!8,4!!9,4!!10,4!!11,4!!12,4!!13,4!!14,4!!15,4!!16,4!!17,4!!18,4!!19,4!!20,4!!21,4!!22,4!!23,4!!bor!
  386.         echo/ !bor!!0,3!!1,3!!2,3!!3,3!!4,3!!5,3!!6,3!!7,3!!8,3!!9,3!!10,3!!11,3!!12,3!!13,3!!14,3!!15,3!!16,3!!17,3!!18,3!!19,3!!20,3!!21,3!!22,3!!23,3!!bor!
  387.         echo/ !bor!!0,2!!1,2!!2,2!!3,2!!4,2!!5,2!!6,2!!7,2!!8,2!!9,2!!10,2!!11,2!!12,2!!13,2!!14,2!!15,2!!16,2!!17,2!!18,2!!19,2!!20,2!!21,2!!22,2!!23,2!!bor!
  388.         echo/ !bor!!0,1!!1,1!!2,1!!3,1!!4,1!!5,1!!6,1!!7,1!!8,1!!9,1!!10,1!!11,1!!12,1!!13,1!!14,1!!15,1!!16,1!!17,1!!18,1!!19,1!!20,1!!21,1!!22,1!!23,1!!bor!
  389.         echo/ !bor!!0,0!!1,0!!2,0!!3,0!!4,0!!5,0!!6,0!!7,0!!8,0!!9,0!!10,0!!11,0!!12,0!!13,0!!14,0!!15,0!!16,0!!17,0!!18,0!!19,0!!20,0!!21,0!!22,0!!23,0!!bor!
  390.         echo/ !bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!!bor!
  391.     if /i "!goback!"=="yes" goto back
  392.  
  393. :: Check for scoring
  394.     if !bx! equ 0 (
  395.         if not "0,!p1y!"=="!bx!,!by!" (
  396.             set "winner=p2"
  397.             goto play
  398.         )
  399.     )
  400.     if !bx! equ 23 (
  401.         if not "23,!p2y!"=="!bx!,!by!" (
  402.             set "winner=p1"
  403.             goto play
  404.         )
  405.     )
  406.  
  407. :: Says the ball can/cannot move the next loop
  408.     if /i "!move!"=="yes" (set "move=no") else (set "move=yes")
  409.  
  410. :: Check for existence of input file, if it doesn't exist go to the error message. (See ":error" for more.)
  411.     if not exist input.txt goto error
  412.  
  413. :: Delay the batch file about 3/4 of a second to increase playability
  414.     ping localhost -n 1 >nul
  415.     ping localhost -n 1 >nul
  416.     ping localhost -n 1 >nul
  417.     ping localhost -n 1 >nul
  418.  
  419. :: Go back to top of ":loop" to continue the game (see ":loop" for more)
  420. goto loop
  421.  
  422. :win
  423. :: Clear screen, display win message to winner, then loop until user exits
  424.     cls
  425.         if "!mode!"=="pvp" echo/      *CONGRATULATIONS*
  426.         if "!mode!"=="cpvcp" echo/       CPvCP  Results:
  427.         if !p1s! equ !win! echo         Player 1 won.
  428.         if !p2s! equ !win! echo         Player 2 won.
  429.         echo/
  430.         echo            Scores:
  431.         echo  Player 1: !p1s!  Player 2: !p2s!
  432.         echo/
  433.         echo   Press the X key to quit.
  434.     goto delete
  435.     timeout /t 5000 /nobreak >nul
  436. goto win
  437.  
  438. :lose
  439. :: Clear screen, display losing message to winner and shames them for losing against an AI, then loop until user exits
  440.     cls
  441.         echo/
  442.         echo    Your lack of skill is
  443.         echo       disappointing...
  444.         echo/
  445.         echo            Scores:
  446.         echo  Player 1: !p1s!  Player 2: !p2s!
  447.         echo/
  448.         echo   Press the X key to quit.
  449.     goto delete
  450.     timeout /t 5000 /nobreak >nul
  451. goto lose
  452.  
  453. :error
  454. :: Clear screen, change color, and display error message, wait for any key to be pressed, then change the color back and quits the game
  455.     color c0
  456.     cls
  457.         echo         ERROR:
  458.         echo 'input.txt' is missing.
  459.         echo Please make sure not to
  460.         echo delete it during
  461.         echo gameplay, as it will
  462.         echo send you to this error
  463.         echo message.
  464.         echo/
  465.         echo Press the X key to
  466.         echo quit.
  467.     goto delete
  468.     timeout /t 5000 /nobreak >nul
  469. goto error
  470.  
  471. :delete
  472.     for /f "delims=" %%g in (input.txt) do set "quit=%%g"
  473.     if "!quit!"=="x" (
  474.         del input.txt
  475.         taskkill /T /F /FI "WINDOWTITLE eq Pong"
  476.     )
  477. goto delete
  478.  
  479.  
  480.  
  481. :: CONTROLLER BELOW
  482. :setup
  483. if exist input.txt del input.txt
  484. for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"
  485. for /F %%a in ('copy /Z "%~F0" nul') do set "CR=%%a"
  486. cd . > input.txt
  487. start "" /b "%~F0" play !mode! new
  488. set "input="
  489. :nextkey
  490.     if not exist input.txt exit /b
  491.     set "key="
  492.     for /f "delims=" %%K in ('xcopy /W "%~F0" "%~F0" 2^>nul') do if not defined key set "key=%%K"
  493.     for /f "delims=" %%z in (input.txt) do set "output1=%%z"
  494.         set "output1=!output1:~-1!"
  495.         if /i "!output1!"==")" del input.txt
  496.         set "output2=!output1:~-5!!output1:~-4!!output1:~-3!!output1:~-2!"
  497.         if /i "!output2!"=="stop" del input.txt
  498.         set "output2=!output1:~-4!!output1:~-3!!output1:~-2!!output1:~-1!"
  499.         if /i "!output2!"=="stop" del input.txt
  500.     if /i "!key:~-1!" equ "!BS!" (
  501.         if defined input set "input=!input:~0,-1!"
  502.     ) else (
  503.         set "input=!input!!key:~-1!"
  504.     )
  505.     set /p "=!input!" > input.txt < nul
  506. goto nextkey
  507.  
  508.  
  509.  
  510. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  511. :: The following lines are color settings. Do not delete them. ::
  512. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  513. 0f
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement