Tetris/Julia: Difference between revisions

m
Line 12:
# constructor: Z = B4(101,[0 0; 0 1; 1 1; 1 2])
function B4(bg,A::Array{Int,2}) # - fills in centered offsets for tetromino
new(bg, A .- sum.(extrema(A,dims=1)).>>1)
end end
# print tetromino on playfield
Line 21:
# hit other B4s or border?
function hit(B,r,c)
for i=1:4 BD[r+B.d[i,1],c>>1+B.d[i,2]+2]>0 && return true end; false
return false
end
# move tetrominos left/right/down, if no hit
Line 37 ⟶ 36:
for j = c.+(0,+2,-2) # try shifted positions
hit(A,r,j) || (draw(B,r,c,0); draw(A,r,j); return (A,j))
end; return (B,c) # cannot rotate: all 3 positions hit
end
return (B,c) # cannot rotate: all 3 positions hit
end
# record-place,clear-full-lines,drop-above,score
Line 118 ⟶ 116:
if (Ifo = !Ifo) io = open(Base.source_path()); readuntil(io,"="^6)
println("\e[0m\e[32H$(readuntil(io,"="^6))"); close(io)
else print("\e[0m\e[32H\e[J") # erase txt
print("\e[0m\e[32H\e[J") # erase txt
end end end
sleep(0.01) # not to take all CPU core time
Line 219 ⟶ 216:
Label - GoTo (for restart) require the main loop enclosed in Begin..End.
Several instructions are written in single lines of at most 84 chars,
to keep the program lessaround than 110100 non-comment lines long!
#.. Comments denote tasks, explain important points.
The program has embedded Unicode characters. They can be replaced with