1-半自动版本

1-半自动版本

我正在尝试制作一款 Chutes and Ladders 风格的游戏。我已经设法拼凑了一个棋盘,但我计划在未来的棋盘中更改方块、梯子和滑梯的位置。梯子是一个特别丑陋的黑客,通过反复试验进行了调整。我将不胜感激任何关于构建这种棋盘的建议。具体来说,我想制作一个命令,通过指定起始和结束坐标来绘制每个梯子。我找不到任何有助于绘制梯子的装饰。

\documentclass{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[scale=1.5]
%board
\draw (0,0) grid (6,1) (5,0)grid (6,3) (0,2)grid (6,3) 
(0,2) grid (1,5) (0,4) grid (6,5)(5,5) grid (6,9) 
(0,8) grid (6,9) (0,6) grid (1,8) (1,6) grid (4,7);

%ladders
\draw[scale=.5,xshift=2.5cm,yshift=1.5cm,rotate=-45] (0,-.5) grid ++(1,6);
\draw[scale=.5,xshift=6.5cm,yshift=9.5cm,rotate=-45] (0,-.5) grid ++(1,6);
\draw[scale=.5,xshift=.5cm,yshift=9.5cm,rotate=0] (0,-.5) grid ++(1,4);

% slides
\draw (1.75,2.5) sin ++(1,-1) cos ++(1,-1);
\draw (1.25,2.5) sin ++(1,-1) cos ++(1,-1);
\draw (5.75,4.5) sin ++(-.5,-1) cos ++(-.5,-1);
\draw (5.25,4.5) sin ++(-.5,-1) cos ++(-.5,-1);

\draw (3.5,6.5) node{$\star$};
\end{tikzpicture}

\end{document}

滑梯和梯子委员会

答案1

(共有四个连续版本。)

1-半自动版本

这是通过 进行的第一次尝试markings。棋盘和梯子使用单元格的整数坐标进行绘制。但您必须手动查找/指定每个梯子的正确条数。

在此处输入图片描述

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings,calc}
\def\ladderwidth{1.5mm}
\def\ladderstep{3mm}
\tikzset{
  board/.style={decorate,decoration={
      markings,
      mark=between positions 0 and 1 step 1cm with
      {\draw[fill=lime!20] (-.5,-.5) rectangle (.5,.5);}
    },
  },
  ladder/.style={decorate,decoration={
      markings,
      mark=between positions {1/#1/2} and {-1/#1/2} step {1/#1} with {
        \draw[thick,orange]
        (0,-\ladderwidth) -- (0,\ladderwidth)
        (\ladderstep,-\ladderwidth) -- (-\ladderstep,-\ladderwidth)
        (\ladderstep,\ladderwidth) -- (-\ladderstep,\ladderwidth);
      }
    },
  },
}
\begin{document}
\begin{tikzpicture}
  \draw[board]
  (1,0) -- (6,0) -- (6,2) -- (1,2) -- (1,4) -- (6,4) -- (6,8) -- (1,8) -- (1,6) -- (4,6);
  \draw[ladder=5] (1,4) -- (1,6);
  \draw[ladder=6] (1,0) -- (3,2);
  \draw[ladder=7] (5,4) -- (6,7);
  \draw[ladder=18] (5,0) -- (2,8);
\end{tikzpicture}
\end{document}

2- 自动版本

这是第二次尝试自动计算(通过路径to)每个梯子的杆数...两张图显示了不同值的结果\ladderstep(左边是 1.5 毫米,右边是 3 毫米)。

1.5毫米在此处输入图片描述 3毫米 在此处输入图片描述

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings,calc}
\def\ladderwidth{1.5mm}
\def\ladderstep{1.5mm}
\tikzset{
  board/.style={decorate,decoration={
      markings, mark=between positions 0 and 1 step 1cm with
      {\draw[fill=lime!20] (-.5,-.5) rectangle (.5,.5);}
    },
  },
  ladder/.style={decorate,decoration={
      markings, mark=between positions {1/#1/2} and {-1/#1/2} step {1/#1} with {
        \draw[thick,orange] (0,-\ladderwidth) -- (0,\ladderwidth)
        (\ladderstep,-\ladderwidth) -- (-\ladderstep,-\ladderwidth)
        (\ladderstep,\ladderwidth) -- (-\ladderstep,\ladderwidth);
      }
    },
  },
  ladder auto/.style={to path={
      let \p1=($(\tikztostart) - (\tikztotarget)$), \n1={veclen(\x1,\y1)} in
      \pgfextra{
        \pgfmathsetmacro{\bars}{int(\n1/\ladderstep/2)+1}
        \pgfinterruptpath
        \draw[ladder=\bars] (\tikztostart) -- (\tikztotarget);
        \endpgfinterruptpath
      }
    },
  },
}
\begin{document}
\begin{tikzpicture}
  \draw[board] (1,0) -- (6,0) -- (6,2) -- (1,2) -- (1,4) -- (6,4)
  -- (6,8) -- (1,8) -- (1,6) -- (4,6);
  \draw (1,4) to[ladder auto] (1,6) (1,0) to[ladder auto] (3,2)
  (5,4) to[ladder auto] (6,7) (5,0) to[ladder auto] (2,8);
