Created
December 19, 2016 12:37
-
-
Save sourceperl/02334c3e65af05ae61acc24b2f928f88 to your computer and use it in GitHub Desktop.
Tree grow simulator on python3 with tk
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 | |
# tree grow simulator on python3 with tk | |
import tkinter as tk | |
from random import randint | |
from enum import Enum | |
class TreePart(Enum): | |
none = 0 | |
trunk = 1 | |
branch = 2 | |
leaf = 3 | |
class TreeCell(object): | |
def __init__(self, x, y, size=2, part=TreePart.trunk): | |
self.x = x | |
self.y = y | |
self.size = size | |
self.part = part | |
@property | |
def color(self): | |
if self.part is TreePart.trunk or self.part is TreePart.branch: | |
return 'saddle brown' | |
elif self.part is TreePart.leaf: | |
return 'green' | |
def fork(self): | |
if self.part is TreePart.trunk: | |
return TreeCell(self.x + randint(-1, 1), self.y - randint(0, 2), size=self.size, part=TreePart.trunk) | |
elif self.part is TreePart.branch: | |
return TreeCell(self.x + randint(-5, 5), self.y - randint(0, 2), size=self.size, part=TreePart.branch) | |
elif self.part is TreePart.leaf: | |
return TreeCell(self.x + randint(-8, 8), self.y - randint(0, 2), size=self.size, part=TreePart.leaf) | |
class TreeApp(tk.Tk): | |
def __init__(self, *args, **kwargs): | |
tk.Tk.__init__(self, *args, **kwargs) | |
# configure main window | |
self.wm_title('Tree grow') | |
# cells | |
self.cells = [TreeCell(399, 600), TreeCell(400, 600), TreeCell(401, 600)] | |
# 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 cells' % len(self.cells), font=('verdana', 10, 'bold')) | |
# launch tree grow | |
self.grow() | |
def grow(self): | |
# do a cell duplicate on last 100 new cells | |
for c in list(self.cells[-100:]): | |
# new cell is a fork of current cell | |
nc = c.fork() | |
# type change for level: first is trunk then branch and finally leaf | |
if 450 < nc.y <= 600: | |
nc.part = TreePart.trunk | |
elif 200 < nc.y <= 450: | |
nc.part = TreePart.branch | |
elif nc.y <= 200: | |
nc.part = TreePart.leaf | |
# add new cell | |
self.cells.append(nc) | |
# draw new cell | |
self.add_cell(nc) | |
# update text | |
self.can.itemconfig(self.cell_txt, text='%d cells' % len(self.cells)) | |
# refresh | |
self.after(200, func=self.grow) | |
def clear(self): | |
self.can.delete('all') | |
def add_cell(self, cell): | |
""" | |
Draw a cell on canvas | |
:param cell: cell to draw | |
:type cell: TreeCell | |
""" | |
r = cell.size/2 | |
(x0, y0) = (cell.x - r, cell.y - r) | |
(x1, y1) = (cell.x + r + 1, cell.y + r + 1) | |
self.can.create_oval(x0, y0, x1, y1, fill=cell.color) | |
if __name__ == '__main__': | |
# main Tk App | |
app = TreeApp() | |
app.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment