Created
December 22, 2016 16:21
-
-
Save sourceperl/3893820eea96b013b1eeaf7683c54f48 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# particle simulator on python3 with tk | |
import tkinter as tk | |
import math | |
class Particle(object): | |
def __init__(self, x, y, size=2, color='saddle brown'): | |
self.x = x | |
self.y = y | |
self.angle = 0 | |
self.size = size | |
self.color = color | |
def move(self): | |
# self.y += 1 | |
# self.x = math.sqrt(1 - (self.y - self.x**2)) | |
self.x += (math.cos(math.radians(self.angle)) * 5) | |
self.y += (math.sin(math.radians(self.angle)) * 5) | |
# print('angle:%i, x:%i, y:%i' % (self.angle, self.x, self.y)) | |
self.angle = self.angle + 1 if self.angle < 359 else 0 | |
class DrawApp(tk.Tk): | |
def __init__(self, *args, **kwargs): | |
tk.Tk.__init__(self, *args, **kwargs) | |
# configure main window | |
self.wm_title('Particle') | |
# cells | |
self.particles = [Particle(200, 200, size=10), Particle(250, 250, size=15),] | |
# canvas | |
self.can = tk.Canvas(self, bg='grey', width=800, height=600) | |
self.can.pack() | |
# create text | |
self.cell_txt = self.can.create_text(680, 80, text='%d deg' % self.particles[0].angle, | |
font=('verdana', 10, 'bold')) | |
# launch tree grow | |
self.animate() | |
def animate(self): | |
# wipe all | |
# self.draw_wipe() | |
# draw all particles | |
for p in list(self.particles): | |
# move and draw particle | |
p.move() | |
self.draw_particle(p) | |
# update text | |
self.can.itemconfig(self.cell_txt, text='%d deg' % self.particles[0].angle) | |
# refresh | |
self.after(50, func=self.animate) | |
def draw_wipe(self): | |
self.can.delete('all') | |
def draw_particle(self, particle): | |
""" | |
Draw a cell on canvas | |
:param particle: Particle to draw | |
:type particle: Particle | |
""" | |
r = particle.size / 2 | |
(x0, y0) = (particle.x - r, particle.y - r) | |
(x1, y1) = (particle.x + r + 1, particle.y + r + 1) | |
self.can.create_oval(x0, y0, x1, y1, fill=particle.color) | |
if __name__ == '__main__': | |
# main Tk App | |
app = DrawApp() | |
app.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment