Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- :::::::::::::::::::::::::::::::
- :: ::
- :: PONG in pure Batch Script ::
- :: Save as Pong.bat ::
- :: ::
- :: Created by: ::
- :: Matthew Horvath ::
- :: ::
- :: v2.5 ::
- :: ::
- :::::::::::::::::::::::::::::::
- :: NOTE: During gameplay, if any key is held for too long, the game will
- :: no longer work. The simple solution to fix this is to hit the ENTER
- :: key.
- :: ANOTHER NOTE: It is possible to beat the CP. You just have to get the
- :: ball go right above/below a corner on the CP's side. The ball does
- :: need to bounce off of the wall closest to the 'chosen' corner.
- :: Basic set up for every batch file
- @echo off
- setlocal enableextensions enabledelayedexpansion
- :: Set window title
- title Pong
- :: Finds the set color (see end of batch file) and displays help message for "color" command if invalid color has been set
- for /f "delims=" %%p in (Pong.bat) do set "color=%%p"
- color !color!
- mode con cols=28 lines=13
- :: Checks if it started up for the game (see ":setup" and ":delete" for more)
- if /i "%3"=="new" set "game=new"
- if /i "%2"=="pvp" set "mode=%2"
- if /i "%2"=="pvcp" set "mode=%2"
- if /i "%2"=="cpvcp" set "mode=%2"
- if /i "%1"=="play" goto %1
- set "credit=By:"
- set banner1=Matthew
- set banner2=Horvath
- :: Sets up different coloring (see batch label ":colorful" for the rest)
- for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
- set "DEL=%%a"
- )
- :: Go to the main menu
- goto menu
- :menu
- :: Clear screen and show options, deletes input.txt if it exists
- if exist input.txt del input.txt
- cls
- echo ___ __ _ _ __
- echo ^|__) / \ ^|\ ^| / \
- echo ^| ^| ^|^| \ ^|^| __
- echo ^| \__/ ^| \^| \__/^|
- echo/
- echo (1) PvP
- echo (2) PvCP
- echo (3) CPvCP
- echo (4) Help
- echo (5) Color
- echo/
- echo !credit! !banner1! !banner2!
- :: Read user input
- choice /c 12345 /n >nul
- :: Send user to correct part of file
- if !errorlevel! equ 1 (
- set "mode=pvp"
- goto setup
- )
- if !errorlevel! equ 2 (
- set "mode=pvcp"
- goto setup
- )
- if !errorlevel! equ 3 (
- set "mode=cpvcp"
- goto setup
- )
- if !errorlevel! equ 4 goto help
- if !errorlevel! equ 5 goto color
- :: Go back to top if invalid key is pressed
- goto menu
- :help
- :: Clear screen and display help menu
- cls
- echo/
- echo To play Pong, Player 1
- echo should use the W (up)
- echo and S (down) keys to
- echo move. Player 2 should
- echo use O (up) and L
- echo (down) to move. If a
- echo key is held down too
- echo long, the game will
- echo break. If that occurs,
- echo just press ENTER. X
- echo quits the game.
- :: Pauses and waits for any keypress
- pause >nul
- :: Go back to the main menu
- goto menu
- :color
- :: Clear Screen and show color options, in their correct colors
- cls
- echo Use two keys to choose
- echo a color. Order: BG/FG
- call :colorful 70 " 0=Black" && call :colorful 08 " 8=Gray" && echo/
- call :colorful 01 " 1=Blue" && call :colorful 09 " 9=Light Blue" && echo/
- call :colorful 02 " 2=Green" && call :colorful 0a " A=Light Green" && echo/
- call :colorful 03 " 3=Aqua" && call :colorful 0b " B=Light Aqua" && echo/
- call :colorful 04 " 4=Red" && call :colorful 0c " C=Light Red" && echo/
- call :colorful 05 " 5=Purple" && call :colorful 0d " D=Light Purple" && echo/
- call :colorful 06 " 6=Yellow" && call :colorful 0e " E=Light Yellow" && echo/
- call :colorful 07 " 7=White" && call :colorful 0f " F=Bright White" && echo/
- :: Read user input
- set /p "color=Color: "
- :: 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!)
- mode con cols=80 lines=25
- color !color!
- mode con cols=28 lines=13
- :: Save new color for future use
- echo !color!>>Pong.bat
- :: Return to main menu
- goto menu
- :colorful
- :: Too hard to explain, changes the color of specific text (used in color change menu; see ":color")
- echo off
- <nul set /p ".=%DEL%" > "%~2"
- findstr /v /a:%1 /R "^$" "%~2" nul
- del "%~2" > nul 2>&1
- exit /b
- :play
- :: Set up the game's basic starting variables
- :: Sets players', ball's, and walls' appearences
- set "p1=["
- set "p2=]"
- set "b="
- set "bor=#"
- :: Sets win limit
- set "win=5"
- :: 's' sets the players' score
- if /i "!winner!"=="p1" set /a "p1s=!p1s!+1"
- if /i "!winner!"=="p2" set /a "p2s=!p2s!+1"
- :: 'b' is ball, 'x' and 'y' set its position, 'y' is random
- set /a "by=!random!*8/32767"
- set "bx=12"
- :: 'y' sets the players' position
- set "p1y=4"
- set "p2y=4"
- :: Sets player scores to 0 if the game is new
- if /i "!game!"=="new" (
- set "p1s=0"
- set "p2s=0"
- )
- :: Says the ball can move
- set "move=yes"
- :: Choose random direction for the ball to go in
- set /a "randver=!random! %%2"
- set /a "randhor=!random! %%2"
- if !randhor! equ 0 set "hor=left"
- if !randhor! equ 1 set "hor=right"
- if !randver! equ 0 set "ver=up"
- if !randver! equ 1 set "ver=down"
- for /l %%x in (0 1 23) do (
- for /l %%y in (0 1 7) do (
- set "%%x,%%y= "
- )
- )
- set "0,!p1y!=!p1!"
- set "23,!p2y!=!p2!"
- set "!bx!,!by!=!b!"
- set "goback=yes"
- goto field
- :back
- set "goback=no"
- :: Pause for a little bit more than one second
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- :: Check if a player has won
- if !p1s! equ !win! goto win
- if /i "!mode!"=="pvp" if !p2s! equ !win! goto win
- if /i "!mode!"=="pvcp" if !p2s! equ !win! goto lose
- if /i "!mode!"=="cpvcp" if !p2s! equ !win! goto win
- :: START THE LOOP
- :loop
- :: Check for existence of input file, if it doesn't exist go to the error message. (See ":error" for more.)
- if not exist input.txt goto error
- :: Resets the playing field
- for /l %%x in (0 1 23) do (
- for /l %%y in (0 1 7) do (
- set "%%x,%%y= "
- )
- )
- if /i "!game!"=="new" (
- set "game=old"
- goto ballpos
- )
- :: 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
- if exist input.txt (
- for /f "delims=" %%d in (input.txt) do set "inp=%%d"
- set "inp=!inp:~-1!"
- )
- :: Tests for X key, which quits the game
- if /i "!inp!"=="x" (
- del input.txt
- taskkill /T /F /FI "WINDOWTITLE eq Pong"
- )
- :: PvCP
- if /i "!mode!"=="pvcp" (
- if !bx! gtr 12 (
- if /i "!hor!"=="right" (
- if !p2y! gtr !by! set /a "p2y=!p2y!-1"
- if !p2y! lss !by! set /a "p2y=!p2y!+1"
- if !bx! lss 23 (
- if !p2y! equ !by! (
- if /i "!ver!"=="up" set /a "p2y=!p2y!+1"
- if /i "!ver!"=="down" set /a "p2y=!p2y!-1"
- )
- )
- )
- )
- if /i "!hor!"=="left" (
- if !bx! lss 22 (
- if !p2y! gtr 4 set /a "p2y=!p2y!-1"
- if !p2y! lss 4 set /a "p2y=!p2y!+1"
- )
- )
- )
- :: CPvCP
- if /i "!mode!"=="cpvcp" (
- if !bx! gtr 11 (
- if /i "!hor!"=="right" (
- if !p2y! gtr !by! set /a "p2y=!p2y!-1"
- if !p2y! lss !by! set /a "p2y=!p2y!+1"
- if !bx! lss 23 (
- if !p2y! equ !by! (
- if /i "!ver!"=="up" set /a "p2y=!p2y!+1"
- if /i "!ver!"=="down" set /a "p2y=!p2y!-1"
- )
- )
- )
- )
- if /i "!hor!"=="left" (
- if !bx! lss 22 (
- if !p2y! gtr 4 set /a "p2y=!p2y!-1"
- if !p2y! lss 4 set /a "p2y=!p2y!+1"
- )
- )
- if !bx! lss 12 (
- if /i "!hor!"=="left" (
- if !p1y! gtr !by! set /a "p1y=!p1y!-1"
- if !p1y! lss !by! set /a "p1y=!p1y!+1"
- if !bx! gtr 0 (
- if !p1y! equ !by! (
- if /i "!ver!"=="up" set /a "p1y=!p1y!+1"
- if /i "!ver!"=="down" set /a "p1y=!p1y!-1"
- )
- )
- )
- )
- if /i "!hor!"=="right" (
- if !bx! gtr 1 (
- if !p1y! gtr 4 set /a "p1y=!p1y!-1"
- if !p1y! lss 4 set /a "p1y=!p1y!+1"
- )
- )
- )
- :: PvP
- if /i "!mode!"=="pvp" (
- if /i "!inp!"=="o" set /a "p2y=!p2y!+1"
- if /i "!inp!"=="l" set /a "p2y=!p2y!-1"
- )
- if /i "!inp!"=="w" set /a "p1y=!p1y!+1"
- if /i "!inp!"=="s" set /a "p1y=!p1y!-1"
- echo v>>input.txt
- ) else (goto error)
- :: Tests if players are out of bounds, sets new positions of players
- if !p1y! gtr 7 set "p1y=7"
- if !p1y! lss 0 set "p1y=0"
- if !p2y! gtr 7 set "p2y=7"
- if !p2y! lss 0 set "p2y=0"
- set "0,!p1y!=!p1!"
- set "23,!p2y!=!p2!"
- :: Check if ball hit walls/players or went into walls; if applicable change ball direction
- if !move! == yes (
- if !ver! == up set /a "by=!by!+1"
- if !ver! == down set /a "by=!by!-1"
- if !hor! == right set /a "bx=!bx!+1"
- if !hor! == left set /a "bx=!bx!-1"
- )
- if !by! lss 0 (
- set /a "by=!by!+2"
- set "ver=up"
- )
- if !by! gtr 7 (
- set /a "by=!by!-2"
- set "ver=down"
- )
- if /i "23,!p2y!"=="!bx!,!by!" set "hor=left"
- if /i "0,!p1y!"=="!bx!,!by!" set "hor=right"
- :ballpos
- :: Set ball's position
- set "!bx!,!by!=!b!"
- :field
- :: Clear screen and draw playing field
- :: Reminder: cols=28 lines=13
- cls
- echo/
- echo/ Player 1: !p1s! Player 2: !p2s!
- 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!
- 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!
- 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!
- 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!
- 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!
- 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!
- 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!
- 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!
- 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!
- 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!
- if /i "!goback!"=="yes" goto back
- :: Check for scoring
- if !bx! equ 0 (
- if not "0,!p1y!"=="!bx!,!by!" (
- set "winner=p2"
- goto play
- )
- )
- if !bx! equ 23 (
- if not "23,!p2y!"=="!bx!,!by!" (
- set "winner=p1"
- goto play
- )
- )
- :: Says the ball can/cannot move the next loop
- if /i "!move!"=="yes" (set "move=no") else (set "move=yes")
- :: Check for existence of input file, if it doesn't exist go to the error message. (See ":error" for more.)
- if not exist input.txt goto error
- :: Delay the batch file about 3/4 of a second to increase playability
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- ping localhost -n 1 >nul
- :: Go back to top of ":loop" to continue the game (see ":loop" for more)
- goto loop
- :win
- :: Clear screen, display win message to winner, then loop until user exits
- cls
- if "!mode!"=="pvp" echo/ *CONGRATULATIONS*
- if "!mode!"=="cpvcp" echo/ CPvCP Results:
- if !p1s! equ !win! echo Player 1 won.
- if !p2s! equ !win! echo Player 2 won.
- echo/
- echo Scores:
- echo Player 1: !p1s! Player 2: !p2s!
- echo/
- echo Press the X key to quit.
- goto delete
- timeout /t 5000 /nobreak >nul
- goto win
- :lose
- :: Clear screen, display losing message to winner and shames them for losing against an AI, then loop until user exits
- cls
- echo/
- echo Your lack of skill is
- echo disappointing...
- echo/
- echo Scores:
- echo Player 1: !p1s! Player 2: !p2s!
- echo/
- echo Press the X key to quit.
- goto delete
- timeout /t 5000 /nobreak >nul
- goto lose
- :error
- :: Clear screen, change color, and display error message, wait for any key to be pressed, then change the color back and quits the game
- color c0
- cls
- echo ERROR:
- echo 'input.txt' is missing.
- echo Please make sure not to
- echo delete it during
- echo gameplay, as it will
- echo send you to this error
- echo message.
- echo/
- echo Press the X key to
- echo quit.
- goto delete
- timeout /t 5000 /nobreak >nul
- goto error
- :delete
- for /f "delims=" %%g in (input.txt) do set "quit=%%g"
- if "!quit!"=="x" (
- del input.txt
- taskkill /T /F /FI "WINDOWTITLE eq Pong"
- )
- goto delete
- :: CONTROLLER BELOW
- :setup
- if exist input.txt del input.txt
- for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"
- for /F %%a in ('copy /Z "%~F0" nul') do set "CR=%%a"
- cd . > input.txt
- start "" /b "%~F0" play !mode! new
- set "input="
- :nextkey
- if not exist input.txt exit /b
- set "key="
- for /f "delims=" %%K in ('xcopy /W "%~F0" "%~F0" 2^>nul') do if not defined key set "key=%%K"
- for /f "delims=" %%z in (input.txt) do set "output1=%%z"
- set "output1=!output1:~-1!"
- if /i "!output1!"==")" del input.txt
- set "output2=!output1:~-5!!output1:~-4!!output1:~-3!!output1:~-2!"
- if /i "!output2!"=="stop" del input.txt
- set "output2=!output1:~-4!!output1:~-3!!output1:~-2!!output1:~-1!"
- if /i "!output2!"=="stop" del input.txt
- if /i "!key:~-1!" equ "!BS!" (
- if defined input set "input=!input:~0,-1!"
- ) else (
- set "input=!input!!key:~-1!"
- )
- set /p "=!input!" > input.txt < nul
- goto nextkey
- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- :: The following lines are color settings. Do not delete them. ::
- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- 0f
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement