Created
March 8, 2018 08:42
-
-
Save arount/bab3f36a15bc2a83df067f10f2ca0d5c to your computer and use it in GitHub Desktop.
Ant poc
This file contains 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/python3 | |
from tkinter import Tk, Label | |
WIDTH = 10 | |
HEIGHT = 10 | |
ant_cell = (0, 0) | |
# Init Tkinter window | |
root = Tk() | |
# Matrix of cells | |
cells = [] | |
def move_ant(root, cells, x, y): | |
''' | |
Move ant on grid at position (x, y) | |
''' | |
global ant_cell | |
# Current position as old position | |
old_pos = ant_cell | |
# Clean text and style of old position cell | |
old_cell = cells[old_pos[0]][old_pos[1]] | |
old_cell['foreground'] = "black" | |
old_cell['text'] = "" | |
# Get cell at new position and set text | |
ant = cells[x][y] | |
ant['text'] = 'A' | |
# Update current ant position cell variable | |
ant_cell = (x, y) | |
# Init `cells` and Tkinter grid | |
for h in range(HEIGHT): | |
cells.append([]) | |
for w in range(WIDTH): | |
cell = Label( | |
root, | |
background="white", | |
foreground="black", | |
text="", | |
borderwidth=1, | |
width=2, | |
relief="sunken", | |
padx=0, | |
pady=0 | |
) | |
cell.grid(row=w, column=h) | |
cells[h].append(cell) | |
def animate(): | |
''' | |
Recursive animation function | |
''' | |
global root, cells, ant_cell | |
move_ant(root, cells, ant_cell[0] + 1, ant_cell[1] + 1) | |
root.after(200, animate) | |
# Init ant position | |
move_ant(root, cells, 5, 5) | |
# Run animation | |
root.after(200, animate) | |
# Run Tk | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment