[Tex/LaTex] How to create a template for 2048 game situations

funtikz-pgf

I guess some of you might know the game 2048. A situation has the following shape:

Example

I would like to create a TikZ template that allows me to make similar drawings.

MWE

To create the following example (which is the best I got), I've used answers from Can TikZ create pixel art images?

\documentclass[tikz]{standalone}
\usepackage{ifthen}
\renewcommand\familydefault{\sfdefault}
\usepackage{tikz}
\usetikzlibrary{calc}
\def\pixels{
{0,2,0,0},
{0,8,0,4},
{2,2,4,16},
{8,16,128,2},
}

% Font color for 2 and 4: #776e65
% Font color for rest: #f9f6f2
% Grid color: #bbada0
% Font family: "Clear Sans", "Helvetica Neue", Arial, sans-serif

\definecolor{pixel 0}{HTML}{CCC0B3}
\definecolor{pixel 2}{HTML}{EEE4DA}
\definecolor{pixel 4}{HTML}{EEE4DA}
\definecolor{pixel 8}{HTML}{F2B179}
\definecolor{pixel 16}{HTML}{F59563}
\definecolor{pixel 32}{HTML}{F2B179} % TODO
\definecolor{pixel 64}{HTML}{F2B179}
\definecolor{pixel 128}{HTML}{EDCF72}
\definecolor{pixel 256}{HTML}{F2B179} % TODO
\definecolor{pixel 512}{HTML}{F2B179} % TODO
\definecolor{pixel 1024}{HTML}{F2B179} % TODO
\definecolor{pixel 2048}{HTML}{F2B179} % TODO
\definecolor{pixel 4096}{HTML}{3E3933}
\begin{document}
\begin{tikzpicture}
  \foreach \line [count=\y] in \pixels {
    \foreach \pix [count=\x] in \line {
      \draw[fill=pixel \pix] (\x,-\y) rectangle +(1,1);
      \ifthenelse{\equal{0}{\pix}}
           {}
           {\node at ($(\x,-\y) + (0.5,0.5)$)    {\Huge \pix};}
    }
  }

\end{tikzpicture}
\end{document}

Rendered:

enter image description here

Issues

  • How do I change the font color?
  • I always have problems with changing fonts: In this case, the font should be bold.
  • The numbers should fit into the cells with some padding.
  • The grid should not be black but #bbada0.

Best Answer

Third version (via pdflatex using ClearSans package)

(Note: ClearSans package, version 2014-05-28)

enter image description here

