Skip to content

Instantly share code, notes, and snippets.

@timlg07
Created March 8, 2021 19:43
Show Gist options
  • Save timlg07/1610299a4252e14a08f31899ef54f2b2 to your computer and use it in GitHub Desktop.
Save timlg07/1610299a4252e14a08f31899ef54f2b2 to your computer and use it in GitHub Desktop.
@echo off
setlocal enableDelayedExpansion
title Batch Game Demo
set /a width = 20, height = 15
set /a windowWidth = width + 1 + (2 * 2)
set /a windowHeight = height + 6
set /a speed = 1
set /a x = width / 2, y = height / 2
set "margin= "
set "playerSymbol=@"
set "backgroundSymbol=."
mode con cols=%windowWidth% lines=%windowHeight%
:game_loop
call :render
call :awaitInput
goto game_loop
:render
cls
echo;
echo:%margin%Use WASD to %margin%
echo:%margin%walk around.%margin%
echo;
for /L %%y in (0 1 %height%) do (
set "line=%margin%"
for /L %%x in (0 1 %width%) do (
if "(%%y, %%x)" equ "(%y%, %x%)" (
set "line=!line!%playerSymbol%"
) else (
set "line=!line!%backgroundSymbol%"
)
)
echo:!line!%margin%
)
exit /b
:awaitInput
choice /c WASD > nul
set /a keyCode = %errorlevel%
if %keyCode% equ 1 (
rem 1 = W = UP
if %y% gtr 0 set /a y -= speed
) else if %keyCode% equ 2 (
rem 2 = A = LEFT
if %x% gtr 0 set /a x -= speed
) else if %keyCode% equ 3 (
rem 3 = S = DOWN
if %y% lss %height% set /a y += speed
) else if %keyCode% equ 4 (
rem 4 = D = RIGHT
if %x% lss %width% set /a x += speed
) else (
goto awaitInput
)
exit /b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment