import tkinter as tk
import math

delta = 0
right = True


def move_it_move_it():
    """Makes bear to move its head
    """
    global delta, right
    if delta < 15 and right:
        delta += 1
        canvas.move('head', 1, 0)
    elif delta > 0 and (not right):
        delta += -1
        canvas.move('head', -1, 0)
    if delta == 15:
        right = False
    elif delta == 0:
        right = True
    if spin_at_all:
        canvas.after(10, move_it_move_it)


def rotate(x, y, fi, tag):
    """Rotates items with <tag> tag by an angle fi in radians/
     around point (x, y)
    """
    for i in canvas.find_withtag(tag):
        coord_list = list(int(item) for item in canvas.coords(i))
        for j in range(len(coord_list) // 2):
            coord_list[2 * j] = x + (coord_list[2 * j] - x) * math.cos(fi) - (
                        coord_list[2 * j + 1] - y) * math.sin(fi)
            coord_list[2 * j + 1] = y + (coord_list[2 * j] - x) * math.sin(
                fi) + (coord_list[2 * j + 1] - y) * math.cos(
                fi)
        canvas.coords(i, coord_list)


delta_spin = 0
spin_right = True
spin_at_all = True


def spinning():
    """Animation of fishing rod rotation
    """
    global delta_spin, spin_right
    if delta_spin == 10:
        spin_right = False
    if spin_right:
        rotate(304, 613, 2.14 / 180, 'spinning')
        delta_spin += 1
    else:
        rotate(304, 613, -2.14 / 180, 'spinning')
    ce = canvas.coords(canvas.find_withtag('end'))
    cl = canvas.coords(canvas.find_withtag('leska'))
    cl[0] = ce[2]
    cl[1] = ce[3]
    canvas.coords(canvas.find_withtag('leska'), cl)

    if spin_at_all:
        canvas.after(50, spinning)


def stop_spin():
    """Stops fishing rod's animation
    """
    global spin_at_all
    spin_at_all = False


def smile():
    """Makes bear smile
    """
    i = canvas.find_withtag('smile')
    canvas.coords(i, 200.0, 475.0, 263.0, 492.0)


root = tk.Tk()  # initialization of tkinter main window
canvas = tk.Canvas(root)  # canvas init
canvas["width"] = 794  # setting size of canvas
canvas["height"] = 1123

##############
# Lots of strings automatically generated by the editor with some
# tag correction
##############

canvas.create_rectangle(0.0, 4.0, 793.0, 623.0, fill="#00ffff", width=1.0,
                        outline="#00ffff")
canvas.create_polygon(300.0, 2.0, 268.0, 27.0, 246.0, 51.0, 232.0, 72.0, 219.0,
                      98.0, 204.0, 134.0, 195.0, 165.0, 194.0,
                      182.0, 194.0, 219.0, 201.0, 258.0, 212.0, 294.0, 227.0,
                      327.0, 247.0, 357.0, 268.0, 379.0, 296.0,
                      404.0, 334.0, 429.0, 380.0, 447.0, 422.0, 458.0, 463.0,
                      463.0, 502.0, 462.0, 548.0, 456.0, 597.0,
                      439.0, 623.0, 426.0, 661.0, 402.0, 691.0, 378.0, 723.0,
                      338.0, 746.0, 294.0, 751.0, 275.0, 757.0,
                      256.0, 760.0, 233.0, 762.0, 203.0, 760.0, 178.0, 756.0,
                      152.0, 750.0, 130.0, 740.0, 101.0, 725.0,
                      76.0, 711.0, 56.0, 690.0, 31.0, 666.0, 10.0, 659.0, 3.0,
                      fill="#a1f9e4", width=1.0,
                      outline="#a1f9e4")
canvas.create_polygon(377.0, 3.0, 350.0, 17.0, 317.0, 43.0, 295.0, 66.0, 277.0,
                      91.0, 260.0, 127.0, 256.0, 145.0, 252.0,
                      169.0, 251.0, 198.0, 252.0, 229.0, 260.0, 264.0, 270.0,
                      289.0, 285.0, 318.0, 315.0, 358.0, 336.0,
                      378.0, 379.0, 402.0, 412.0, 414.0, 449.0, 421.0, 497.0,
                      422.0, 545.0, 414.0, 586.0, 399.0, 630.0,
                      372.0, 663.0, 338.0, 685.0, 304.0, 700.0, 269.0, 708.0,
                      235.0, 712.0, 207.0, 709.0, 165.0, 695.0,
                      120.0, 675.0, 81.0, 658.0, 60.0, 625.0, 28.0, 587.0, 1.0,
                      fill="#00ffff", width=1.0,
                      outline="#00ffff")
canvas.create_polygon(189.0, 173.0, 187.0, 208.0, 761.0, 221.0, 762.0, 185.0,
                      fill="#a1f9e4", width=1.0,
                      outline="#a1f9e4")
canvas.create_polygon(465.0, 3.0, 440.0, 460.0, 478.0, 464.0, 505.0, 3.0,
                      fill="#a1f9e4", width=1.0, outline="#a1f9e4")
canvas.create_polygon(197.0, 173.0, 195.0, 208.0, 246.0, 209.0, 248.0, 188.0,
                      248.0, 173.0, fill="#dcf7da", width=1.0,
                      outline="#dcf7da")
canvas.create_polygon(711.0, 183.0, 710.0, 218.0, 762.0, 220.0, 761.0, 184.0,
                      fill="#dcf7da", width=1.0,
                      outline="#dcf7da")
canvas.create_polygon(444.0, 421.0, 462.0, 424.0, 480.0, 424.0, 481.0, 464.0,
                      461.0, 464.0, 440.0, 462.0, 441.0, 441.0,
                      fill="#dcf7da", width=1.0, outline="#dcf7da")
canvas.create_polygon(455.0, 201.0, 456.0, 214.0, 490.0, 214.0, 494.0, 182.0,
                      456.0, 178.0, fill="#dcf7da", width=1.0,
                      outline="#dcf7da")
canvas.create_oval(191.0, 176.0, 225.0, 203.0, fill="#fff6d5", width=1.0,
                   outline="#fff6d5")
canvas.create_oval(451.0, 167.0, 511.0, 218.0, fill="#fff6d5", width=1.0,
                   outline="#fff6d5")
canvas.create_oval(457.0, 433.0, 478.0, 456.0, fill="#fff6d5", width=1.0,
                   outline="#fff6d5")
canvas.create_oval(723.0, 188.0, 758.0, 214.0, fill="#fff6d5", width=1.0,
                   outline="#fff6d5")
canvas.create_rectangle(1.0, 622.0, 795.0, 1111.0, fill="#e6e6e6", width=1.0,
                        outline="#000000")
canvas.create_oval(134.0, 420.0, 308.0, 514.0, fill="#e6e6e6", width=1.0,
                   outline="#000000", tags=('head'))
canvas.create_oval(7.0, 488.0, 260.0, 939.0, fill="#e6e6e6", width=1.0,
                   outline="#000000")
canvas.create_oval(146.0, 815.0, 330.0, 955.0, fill="#e6e6e6", width=1.0,
                   outline="#000000")
canvas.create_oval(257.0, 927.0, 396.0, 979.0, fill="#e6e6e6", width=1.0,
                   outline="#000000")
canvas.create_polygon(159.0, 453.0, 152.0, 447.0, 148.0, 440.0, 148.0, 434.0,
                      152.0, 429.0, 158.0, 427.0, 164.0, 426.0,
                      168.0, 429.0, 172.0, 429.0, 175.0, 433.0, 177.0, 436.0,
                      173.0, 442.0, 170.0, 446.0, 167.0, 448.0,
                      163.0, 451.0, 161.0, 452.0, fill="#e6e6e6", width=1.0,
                      outline="#000000", tags=('head'))
canvas.create_oval(208.0, 447.0, 219.0, 455.0, fill="#000000", width=1.0,
                   outline="#000000", tags=('head'))
canvas.create_oval(300.0, 454.0, 313.0, 462.0, fill="#000000", width=1.0,
                   outline="#000000", tags=('head'))
canvas.create_line(200.0, 491.0, 263.0, 492.0, fill="#000000", width=1.0,
                   tags=('head', 'smile'))
canvas.create_line(287.0, 489.0, 302.0, 484.0, fill="#000000", width=1.0,
                   tags='head')
canvas.create_line(263.0, 493.0, 288.0, 489.0, fill="#000000", width=1.0,
                   tags=('head'))
canvas.create_line(266.0, 696.0, 304.0, 613.0, fill="#000000", width=4.0,
                   tags='spinning')
canvas.create_line(304.0, 613.0, 361.0, 545.0, fill="#000000", width=4.0,
                   tags='spinning')
canvas.create_line(361.0, 545.0, 414.0, 501.0, fill="#000000", width=4.0,
                   tags=('spinning'))
canvas.create_line(414.0, 501.0, 474.0, 447.0, fill="#000000", width=4.0,
                   tags=('spinning'))
canvas.create_line(474.0, 447.0, 538.0, 400.0, fill="#000000", width=4.0,
                   tags=('spinning'))
canvas.create_line(538.0, 400.0, 599.0, 358.0, fill="#000000", width=4.0,
                   tags=('spinning'))
canvas.create_line(599.0, 358.0, 635.0, 336.0, fill="#000000", width=4.0,
                   tags=('spinning', 'end'))
canvas.create_oval(213.0, 584.0, 325.0, 632.0, fill="#e6e6e6", width=1.0,
                   outline="#000000")
canvas.create_oval(445.0, 787.0, 749.0, 878.0, fill="#4d4d4d", width=1.0,
                   outline="#000000")
canvas.create_oval(475.0, 814.0, 713.0, 879.0, fill="#165044", width=1.0,
                   outline="#000000")
canvas.create_line(632.0, 336.0, 642.0, 836.0, fill="#000000", width=1.0,
                   tags=('leska'))
canvas.create_polygon(480.0, 1000.0, 501.0, 1040.0, 542.0, 993.0, 520.0,
                      1000.0, 501.0, 1002.0, fill="#bfcbc8",
                      width=1.0, outline="#808080")
canvas.create_polygon(542.0, 993.0, 578.0, 965.0, 601.0, 952.0, 617.0, 946.0,
                      642.0, 942.0, 670.0, 946.0, 690.0, 955.0,
                      678.0, 969.0, 657.0, 984.0, 639.0, 992.0, 605.0, 997.0,
                      567.0, 997.0, 550.0, 996.0,
                      fill="#bfcbc8", width=1.0, outline="#808080")
canvas.create_polygon(567.0, 1020.0, 600.0, 1016.0, 600.0, 1008.0, 596.0,
                      996.0, 577.0, 997.0, 575.0, 1009.0, 570.0,
                      1016.0, fill="#dda6a6", width=1.0, outline="#808080")
canvas.create_polygon(641.0, 991.0, 654.0, 985.0, 662.0, 990.0, 672.0, 992.0,
                      679.0, 991.0, 658.0, 1015.0, 652.0,
                      1012.0, 644.0, 1000.0, 643.0, 996.0, fill="#dda6a6",
                      width=1.0, outline="#808080")
canvas.create_polygon(604.0, 952.0, 589.0, 944.0, 571.0, 939.0, 556.0, 938.0,
                      629.0, 924.0, 636.0, 927.0, 641.0, 937.0,
                      641.0, 942.0, 625.0, 944.0, 611.0, 949.0, fill="#dda6a6",
                      width=2.0, outline="#808080")
canvas.create_oval(650.0, 953.0, 668.0, 972.0, fill="#7979f2", width=1.0,
                   outline="#7979f2")
canvas.create_oval(657.0, 959.0, 665.0, 968.0, fill="#7e848c", width=1.0,
                   outline="#7e848c")
canvas.create_polygon(653.0, 959.0, 657.0, 960.0, 660.0, 963.0, 660.0, 964.0,
                      656.0, 964.0, 653.0, 961.0, 652.0, 961.0,
                      fill="#dfdfed", width=1.0, outline="#dfdfed")
canvas.grid(sticky="nwes")

#############
# Starting animations
#############

canvas.after(10, spinning)  # Fishing rod's rotation after 10ms
canvas.after(1000, stop_spin)  # Stop fishing rod's rotation after 1s
canvas.after(1400, smile)  # Smile after 1,4s
move_it_move_it()  # Start move head

root.mainloop()  # main loop for events automatic processing