\end{tikzpicture}
\end{document}

3- 完全自动化版本

这是第三次尝试。我添加了降落伞(总是通过路径to)。

在此处输入图片描述

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings,calc}
\def\ladderwidth{1.5mm}
\def\ladderstep{1.5mm}
\tikzset{
  board/.style={decorate,decoration={
      markings,
      mark=between positions 0 and 1 step 1cm with
      {\draw[fill=lime!20] (-.5,-.5) rectangle (.5,.5);}
    },
  },
  ladder/.style={decorate,decoration={
      markings,
      mark=between positions {1/#1/2} and {-1/#1/2} step {1/#1} with {
        \draw[thick,orange]
        (0,-\ladderwidth) -- (0,\ladderwidth)
        (\ladderstep,-\ladderwidth) -- (-\ladderstep,-\ladderwidth)
        (\ladderstep,\ladderwidth) -- (-\ladderstep,\ladderwidth);
      },
    },
  },
  ladder auto/.style={
    to path={
      let
      \p1=($(\tikztostart) - (\tikztotarget)$),
      \n1={veclen(\x1,\y1)}
      in
      \pgfextra{
        \pgfmathsetmacro{\bars}{int(\n1/\ladderstep/2)+1}
        \pgfinterruptpath
        \draw[ladder=\bars] (\tikztostart) -- (\tikztotarget);
        \endpgfinterruptpath
      }
    },
  },
  chute auto/.style={
    to path={
      let
      \p1=([xshift=\ladderwidth]\tikztostart),
      \p2=([xshift=-\ladderwidth]\tikztostart),
      \p3=([xshift=\ladderwidth]\tikztotarget),
      \p4=([xshift=-\ladderwidth]\tikztotarget),
      \p5=($(\p1)!.5!(\p3)$),
      \p6=($(\p2)!.5!(\p4)$)
      in
      \pgfextra{
        \pgfinterruptpath
        \path[thick,draw=orange]
        (\p1) sin (\p5) cos (\p3)
        (\p4) sin (\p6) cos (\p2);
        \fill[fill=orange!20]
        (\p1) sin (\p5) cos (\p3) --
        (\p4) sin (\p6) cos (\p2) -- cycle;
        \endpgfinterruptpath
      }
    },
  },
}
\begin{document}
\begin{tikzpicture}
  \draw[board] (1,0) -- (6,0) -- (6,2) -- (1,2) -- (1,4) -- (6,4)
  -- (6,8) -- (1,8) -- (1,6) -- (4,6);
  \draw[chute auto] (1,2) to (3,0) (2,4) to (5,2) (5,8) to (2,2);
  \draw[ladder auto] (1,4) to (1,6) (1,0) to (3,2)
  (5,4) to (6,7) (5,0) to (2,8);
\end{tikzpicture}
\end{document}

4- 具有样式、刻度和变体的完整自动版本

这是第四次尝试,具有请求的缩放功能……但没有scale选项。更改:

  • 您可以通过以下方式选择全局尺寸\cellsize:每种... auto样式都会考虑\cellsize距离并改变坐标系以将整数坐标保持在单元格的中心。

  • 有三种新样式可供选择绘图和填充选项:cellladderchute

  • 有一种新的ladder auto bis样式可以绘制梯子上的水平杆。

在此处输入图片描述

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings,calc}
\pgfmathsetmacro{\cellsize}{1cm}
\pgfmathsetmacro{\ladderstep}{\cellsize/6}
\pgfmathsetmacro{\ladderwidth}{\cellsize/6}
\tikzset{
  % styles to draw and to fill
  cell/.style={
    draw=green!50!black,top color=lime!10,bottom color=lime,
    line width=1pt,
    rounded corners=\cellsize/5
  },
  ladder/.style={
    draw=orange,line cap=round,line join=round,line width=2pt,
  },
  chute/.style={
    top color=blue!30,
    bottom color=blue!30,
    middle color=blue!5,
    draw=blue!50!black,line cap=round,
    line join=round,line width=1pt,
  },
  % styles to draw board, ladders and chutes
  board auto/.style={
    x=\cellsize pt,
    y=\cellsize pt,
    decorate,decoration={
      markings,
      mark=between positions 0 and 1 step \cellsize pt with
      {\path[cell] (-.5*\cellsize pt,-.5*\cellsize pt)
        rectangle (.5*\cellsize pt,.5*\cellsize pt);}
    },
  },
  ladder draw/.style={
    decorate,decoration={
      markings,
      mark=between positions {1/#1/2} and {-1/#1/2} step {1/#1} with {
        \path[ladder]
        (0,-\ladderwidth pt) -- (0,\ladderwidth pt)
        (\ladderstep pt,-\ladderwidth pt) -- (-\ladderstep pt,-\ladderwidth pt)
        (\ladderstep pt,\ladderwidth pt) -- (-\ladderstep pt,\ladderwidth pt);
      },
    },
  },
  ladder auto/.style={
    x=\cellsize pt,
    y=\cellsize pt,
    to path={
      let
      \p1=($(\tikztostart) - (\tikztotarget)$),
      \n1={veclen(\x1,\y1)}
      in
      \pgfextra{
        \pgfmathsetmacro{\bars}{int(\n1/\ladderstep/2)+1}
        \pgfinterruptpath
        \draw[ladder draw=\bars] (\tikztostart) -- (\tikztotarget);
        \endpgfinterruptpath
      }
    },
  },
  ladder auto bis/.style={
    x=\cellsize pt,
    y=\cellsize pt,
    to path={
      let
      \p1=(\tikztostart),
      \p2=(\tikztotarget),
      \p3=($(\tikztostart) - (\tikztotarget)$),
      \p4=([xshift=-\ladderwidth pt]\p1),
      \p5=([xshift=-\ladderwidth pt]\p2),
      \p6=([xshift=\ladderwidth pt]\p1),
      \p7=([xshift=\ladderwidth pt]\p2),
      \n1={veclen(\x3,\y3)}
      in
      \pgfextra{
        \pgfmathtruncatemacro{\bars}{int(\n1/\ladderstep/2)+1}
        \pgfinterruptpath
        \path[ladder] (\p4) -- (\p5);
        \path[ladder] (\p6) -- (\p7)
        \foreach \bar in {1,...,\bars}{
          \pgfextra{\pgfmathsetmacro{\pos}{1/(\bars+1)*\bar}}
          coordinate[pos=\pos] (p\bar)
        };
        \foreach \bar in {1,...,\bars}{
          \path[ladder] (p\bar) -- ++(-2*\ladderwidth pt,0);
        }
        \endpgfinterruptpath
      }
    },
  },
  chute auto/.style={
    x=\cellsize pt,
    y=\cellsize pt,
    to path={
      let
      \p1=([xshift=\ladderwidth pt]\tikztostart),
      \p2=([xshift=-\ladderwidth pt]\tikztostart),
      \p3=([xshift=\ladderwidth pt]\tikztotarget),
      \p4=([xshift=-\ladderwidth pt]\tikztotarget),
      \p5=($(\p1)!.5!(\p3)$),
      \p6=($(\p2)!.5!(\p4)$)
      in
      \pgfextra{
        \pgfinterruptpath
        \path[chute]
        (\p1) sin (\p5) cos (\p3) --
        (\p4) sin (\p6) cos (\p2) -- cycle;
        \endpgfinterruptpath
      }
    },
  },
}
\begin{document}
\begin{tikzpicture}
  \draw[board auto] (1,0) -- (6,0) -- (6,2) -- (1,2) -- (1,4) -- (6,4)
  -- (6,8) -- (1,8) -- (1,6) -- (4,6);
  \draw[chute auto] (1,2) to (3,0) (2,4) to (5,2) (5,8) to (2,2);
  \draw[ladder auto bis] (1,4) to (1,6) (1,0) to (3,2)
  (5,4) to (6,7) (5,0) to (2,8);
\end{tikzpicture}
\end{document}

相关内容