\documentclass[tikz]{standalone}
\usepackage{ClearSans}
\usepackage[T1]{fontenc}
\usepackage{tikz}
%
\usetikzlibrary{fit,backgrounds}
%
\def\gamefont{\bfseries\sffamily}
%
\definecolor{grid color}{HTML}{BBADA0}
\definecolor{pixel 0}{HTML}{CCC0B3}
\definecolor{pixel 2}{HTML}{EEE4DA}
\definecolor{pixel 4}{HTML}{EDE0C8}
\definecolor{pixel 8}{HTML}{F2B179}
\definecolor{pixel 16}{HTML}{F59563}
\definecolor{pixel 32}{HTML}{F67C5F}
\definecolor{pixel 64}{HTML}{F65E3B}
\definecolor{pixel 128}{HTML}{EDCF72}
\definecolor{pixel 256}{HTML}{EDCC61}
\definecolor{pixel 512}{HTML}{EDC850}
\definecolor{pixel 1024}{HTML}{EDC53F}
\definecolor{pixel 2048}{HTML}{EDC22E}
\definecolor{pixel 4096}{HTML}{3E3933}
%
\definecolor{small color}{HTML}{776E65}
\definecolor{big color}{HTML}{F9F6F2}
%
\tikzset{
  case 2048 base/.style={
    minimum size=9mm,rounded corners=.3mm,text=#1,inner sep=0,line width=0,
  },
  %
  case 2048 Large/.style={font=\Large\gamefont,case 2048 base=#1},
  case 2048 large/.style={font=\large\gamefont,case 2048 base=#1},
  case 2048 normal/.style={font=\normalsize\gamefont,case 2048 base=#1},
  %
  case 2048 0/.style={case 2048 Large=black,fill=pixel 0,node contents={}},
  case 2048 2/.style={case 2048 Large=small color,fill=pixel 2,node contents={2}},
  case 2048 4/.style={case 2048 Large=small color,fill=pixel 4,node contents={4}},
  case 2048 8/.style={case 2048 Large=big color,fill=pixel 8,node contents={8}},
  case 2048 16/.style={case 2048 Large=big color,fill=pixel 16,node contents={16}},
  case 2048 32/.style={case 2048 Large=big color,fill=pixel 32,node contents={32}},
  case 2048 64/.style={case 2048 Large=big color,fill=pixel 64,node contents={64}},
  case 2048 128/.style={case 2048 large=big color,fill=pixel 128,node contents={128}},
  case 2048 256/.style={case 2048 large=big color,fill=pixel 256,node contents={256}},
  case 2048 512/.style={case 2048 large=big color,fill=pixel 512,node contents={512}},
  case 2048 1024/.style={case 2048 normal=big color,fill=pixel 1024,node contents={1024}},
  case 2048 2048/.style={case 2048 normal=big color,fill=pixel 2048,node contents={2048}},
  case 2048 4096/.style={case 2048 normal=big color,fill=pixel 4096,node contents={4096}},
}
\begin{document}
\begin{tikzpicture}
  \def\pixels{
    {2,4,0,2048},
    {8,0,2,4096},
    {8,16,32,64},
    {1024,512,256,128},
  }

  \foreach \line [count=\y] in \pixels {
    \foreach \pix [count=\x] in \line {
      \path (\x,-\y) node[name=c2048-\x-\y,case 2048 \pix];
    }
  }

  \begin{scope}[on background layer]
    \node[fill=grid color,fit=(c2048-1-1)(c2048-4-4),
    inner sep=1mm,rounded corners=.3mm]{};
  \end{scope}
\end{tikzpicture}
\end{document}

2nd version (using the Intel ClearSans font with help of lualatex)

enter image description here

\documentclass[tikz]{standalone}
\usepackage{fontspec}
\usepackage{tikz}
%
\usetikzlibrary{fit,backgrounds}
%
\newfontfamily\clearsansfont{ClearSans}
\def\gamefont{\bfseries\clearsansfont}
%
\definecolor{grid color}{HTML}{BBADA0}
\definecolor{pixel 0}{HTML}{CCC0B3}
\definecolor{pixel 2}{HTML}{EEE4DA}
\definecolor{pixel 4}{HTML}{EDE0C8}
\definecolor{pixel 8}{HTML}{F2B179}
\definecolor{pixel 16}{HTML}{F59563}
\definecolor{pixel 32}{HTML}{F67C5F}
\definecolor{pixel 64}{HTML}{F65E3B}
\definecolor{pixel 128}{HTML}{EDCF72}
\definecolor{pixel 256}{HTML}{EDCC61}
\definecolor{pixel 512}{HTML}{EDC850}
\definecolor{pixel 1024}{HTML}{EDC53F}
\definecolor{pixel 2048}{HTML}{EDC22E}
\definecolor{pixel 4096}{HTML}{3E3933}
%
\definecolor{small color}{HTML}{776E65}
\definecolor{big color}{HTML}{F9F6F2}
%
\tikzset{
  case 2048 base/.style={
    minimum size=9mm,rounded corners=.3mm,text=#1,inner sep=0,line width=0,
  },
  %
  case 2048 Large/.style={font=\Large\gamefont,case 2048 base=#1},
  case 2048 large/.style={font=\large\gamefont,case 2048 base=#1},
  case 2048 normal/.style={font=\normalsize\gamefont,case 2048 base=#1},
  %
  case 2048 0/.style={case 2048 Large=black,fill=pixel 0,node contents={}},
  case 2048 2/.style={case 2048 Large=small color,fill=pixel 2,node contents={2}},
  case 2048 4/.style={case 2048 Large=small color,fill=pixel 4,node contents={4}},
  case 2048 8/.style={case 2048 Large=big color,fill=pixel 8,node contents={8}},
  case 2048 16/.style={case 2048 Large=big color,fill=pixel 16,node contents={16}},
  case 2048 32/.style={case 2048 Large=big color,fill=pixel 32,node contents={32}},
  case 2048 64/.style={case 2048 Large=big color,fill=pixel 64,node contents={64}},
  case 2048 128/.style={case 2048 large=big color,fill=pixel 128,node contents={128}},
  case 2048 256/.style={case 2048 large=big color,fill=pixel 256,node contents={256}},
  case 2048 512/.style={case 2048 large=big color,fill=pixel 512,node contents={512}},
  case 2048 1024/.style={case 2048 normal=big color,fill=pixel 1024,node contents={1024}},
  case 2048 2048/.style={case 2048 normal=big color,fill=pixel 2048,node contents={2048}},
  case 2048 4096/.style={case 2048 normal=big color,fill=pixel 4096,node contents={4096}},
}
\begin{document}
\begin{tikzpicture}
  \def\pixels{
    {2,4,0,2048},
    {8,0,2,4096},
    {8,16,32,64},
    {1024,512,256,128},
  }

  \foreach \line [count=\y] in \pixels {
    \foreach \pix [count=\x] in \line {
      \path (\x,-\y) node[name=c2048-\x-\y,case 2048 \pix];
    }
  }

  \begin{scope}[on background layer]
    \node[fill=grid color,fit=(c2048-1-1)(c2048-4-4),
    inner sep=1mm,rounded corners=.3mm]{};
  \end{scope}
\end{tikzpicture}
\end{document}

First version (default font and pdflatex)

Here is a (completed) solution without ifthen package:

enter image description here

\documentclass[tikz]{standalone}
\renewcommand\familydefault{\sfdefault}
\usepackage{tikz}
\usetikzlibrary{fit,backgrounds}
%
\definecolor{grid color}{HTML}{BBADA0}
\definecolor{pixel 0}{HTML}{CCC0B3}
\definecolor{pixel 2}{HTML}{EEE4DA}
\definecolor{pixel 4}{HTML}{EDE0C8}
\definecolor{pixel 8}{HTML}{F2B179}
\definecolor{pixel 16}{HTML}{F59563}
\definecolor{pixel 32}{HTML}{F67C5F}
\definecolor{pixel 64}{HTML}{F65E3B}
\definecolor{pixel 128}{HTML}{EDCF72}
\definecolor{pixel 256}{HTML}{EDCC61}
\definecolor{pixel 512}{HTML}{EDC850}
\definecolor{pixel 1024}{HTML}{EDC53F}
\definecolor{pixel 2048}{HTML}{EDC22E}
\definecolor{pixel 4096}{HTML}{3E3933}
%
\definecolor{small color}{HTML}{776E65}
\definecolor{big color}{HTML}{F9F6F2}
%
\tikzset{
  case 2048 base/.style={minimum size=9mm,rounded corners=.3mm,text=#1,inner sep=0},
  %
  case 2048 LARGE/.style={font=\LARGE\bfseries\sffamily,case 2048 base=#1},
  case 2048 Large/.style={font=\Large\bfseries\sffamily,case 2048 base=#1},
  case 2048 large/.style={font=\large\bfseries\sffamily,case 2048 base=#1},
  case 2048 normal/.style={font=\normalsize\bfseries\sffamily,case 2048 base=#1},
  %
  case 2048 0/.style={case 2048 Large=black,fill=pixel 0,node contents={}},
  case 2048 2/.style={case 2048 Large=small color,fill=pixel 2,node contents={2}},
  case 2048 4/.style={case 2048 Large=small color,fill=pixel 4,node contents={4}},
  case 2048 8/.style={case 2048 Large=big color,fill=pixel 8,node contents={8}},
  case 2048 16/.style={case 2048 Large=big color,fill=pixel 16,node contents={16}},
  case 2048 32/.style={case 2048 Large=big color,fill=pixel 32,node contents={32}},
  case 2048 64/.style={case 2048 Large=big color,fill=pixel 64,node contents={64}},
  case 2048 128/.style={case 2048 large=big color,fill=pixel 128,node contents={128}},
  case 2048 256/.style={case 2048 large=big color,fill=pixel 256,node contents={256}},
  case 2048 512/.style={case 2048 large=big color,fill=pixel 512,node contents={512}},
  case 2048 1024/.style={case 2048 normal=big color,fill=pixel 1024,node contents={1024}},
  case 2048 2048/.style={case 2048 normal=big color,fill=pixel 2048,node contents={2048}},
  case 2048 4096/.style={case 2048 normal=big color,fill=pixel 4096,node contents={4096}},
}
\begin{document}
\begin{tikzpicture}
  \def\pixels{
    {0,2,32,64},
    {256,8,512,4},
    {1024,2048,4,16},
    {4096,16,128,2},
  }

  \foreach \line [count=\y] in \pixels {
    \foreach \pix [count=\x] in \line {
      \path (\x,-\y) node[name=c2048-\x-\y,case 2048 \pix];
    }
  }

  \begin{scope}[on background layer]
    \node[fill=grid color,fit=(c2048-1-1)(c2048-4-4),
    inner sep=1mm,rounded corners=.3mm]{};
  \end{scope}
\end{tikzpicture}
\end{document}