Advertisement
IcarusLives

Batch PONG

Mar 10th, 2017
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 6.27 KB | None | 0 0
  1. @echo off & setlocal enableDelayedExpansion
  2. if "%~1" neq "" goto %~1
  3.  
  4. call :canvas 60 30
  5.  
  6. :: generate ball
  7. set /a "bx=30", "by=15", "bi=!random! %% 2 + 1", "bj=!random! %% 2 + 1"
  8.  
  9. :: generate self
  10. set /a "ml=6"
  11. set /a "mx=1", "my=11", "myl=my + ml"
  12.  
  13.  
  14. "%~F0" Input >out.txt | "%~F0" Engine <out.txt
  15. goto :eof
  16.  
  17. :Engine
  18.    
  19.     for /l %%# in () do (
  20.         set /p "com="
  21.        
  22.         if /i "!com!" equ "W" if !my!  geq 0        set /a "my-=1"
  23.         if /i "!com!" equ "S" if !myl! leq %height% set /a "my+=1"
  24.        
  25.         set /a "bx+=bi"
  26.         set /a "by+=bj"
  27.         set /a "myl=my + ml"
  28.        
  29.         rem Bounce off top, bottom, and right walls
  30.         if !bx! geq %width%  set /a "bx=width",  "bi*=-1"
  31.         if !by! geq %height% set /a "by=height", "bj*=-1"
  32.         if !by! leq 0        set /a "by=0",      "bj*=-1"
  33.        
  34.         rem Detect if we miss the ball
  35.         if !bx! lss 0 (
  36.             if !points! gtr 0 set /a "points-=1"
  37.             set /a "bx=30", "by=15"
  38.             set /a "bi=!random! %% 2 + 1", "bj=!random! %% 2 + 1"
  39.         )
  40.        rem Detect if we hit the ball
  41.         if !bx! equ %mx% if !by! geq !my! if !by! leq !myl! (
  42.             set /a "points+=2", "bi*=-1"
  43.         )
  44.  
  45.         rem This is ball!
  46.         call :plot !bx! !by! O
  47.            
  48.         rem This is me!
  49.         call :plotLine %mx% !my! %mx% !myl!
  50.        
  51.         call :showCanvas
  52.         call :updateCanvas
  53.         echo=W - Move up    S - Move Down       Points = !points!
  54.     )
  55. exit /b
  56.  
  57. :Input
  58.     for /f "tokens=*" %%a in ('choice /c:ws /n') do echo %%a
  59. goto :Input
  60.  
  61. :canvas
  62.     @echo off
  63.         set /a "width=%~1 - 1", "height=%~2 - 1", "_=-1", "conWidth=width + 4", "conHeight=height + 5"
  64.         call :cursorpos
  65.         for /l %%a in (-2,1,%width%) do set "outerBuffer=!outerBuffer!#"
  66.         for /l %%a in (0,1,%width%)  do set "widthBuffer=!widthBuffer! "
  67.         call :updateCanvas
  68.         mode con: cols=%conWidth% lines=%conHeight%
  69.     goto :eof
  70.    
  71.     :updateCanvas
  72.         set "_=-1"
  73.             for /l %%a in (0,1,%height%) do set /a "_+=1" & set "_[!_!]=%widthBuffer%"
  74.         set "_="
  75.     goto :eof
  76.    
  77.     :showCanvas
  78.         "%temp%\cursorpos.exe" 0 0
  79.         echo=%outerBuffer%
  80.         for /l %%a in (0,1,%height%) do echo=#!_[%%a]!#
  81.         echo=%outerBuffer%
  82.     goto :eof
  83. goto :eof
  84.  
  85. :plot x y
  86.     setlocal
  87.         set /a "_x2=%~1 + 1"
  88.         (endlocal
  89.             set "_[%~2]=!_[%~2]:~0,%~1!%~3!_[%~2]:~%_x2%!"
  90.         )
  91. goto :eof
  92.  
  93. :plotLine x0 y0 x1 y1
  94.     set /a "x0=%~1", "y0=%~2", "x1=%~3", "y1=%~4", "dx=x1 - x0", "dy=y1 - y0"
  95.    
  96.     if %dy% lss 0 ( set /a "dy=-dy", "stepy=-1" ) else ( set /a "stepy=1")
  97.     if %dx% lss 0 ( set /a "dx=-dx", "stepx=-1" ) else ( set /a "stepx=1")
  98.     set /a "dx<<=1", "dy<<=1"
  99.  
  100.     if %dx% gtr %dy% (
  101.         set /a "fraction=dy - (dx >> 1)"
  102.         for /l %%x in (%x0%,%stepx%,%x1%) do (
  103.             if !fraction! geq 0 (
  104.                 set /a "y0+=stepy"
  105.                 set /a "fraction-=dx"
  106.             )
  107.             set /a "fraction+=dy"
  108.             if 0 leq %%x if %%x lss %width% if 0 leq !y0! if !y0! lss %width% (
  109.                 call :plot %%x !y0! "|"
  110.             )
  111.         )
  112.     ) else (
  113.         set /a "fraction=dx - (dy >> 1)"
  114.         for /l %%y in (%y0%,%stepy%,%y1%) do (
  115.             if !fraction! geq 0 (
  116.                 set /a "x0+=stepx"
  117.                 set /a "fraction-=dy"
  118.             )
  119.             set /a "fraction+=dx"
  120.             if 0 leq !x0! if !x0! lss %width% if 0 leq %%y if %%y lss %width% (
  121.                 call :plot !x0! %%y "|"
  122.             )
  123.         )
  124.     )
  125.     for %%a in (x0 y0 x1 y1 dx dy stepx stepy fraction) do set "%%a="
  126. goto :eof
  127.  
  128. :cursorpos
  129. if not exist "%temp%\cursorpos.exe" (
  130.     for %%a in (
  131.         "TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  132.         "AAAAAAAAAAAAAAAAsAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5v"
  133.         "dCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABVtbj9EdTWrhHU1q4R1Nau"
  134.         "n8vFrhjU1q7t9MSuE9TWrlJpY2gR1NauAAAAAAAAAABQRQAATAECAOuE4k8AAAAA"
  135.         "AAAAAOAADwELAQUMAAIAAAACAAAAAAAAABAAAAAQAAAAIAAAAABAAAAQAAAAAgAA"
  136.         "BAAAAAAAAAAEAAAAAAAAAAAwAAAAAgAAAAAAAAMAAAAAABAAABAAAAAAEAAAEAAA"
  137.         "AAAAABAAAAAAAAAAAAAAABwgAAAoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  138.         "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  139.         "AAAAAAAAAAAAIAAAHAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC50ZXh0AAAA"
  140.         "QgEAAAAQAAAAAgAAAAIAAAAAAAAAAAAAAAAAACAAAGAucmRhdGEAAPYAAAAAIAAA"
  141.         "AAIAAAAEAAAAAAAAAAAAAAAAAABAAABAAAAAAAAAAADoBgAAAFDoEwEAAFWL7IPE"
  142.         "4Gr16BIBAACJRfyNReZQ/3X86P0AAABmi0XsZolF5Oi8AAAA6NsAAACAPgB1BYtF"
  143.         "6utcgD49dQZG6MYAAABmi03q6EoAAACJRerotQAAAIA+AHQYgD4sdQZG6KUAAABm"
  144.         "i03k6CkAAABmiUXsi13qU/91/OiuAAAAjUXmUFNqAY1F41D/dfzolQAAAA+2RePJ"
  145.         "wzPAMtsz0ooWRoD6K3QIgPotdQmAywKAywGKFkaA+jByD4D6OXcKgOowa8AKA8Lr"
  146.         "6fbDAXQL9sMCdANm99hmA8FOw8zMzMzMzMzMzMzMzMzoRwAAAIvwigZGPCJ1CYoG"
  147.         "RjwidfnrDIoGRjwgdASEwHX1TsOKBkY8IHT5TsPM/yUUIEAA/yUAIEAA/yUEIEAA"
  148.         "/yUIIEAA/yUMIEAA/yUQIEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  149.         "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  150.         "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  151.         "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  152.         "AAAAAAAAAAAAAAAAAAAAAG4gAACMIAAAnCAAALogAADWIAAAYCAAAAAAAABEIAAA"
  153.         "AAAAAAAAAADoIAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAbiAAAIwgAACcIAAA"
  154.         "uiAAANYgAABgIAAAAAAAAJsARXhpdFByb2Nlc3MA9QBHZXRDb25zb2xlU2NyZWVu"
  155.         "QnVmZmVySW5mbwAAagFHZXRTdGRIYW5kbGUAADgCUmVhZENvbnNvbGVPdXRwdXRD"
  156.         "aGFyYWN0ZXJBAG0CU2V0Q29uc29sZUN1cnNvclBvc2l0aW9uAADmAEdldENvbW1h"
  157.         "bmRMaW5lQQBrZXJuZWwzMi5kbGwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  158.         "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  159.         "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  160.         "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  161.         "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  162.         "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  163.     ) do echo %%~a>>cursorpos.txt
  164.    
  165.     certutil -decode cursorpos.txt %temp%\cursorpos.exe
  166.     del /f /a cursorpos.txt
  167. )
  168. goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement