\documentclass[border = 5pt, tikz]{standalone}
\usepackage{xcolor-material}
% from https://tex.stackexchange.com/questions/413389/how-to-make-a-simple-drawing-of-an-apple-using-only-the-tikz-library
\usetikzlibrary{fit}
\tikzset{%
apple/.pic={
\fill [MaterialBrown] (-1/8,0)
arc (180:120:1 and 3/2) coordinate [pos=3/5] (@)-- ++(1/6,-1/7)
arc (120:180:5/4 and 3/2) -- cycle;
\fill [MaterialLightGreen500] (0,-9/10)
.. controls ++(180:1/8) and ++( 0:1/4) .. (-1/3, -1)
.. controls ++(180:1/3) and ++(270:1/2) .. ( -1, 0)
.. controls ++( 90:1/3) and ++(180:1/3) .. (-1/2, 3/4)
.. controls ++( 0:1/8) and ++(135:1/8) .. ( 0, 4/7)
.. controls ++( 45:1/8) and ++(180:1/8) .. ( 1/2, 3/4)
.. controls ++( 0:1/3) and ++( 90:1/3) .. ( 1, 0)
.. controls ++(270:1/2) and ++( 0:1/3) .. ( 1/3, -1)
.. controls ++(180:1/4) and ++( 0:1/8) .. cycle;
\fill [MaterialLightGreen600] (0, 4/7)
.. controls ++( 45:1/8) and ++(180:1/8) .. ( 1/2, 3/4)
.. controls ++( 0:1/3) and ++( 90:1/3) .. ( 1, 0)
.. controls ++(270:1/2) and ++( 0:1/3) .. ( 1/3, -1)
.. controls ++(180:1/4) and ++( 0:1/8) .. ( 0,-9/10);
\fill [MaterialGreen500, shift={(@)}, rotate=-30]
(0,0) arc (45:135:3/4 and 3/5) arc (225:315:3/4 and 3/5);
\fill [MaterialGreen700, shift={(@)}, rotate=-30]
(0,0) arc (315:225:3/4 and 3/5) -- cycle;
},
cherry/.pic={
\foreach \i in {1,2}{
\tikzset{shift={(-1+\i*3/4, -3/5+\i/5)},scale=1/2, rotate=15-\i*10}
\fill [MaterialRed700] (0,19/20)
.. controls ++(180:1/8) and ++( 0:1/4) .. (-1/3, 1)
.. controls ++(180:1/3) and ++( 90:1/2) .. ( -1, 0)
.. controls ++(270:1/2) and ++(180:1/2) .. ( 0, -1)
.. controls ++( 0:1/2) and ++(270:1/2) .. ( 1, 0)
.. controls ++( 90:1/2) and ++( 0:1/3) .. ( 1/3, 1)
.. controls ++(180:1/4) and ++( 0:1/8) .. cycle;
\fill [MaterialRed800] (0, -1)
.. controls ++( 0:1/2) and ++(270:1/2) .. ( 1, 0)
.. controls ++( 90:1/2) and ++( 0:1/3) .. ( 1/3, 1)
.. controls ++(180:1/4) and ++( 0:1/8) .. ( 0,19/20) -- cycle;
\fill [MaterialRed900] (0,3/4) coordinate (@\i)
ellipse [x radius=1/4, y radius=1/8];
}
\fill [MaterialBrown]
(1/4,11/8) -- (3/8,11/8) coordinate (@)
.. controls ++(270:1/2) and ++(135:1/3) .. (@1)
.. controls ++(135:1/2) and ++(270:1/2) .. cycle;
\fill [MaterialBrown]
(1/4,11/8) -- (3/8,11/8)
.. controls ++(315:1/2) and ++(45:1/2) .. (@2)
.. controls ++(60:1/2) and ++(315:1/2) .. cycle;
\fill [MaterialGreen500, shift={(@)}, rotate=20]
(0,0) arc (45:135:3/4 and 3/5) arc (225:315:3/4 and 3/5);
\fill [MaterialGreen700, shift={(@)}, rotate=20]
(0,0) arc (315:225:3/4 and 3/5) -- cycle;
}}
\newcount\nframes
\nframes=80
\begin{document}
\foreach \n in {0,...,\nframes}{
\begin{tikzpicture}
\pgfmathsetmacro{\x}{10.0 * \n / \nframes}
\pgfmathsetmacro{\angle}{5 + 25 * abs(sin(3 * 360 * (\n / \nframes)))}
\clip (-1, -1.2) rectangle (10, 1.2);
% food
\begin{scope}
\clip (\x, -1.2) rectangle (10, 1.2);
\foreach \i in {0,...,10} {
\ifnum\i=5 \path (8, 0) pic[scale = 0.4] {apple};
\else \ifnum\i=8 \path (5, 0) pic[scale = 0.4] {cherry};
\else \fill[gray, opacity = 0.3] (\i, 0) circle (0.2);
\fi \fi
}
\end{scope}
% pacman
\begin{scope}[xshift = \x cm]
\draw[thick,fill = yellow]
(0,0) -- ++ (\angle:1cm) arc (\angle:360-\angle:1cm) -- cycle;
\fill (0,0.66) circle (1.5mm);
\end{scope}
\end{tikzpicture}
}
\end{document}
EDIT: Thanks to @UlrikeFischer for the suggestion :)
\newcommand{\interrupted}{%
\pgfmathsetmacro{\dx}{11.0 / 7.0}
\foreach \i/\c in {0/gray,1/yellow,2/cyan,3/green,4/magenta,5/red,6/blue} {
\fill[\c] (\i * \dx - 1, -0.4) rectangle (\i * \dx + \dx - 1, 1.2);
}
\foreach \i/\c in {0/blue,1/black,2/magenta,3/black,4/cyan,5/black,6/white} {
\fill[\c] (\i * \dx - 1, -0.6) rectangle (\i * \dx + \dx - 1, -0.4);
}
\pgfmathsetmacro{\dx}{11.0 / 6.0}
\foreach \i/\c in {0/blue!40!black,1/white,2/blue,3/black,4/black!80,5/black} {
\fill[\c] (\i * \dx - 1, -1.2) rectangle (\i * \dx + \dx - 1, -0.6);
}
\node[white] at (5.5, 0) {\textbf{No ducks were harmed in this test}}
}
And add this to the main loop
\pgfmathsetmacro{\switchl}{\x > 4.0}
\pgfmathsetmacro{\switchr}{\x < 6.0}
\ifnum\switchl=1 \ifnum\switchr=1
\interrupted{};
\fi\fi