Advertisement
here2share

# tk_wave_ani_plus.py

Apr 8th, 2023 (edited)
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.34 KB | None | 0 0
  1. # tk_wave_ani_plus.py
  2.  
  3. import tkinter as tk
  4. import math
  5. import random
  6.  
  7. # create the tkinter window
  8. root = tk.Tk()
  9. root.title("Wave Animation")
  10. root.geometry("1200x200")
  11.  
  12. # create the canvas
  13. canvas = tk.Canvas(root, width=1200, height=200, borderwidth=1, relief="solid", bg="blue")
  14. canvas.pack()
  15.  
  16. num_waves = 20
  17. waves = []
  18. for i in range(num_waves):
  19.     wave = {
  20.         "x": random.uniform(-math.pi, math.pi),
  21.         "y": 100,
  22.         "speed": random.uniform(1, 10),
  23.         "amplitude": random.uniform(10, 250),
  24.         "frequency": random.uniform(0.0001, 0.1),
  25.         "phase": random.uniform(0, 50 * math.pi)
  26.     }
  27.     waves.append(wave)
  28.  
  29. def draw_water():
  30.     canvas.delete("all")
  31.    
  32.     # draw the waves
  33.     points = []
  34.     for x in range(canvas.winfo_width()):
  35.         y = 0
  36.         for i in range(num_waves):
  37.             wave = waves[i]
  38.             wave["x"] -= random.uniform(0, 0.000001)
  39.             fy = wave["y"] + math.sin(wave["phase"] + (x * math.sin(wave["x"])) * wave["frequency"]) * (wave["amplitude"])
  40.             y += fy
  41.         y /= num_waves
  42.         points.append((x, y))
  43.     points.append((canvas.winfo_width(), canvas.winfo_height()))
  44.     points.append((0, canvas.winfo_height()))
  45.     canvas.create_polygon(points, fill="darkblue", outline="black")
  46.  
  47.     # update the waves
  48.     for i in range(num_waves):
  49.         wave = waves[i]
  50.         wave["phase"] += wave["speed"] * 0.01
  51.        
  52.     root.after(1, draw_water)
  53.  
  54. draw_water()
  55.  
  56. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement