Thank you Hans, it is quite effective.I just deleted commas and comments....
magicSquare[n_, ordx_, ordy_, breakx_, breaky_, i_, j_] :=
Module[{magicMatrix = Table[0, {n}, {n}], x = i, y = j, counter = 1,
nextox, nextoy, nextbx, nextby}, magicMatrix[[x, y]] = 1;
While[counter < n^2, counter++;
nextox = x + ordy;
If[nextox < 1, nextox = n + nextox];
If[nextox > n, nextox = Mod[nextox, n]];
nextoy = y + ordx;
If[nextoy < 1, nextoy = n + nextoy];
If[nextoy > n, nextoy = Mod[nextoy, n]];
If[magicMatrix[[nextox, nextoy]] == 0, x = nextox; y = nextoy;
magicMatrix[[x, y]] = counter, nextbx = x + breaky;
If[nextbx < 1, nextbx = n + nextbx];
If[nextbx > n, nextbx = Mod[nextbx, n]];
nextby = y + breakx;
If[nextby < 1, nextby = n + nextby];
If[nextby > n, nextby = Mod[nextby, n]];
If[magicMatrix[[nextbx, nextby]] == 0, x = nextbx;
y = nextby;
magicMatrix[[x, y]] = counter]]];
Return[MatrixForm[magicMatrix]];];
In[4]:= z = magicSquare[9, 1, 1, 0, 2, 6, 5] // AbsoluteTiming
Out[4]= {0.000499306,\!\(
TagBox[
RowBox[{"(", "", GridBox[{
{"37", "78", "29", "70", "21", "62", "13", "54", "5"},
{"6", "38", "79", "30", "71", "22", "63", "14", "46"},
{"47", "7", "39", "80", "31", "72", "23", "55", "15"},
{"16", "48", "8", "40", "81", "32", "64", "24", "56"},
{"57", "17", "49", "9", "41", "73", "33", "65", "25"},
{"26", "58", "18", "50", "1", "42", "74", "34", "66"},
{"67", "27", "59", "10", "51", "2", "43", "75", "35"},
{"36", "68", "19", "60", "11", "52", "3", "44", "76"},
{"77", "28", "69", "20", "61", "12", "53", "4", "45"}
},
GridBoxAlignment->{
"Columns" -> {{Center}}, "ColumnsIndexed" -> {},
"Rows" -> {{Baseline}}, "RowsIndexed" -> {}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.7]},
Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> {
Offset[0.2], {
Offset[0.4]},
Offset[0.2]}, "RowsIndexed" -> {}}], "", ")"}],
Function[BoxForm`e$,
MatrixForm[BoxForm`e$]]]\) }