1 2 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
|
import tkinter as tk
import time
dx=0
dy=0
figures = []
def create_personnage(canvas, x, y, color='', legs=0, dx=dx, dy=dy):
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, dx, dy in ((10, 10, 4, 10), (45, 80, 5, 12)):
figures.append(create_personnage(canvas, x, y, color='red',dx=dx, dy=dy))
figures.append(create_personnage(canvas, 55, 0, color='blue', dx=8, dy=8))
colors = ['blue', 'red']
while not done:
canvas.delete('all')
figures_move(canvas, colors)
root.update()
time.sleep(0.2) |
Partager