Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_wave_ani_plus.py
- import tkinter as tk
- import math
- import random
- # create the tkinter window
- root = tk.Tk()
- root.title("Wave Animation")
- root.geometry("1200x200")
- # create the canvas
- canvas = tk.Canvas(root, width=1200, height=200, borderwidth=1, relief="solid", bg="blue")
- canvas.pack()
- num_waves = 20
- waves = []
- for i in range(num_waves):
- wave = {
- "x": random.uniform(-math.pi, math.pi),
- "y": 100,
- "speed": random.uniform(1, 10),
- "amplitude": random.uniform(10, 250),
- "frequency": random.uniform(0.0001, 0.1),
- "phase": random.uniform(0, 50 * math.pi)
- }
- waves.append(wave)
- def draw_water():
- canvas.delete("all")
- # draw the waves
- points = []
- for x in range(canvas.winfo_width()):
- y = 0
- for i in range(num_waves):
- wave = waves[i]
- wave["x"] -= random.uniform(0, 0.000001)
- fy = wave["y"] + math.sin(wave["phase"] + (x * math.sin(wave["x"])) * wave["frequency"]) * (wave["amplitude"])
- y += fy
- y /= num_waves
- points.append((x, y))
- points.append((canvas.winfo_width(), canvas.winfo_height()))
- points.append((0, canvas.winfo_height()))
- canvas.create_polygon(points, fill="darkblue", outline="black")
- # update the waves
- for i in range(num_waves):
- wave = waves[i]
- wave["phase"] += wave["speed"] * 0.01
- root.after(1, draw_water)
- draw_water()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement