I am trying to build some kind of a game where you have to keep the cursor as close as possible to a line. For this I animated a piecewise function with animation.FuncAnimation and added a crosshair cursor.
The problem is that the cursor disappears when it's not moving and each movement triggers some "blinking". How can I make it smooth ?
By the way, I am planning afterwards to define some scoring system by measuring the distance between the cursor and the line at each frame.
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.widgets import Cursor
fig, ax = plt.subplots()
x = range(120)
Y = [4, -1, 0, -2, -3, -5, 0, 2, 1, 3]
y = []
for i in range(14 * 60):
if i <= 180:
y.append(0)
elif 180 < i <= 240:
y.append((i - 180) * Y[0] / 60)
elif 240 < i <= 300:
y.append((i - 240) * (Y[1] - Y[0]) / 60 + Y[0])
elif 300 < i <= 360:
y.append((i - 300) * (Y[2] - Y[1]) / 60 + Y[1])
elif 360 < i <= 420:
y.append((i - 360) * (Y[3] - Y[2]) / 60 + Y[2])
elif 420 < i <= 480:
y.append((i - 420) * (Y[4] - Y[3]) / 60 + Y[3])
elif 480 < i <= 540:
y.append((i - 480) * (Y[5] - Y[4]) / 60 + Y[4])
elif 540 < i <= 600:
y.append((i - 540) * (Y[6] - Y[5]) / 60 + Y[5])
elif 600 < i <= 660:
y.append((i - 600) * (Y[7] - Y[6]) / 60 + Y[6])
elif 660 < i <= 720:
y.append((i - 660) * (Y[8] - Y[7]) / 60 + Y[7])
elif 720 < i <= 780:
y.append((i - 720) * (Y[9] - Y[8]) / 60 + Y[8])
elif 780 < i <= 840:
y.append(Y[9])
line, = ax.plot(x, y[0:120])
def animate(i):
line.set_ydata(y[0 + i:120 + i]) # update the data.
return line,
ani = animation.FuncAnimation(fig, animate, interval=20, blit=True, save_count=50)
plt.xlim([0, 120])
plt.ylim([-6, 6])
v1 = ax.axvline(55, color='pink', lw=2, alpha=0.7)
v2 = ax.axvline(65, color='pink', lw=2, alpha=0.7)
ax.fill_betweenx([-6, 6], 55, 65, color='pink', alpha=0.5, transform=ax.get_xaxis_transform())
cursor = Cursor(ax, useblit=True, color='red', linewidth=1)
plt.show()