Ответ:
from tkinter import *
import random
import time
def move(event):
if event.keysym == 'Up':
canvas.move(p, 0, -3)
elif event.keysym == 'Down':
canvas.move(p, 0, 3)
elif event.keysym == 'Left':
canvas.move(p, -3, 0)
elif event.keysym == 'Right':
canvas.move(p, 3, 0)
def mainloop():
while 1:
canvas.move(c, random.randint(-6, 6), random.randint(-6, 6))
tk.update_idletasks()
tk.update()
time.sleep(0.01)
colors=['black', 'white', 'red', 'yellow', 'blue', 'purple']
tk=Tk()
tk.geometry('400x400')
canvas=Canvas (tk, width=400, height=400)
canvas.pack(fill='both', expand=True)
p=canvas.create_rectangle(10, 10, 70, 150, fill=random.choice(colors), outline=random.choice(colors))
c=canvas.create_oval(190, 190, 210, 210, fill=random.choice(colors), outline=random.choice(colors))
canvas.bind_all('', move)
canvas.bind_all('', move)
canvas.bind_all('', move)
canvas.bind_all('', move)
mainloop()
Объяснение: