| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 
 | import tkinter as tk
import time
 
figures = []
def create_personnage(canvas, x, y, color='red', legs=0, dx=2, dy=2):
    personnage = [ x, y, color, legs, dx, dy ]    
    size = 40
    ccr = canvas.create_rectangle
    ccr(x, y, x+size, y+size, fill=color)
    y0 = y + size + 5
    x0 = x
    if legs % 2:
        ccr(x0, y0, x0+15, y0+10, fill=color)
    else:
        ccr(x0+5, y0, x0+20, y0+10, fill=color)
    x0 = x+25
    if legs % 2:
        ccr(x0, y0, x0+15, y0+10, fill=color)
    else:
        ccr(x0-5, y0, x0+10, y0+10, fill=color)
    return personnage
 
done = False
def set_done():
    global done
    done = True
 
def figures_move(canvas, colors):
    for ix, personnage in enumerate(figures):
        x, y, color, legs, dx, dy = personnage
        if color in colors:
            x += dx
            y += dy
            legs = (legs + 1) % 4
            p = create_personnage(canvas, x, y, color, legs, dx, dy)
            figures[ix] = p # on mémorise la nouvelle position, état.
        else: # on fait du surplace.
            create_personnage(canvas, x, y, color, legs, dx, dy)
 
root = tk.Tk()
canvas = tk.Canvas(root)
canvas.pack()
 
for x, y in ((10, 10), (45, 80)):
    figures.append(create_personnage(canvas, x, y))
figures.append(create_personnage(canvas, 55, 0, color='blue', dx=8, dy=8))
 
colors = ['blue', 'red']
def do_toggle():
    if 'blue' in colors:
        colors.remove('blue')
    else:
        colors.append('blue')
tk.Button(root, text='toggle', command=do_toggle).pack()
tk.Button(root, text='quit', command=set_done).pack()
 
while not done:
    canvas.delete('all')
    figures_move(canvas, colors)
    root.update()
    time.sleep(0.2) |