2017-07-05 80 views
0

我一直在爲自己製作一個項目,它可以批量生成一個網格,可以通過批處理遊戲等方式訪問和更改特定點以獲得更好的圖形。在最終輸出時多行批處理文件打印中的奇怪錯誤

@echo off 
setlocal ENABLEDELAYEDEXPANSION 

!x! 
::::::::::: 
:: ALIAS :: 
::::::::::: 
:@alias 
    set x=exit/b 
    goto:@main 
    !x! 
::::::::::: 
:: MAIN :: 
::::::::::: 
:@main 
call:$Canvas _canvas 16 16 . 
set px=%~1 
set py=%~2 
set _canvas_point[!px!][!py!]=# 
!_canvas#draw! 

!x! 
::::::::::: 
::OBJECTS:: 
::::::::::: 
:@objects 
!x! 
:$Canvas 
    set this=%~1 
    set /a !this!_width=%~2 
    set /a !this!_height=%~3 
    set !this!_default_char=%~4 
    set !this!#draw=call:#$canvas_proto_draw !this! 
    for /L %%i in (1, 1, !%this%_height!) do (
     for /L %%j in (1, 1, !%this%_width!) do (
      set !this!_point[%%j][%%i]=!%this%_default_char! 
      set !this!_row[%%i]=!%this%_row[%%i]!!%this%_point[%%j][%%i]! 
      ) 
     ) 
    !x! 
    :#$Canvas_proto_draw 
     set this=%~1 
     for /L %%i in (1, 1, !%this%_height!) do (
     for /L %%j in (1, 1, !%this%_width!) do (
      set !this!_row[%%i]=!%this%_row[%%i]!!%this%_point[%%j][%%i]! 
      ) 
     ) 
     for /L %%i in (1, 1, !%this%_height!) do (
      echo !%this%_row[%%i]! 
      ) 
    !x! 

我的問題:給它1, 1會導致點出了通過頂行大致半山腰。我不知道爲什麼會發生這種情況,需要幫助。我梳理了echo的所有輸出,並且_canvas_point[1][1]的值在更新上的值爲#

這裏是輸出:

C:\Home>canvas 1 1 
................#............... 
................................ 
................................ 
................................ 
................................ 
................................ 
................................ 
................................ 
................................ 
................................ 
................................ 
................................ 
................................ 
................................ 
................................ 
................................ 

C:\Home> 

Here是一種以具有@echo on

快速事物的輸出引擎收錄鏈接: 是的,這是在批處理文件 面向對象編程一個微弱的嘗試是的,我知道其他語言對這種事情更好,我只是想用批處理文件來做 是的,我有奇怪的文體選擇與名稱和標籤等。這樣我就可以更好地閱讀它,看到標籤用於goto位置和功能標籤。

回答

0

也許下面的例子來靠近你要完成什麼(面向對象.bat文件的方式):

@echo off 
setlocal ENABLEDELAYEDEXPANSION 

rem Initialize object "_canvas" (index numbers are row then column) 
call :#$proto_init _canvas 16 16 . 

rem Set a few "_canvas" array characters to non-default values 
call :#$proto_set _canvas 1 1 ? 
call :#$proto_set _canvas 8 8 # 
call :#$proto_set _canvas 16 16 $ 

rem Display "_canvas" 
call :#$proto_draw _canvas 
endlocal 
exit/b 


:#$proto_init 
set /a %1_height=%2, %1_width=%3, %1_len=%2 * %3 
set %1_default_char=%4 
set %1_data=X 
for /l %%i in (1, 1, !%1_len!) do set %1_data=!%1_data!!%1_default_char! 
exit /b 


:#$proto_set 
for /f %%i in ('set /a ^(%2 - 1^) * !%1_height! + %3') do (
    for /f %%j in ('set /a %%i + 1') do (
    set %1_data=!%1_data:~0,%%i!%4!%1_data:~%%j! 
) 
) 
exit/b 


:#$proto_draw 
for /l %%w in (!%1_width!, 1, !%1_width!) do (
    for /l %%h in (1, %%w, !%1_len!) do echo !%1_data:~%%h,%%w! 
) 
exit /b 
+0

謝謝,尋求幫助。我明白,這種方法沒有多大意義,但謝謝你的幫助:) – Bmofa

0

在:#$ Canvas_proto_draw

:#$Canvas_proto_draw 
set this=%~1 
for /L %%i in (1, 1, !%this%_height!) do (
SET "!this!_row[%%i]=" 
for /L %%j in (1, 1, !%this%_width!) do (
... 

您可能會發現您顯示的數據是32列x 16行。這是由於在追加值之前未清除每行。