Advertisement
Chl_Snt

№16. Tkinter. Анимация

Jul 12th, 2023
973
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | None | 0 0
  1. import random
  2. import time
  3. from tkinter import *
  4. from tkinter.colorchooser import *
  5.  
  6.  
  7. root = Tk()
  8. root.geometry("800x600+200+200")
  9. root.title("Анимация")
  10. root["bg"] = 'white'
  11.  
  12. canvas = Canvas(root, width=800, height=600, bg="white")
  13. canvas.pack()
  14. color = "red"
  15.  
  16. h_x = 10
  17.  
  18. bullets = []
  19.  
  20.  
  21. canvas.create_rectangle(0, 180, 40, 200, fill="black")
  22. canvas.create_rectangle(0, 250, 40, 270, fill="black")
  23.  
  24.  
  25. def ask_color(event):
  26.     global color
  27.     color_code = askcolor(title="Выбери цвет")
  28.     color = color_code[1]
  29.  
  30.  
  31. root.bind_all("<2>", ask_color)
  32.  
  33.  
  34. def shot():
  35.     global bullets
  36.     hero = canvas.create_oval(5, 205, 45, 245, fill=color)
  37.     b = (hero, 10, random.random())
  38.     bullets.append(b)
  39.     root.after(50, shot)
  40.  
  41.  
  42. def draw():
  43.     global h_x
  44.     for bull in bullets:
  45.         bullet = bull[0]
  46.         h_x = bull[1]
  47.         h_y = bull[2]
  48.  
  49.         x, y, x1, y1 = canvas.coords(bullet)
  50.         if x1 >= 800:  # если мяч коснулся правой стенки
  51.             canvas.delete(bullet)
  52.             bullets.remove(bull)
  53.  
  54.         canvas.move(bullet, h_x, h_y)
  55.  
  56.  
  57. shot()
  58. while True:
  59.     root.update()
  60.     root.update_idletasks()
  61.     draw()
  62.     time.sleep(0.01)
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement