Hello my friends, in this new entry I am going to show you how to make cool animations with some algebra and Python, algebra? Yes my friend, because animating an object is based on some transformation of coordinates, translating or rotating for example, but we will get into that don't worry; Now let's talk about the Python Tools that we are going to use.

· Numpy

· Matplotlib

With

**numpy**we can make and manipulate arrays with Python, the array type in this package has methods that can use for common operations in algebra, among other uses with also are going to take into account the trigonometric functions,**sin**and**cos**, useful for rotations.
With matplotlib we can create pretty cool plots, and turning the interactive mode on, we can make some iteractive plots hence a cool animation. Let me show you the final result for our code so you can have a pretty good idea.

Cool right, as we can see on the animation, a small pacman figure is moving through a small maze, and in each corner it will turn or rotate to keep moving through the maze or blue path. The code for this is very simple, so let's do the next thing, I will show you the entire code and explain the parts that could be tricky.

**import**

**numpy**

**as**

**np**

**import**

**matplotlib.pyplot**

**as**

**plt**

*# Functions*

translate =

**lambda**dx, dy: np.matrix ([[1, 0, dx], [0, 1, dy], [0, 0, 1]])**def**rotate(angle):

radians = angle * (np.pi / 180.0)

**return**np.array([[np.cos(radians), -np.sin(radians), 0],

[np.sin(radians), np.cos(radians), 0],

[0, 0, 1]])

vector =

**lambda**x: np.transpose(np.array(x))**def**animate():

*'''*

*Animation Function*

*'''*

**global**dx, dy, xcoords, ycoords, pacman_center, current_pacman, plt

current_pacman = translate(dx, dy) * current_pacman

xcoords = np.array(current_pacman[0]).reshape(9)

ycoords = np.array(current_pacman[1]).reshape(9)

pacman_center[0] += dx

pacman_center[1] += dy

**if**pacman_center == [22.5, 2.5]:

dx = 0

dy = 0.5

current_pacman = translate(22.5, 2.5) * rotate(90) * translate(-22.5, -2.5) * current_pacman

**elif**pacman_center == [22.5, 22.5]:

dx = -0.5

dy = 0

current_pacman = translate(22.5, 22.5) * rotate(90) * translate(-22.5, -22.5) * current_pacman

**elif**pacman_center == [2.5, 22.5]:

dx = 0

dy = -0.5

current_pacman = translate(2.5, 22.5) * rotate(90) * translate(-2.5, -22.5) * current_pacman

**elif**pacman_center == [2.5, 12.5]:

dx = 0.5

dy = 0

current_pacman = translate(2.5, 12.5) * rotate(90) * translate(-2.5, -12.5) * current_pacman

**elif**pacman_center == [14, 12.5]:

dx = 0

dy = 0

plt.fill(x_maze, y_maze, color='b')

plt.fill(xcoords, ycoords, color='y')

plt.plot(14, 12.5, marker='d')

plt.plot(22.5, 2.5, marker='*')

plt.plot(22.5, 22.5, marker='*')

plt.plot(2.5, 22.5, marker='*')

plt.plot(2.5, 12.5, marker='*')

**return**dx, dy

x_maze = np.array([0.0, 25, 25, 0, 0, 15, 15, 5, 5, 20, 20, 0])

y_maze = np.array([0.0, 0, 25, 25, 10, 10, 15, 15, 20, 20, 5, 5])

xcoords = np.array([0.0, 1, 2, 3, 2, 3, 2, 1, 0])

ycoords = np.array([2, 1, 1, 2, 2.5, 3, 4, 4, 3])

zcoords = np.ones(len(xcoords))

pacman = np.array([xcoords, ycoords, zcoords])

plt.ion()

plt.axis('equal')

plt.axis([-5, 30, -5, 30])

current_pacman = pacman

dx = 0.5

dy = 0.0

pacman_center = [1.5, 2.5]

**for**i

**in**range(200):

**if**animate() == (0, 0):

**break**

plt.pause(0.05)

As always we make the importation of the packages (our tools) that we are going to use, in this case numpy and matplotlib.

Then we are going to create some functions, 2 of those are anonymous functions, if you are not familiar with these kind of functions you can check my entry of lambda, these functions can take some arguments and will return a matrix or double size array, based on these matrix, by making dot product with some coordinates in array shape, you can rotate or translate those coordinates, in this way how we can make animations.

The main two steps in the animations are,

· Create new coordinates, by translating or rotating

· Update interactively the plot with new coordinates

These two steps are made in a function call animate, that takes the global variables with the coordinates of the maze and the packman figure and update the plot, in this functions there are also some conditional statements to check whether the pacman figure has reached a corner in order to rotate it.

Something else to point out is for us to be able to update the plot we need to turn the interactive mode of matplotlib on, we can do this by using

**plt.ion****()**, and that is all.
Finally we use a for loop for making the frame update with a pause to control the speed of refreshing the plot, and a conditional statement to break the for loop if the pacman figure has reached the end.

Well my friend we have come to the end of this entry, hope you can use some of this code in your programing.

So thanks for coming by my new entry and I hope you have enjoyed this as much as I enjoyed writing it, stop by the comments if you want to discuss about this, share in your social media and subscribe. Cheers my friends.