こんなん
@echo off
setlocal enabledelayedexpansion
REM ----- タイトルを設定:このファイル名
title %~nx0
REM ----- スクリーンの大きさを設定
set /a scrWidth = 36
set /a scrHeight = 21
REM ----- ブロックを生成
set /a blockRow = 35
set /a blockCol = 10
for /L %%A in (1, 1, %blockRow% - 1) do (
for /L %%B in (1, 1, %blockCol% - 1) do (
set /a block[%%A][%%B] = 1
)
)
REM ----- ボールの初期位置を設定
set /a posx = %RANDOM% %% %scrWidth%
set /a posy = %RANDOM% %% %scrHeight%
if %posy% LSS %blockCol% set /a posy = %blockCol% + 1
REM ---- ボールの初期速度を設定
set /a xmod2 = posx %% 2
set /a ymod2 = posy %% 2
if %xmod2% == 0 (set /a verx = 1) else (set /a verx = -1)
if %ymod2% == 0 (set /a very = 1) else (set /a very = -1)
:LOOPTOP
REM ----- スクリーンバッファの初期化
REM ----- 0123456789012345678901234567890123456
set scrBuf[0]=.....................................
set scrBuf[1]=.....................................
set scrBuf[2]=.....................................
set scrBuf[3]=.....................................
set scrBuf[4]=.....................................
set scrBuf[5]=.....................................
set scrBuf[6]=.....................................
set scrBuf[7]=.....................................
set scrBuf[8]=.....................................
set scrBuf[9]=.....................................
set scrBuf[10]=.....................................
set scrBuf[11]=.....................................
set scrBuf[12]=.....................................
set scrBuf[13]=.....................................
set scrBuf[14]=.....................................
set scrBuf[15]=.....................................
set scrBuf[16]=.....................................
set scrBuf[17]=.....................................
set scrBuf[18]=.....................................
set scrBuf[19]=.....................................
set scrBuf[20]=.....................................
set scrBuf[21]=.....................................
REM ----- 次フレームのボールの移動先
set /a nextx = posx + verx
set /a nexty = posy + very
REM ----- ボールのカベ反射をチェック
if %nextx% GTR %scrWidth% (set /a verx = verx * -1 & set /a nextx = posx + !verx!)
if %nexty% GTR %scrHeight% (set /a very = very * -1 & set /a nexty = posy + !very!)
if %nextx% LSS 0 (set /a verx = verx * -1 & set /a nextx = posx + !verx!)
if %nexty% LSS 0 (set /a very = very * -1 & set /a nexty = posy + !very!)
REM ----- ボールの移動先にブロックがいるかチェック
call set checkBlockExist=%%block[%nextx%][%nexty%]%%
if "%checkBlockExist%"=="1" (
set /a block[!nextx!][!nexty!] = 0
REM ----- 周囲のブロックも壊す
set /a nextxp = nextx + 1
set /a nextxm = nextx - 1
set /a nextyp = nexty + 1
set /a nextym = nexty - 1
if !nextxm! GEQ 0 set /a block[!nextxm!][!nexty!] = 0
if !nextym! GEQ 0 set /a block[!nextx!][!nextym!] = 0
set /a block[!nextx!][!nextyp!] = 0
set /a block[!nextxp!][!nexty!] = 0
REM ----- 反転
set /a very = very * -1
)
REM ----- ボールの移動
set /a posx = posx + verx
set /a posy = posy + very
REM ----- ブロックのキャラクタをスクリーンバッファに上書き
for /L %%A in (1, 1, %blockRow% - 1) do (
for /L %%B in (1, 1, %blockCol% - 1) do (
call set checkBlockExist=%%block[%%A][%%B]%%
if "!checkBlockExist!"=="1" (
set /a blxp = %%A + 1
call set scrBuf[%%B]=%%scrBuf[%%B]:~0,%%A%%#%%scrBuf[%%B]:~!blxp!%%
)
)
)
REM ----- ボールのキャラクタをスクリーンバッファに上書き
set /a posxp= posx + 1
call set scrBuf[%posy%]=%%scrBuf[%posy%]:~0,%posx%%%@%%scrBuf[%posy%]:~%posxp%%%
REM ----- スクリーン描画処理
CLS
echo ┌─────────────────────────────────────┐
for /L %%A in (0, 1, %scrHeight%) do (
REM ----- キャラクタを倍化
call set scrBuf[%%A]=%%scrBuf[%%A]:.= %%
call set scrBuf[%%A]=%%scrBuf[%%A]:@=●%%
call set scrBuf[%%A]=%%scrBuf[%%A]:#=■%%
call echo │%%scrBuf[%%A]%%│
)
echo └─────────────────────────────────────┘
REM timeout 1 >NUL
goto :LOOPTOP
endlocal
- メモ
- ビット演算を使えば、もう少し高速化・効率化できそうだなと思った。