Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- from tkinter import *
- from time import sleep
- from random import randint
- SQ_SIZE = 800
- SQ_STEP = 10
- gridMap = {}
- class Color(object):
- """
- Color, should be an Enum, but this was written in Python3.3
- """
- BLACK = 'black'
- WHITE = 'white'
- class Direction(object):
- """
- Direction, should be an Enum, but this was written in Python3.3
- """
- UP = 90
- LEFT = 180
- DOWN = 270
- RIGHT = 0
- class Coord(object):
- """
- Defines a coordinate with a (x, y) point.
- Method get_key() returns a string on the form (x,y)
- """
- x,y = int, int
- def __init__(self, x, y):
- self.x = x
- self.y = y
- def get_key(self):
- """
- returns a string on the form (x,y)
- """
- return str.format("{0},{1}", self.x, self.y)
- class Point(object):
- """
- A Point defines an object on the Canvas (note only rectangles as of now)
- """
- ID = str
- top_left = Coord
- bot_right = Coord
- color = object
- def __init__(self, ID, x, y, color):
- self.ID = ID
- self.top_left = Coord(x, y)
- self.bot_right = Coord(x + SQ_STEP, y + SQ_STEP)
- self.color = color
- w.create_rectangle(self.top_left.x, self.top_left.y, self.bot_right.x, self.bot_right.y, fill=self.color)
- def updateColor(self):
- """
- Flips the coler of the object from balck to white and vica versa
- """
- if self.color is Color.BLACK:
- self.color = Color.WHITE
- elif self.color is Color.WHITE:
- self.color = Color.BLACK
- else:
- print("Error in updating colors")
- w.create_rectangle(self.top_left.x, self.top_left.y, self.bot_right.x, self.bot_right.y, fill=self.color)
- class Ant(object):
- """
- The object of them all!! Walks around a flips colors of Points
- """
- direction, locationKey = object, Coord
- def __init__(self, direction = None, loctaionKey = None):
- if direction is None and loctaionKey is None:
- self.direction = Direction.UP
- self.locationKey = Coord(400, 400)
- elif direction is not None and loctaionKey is None:
- self.direction = direction
- self.locationKey = Coord(400, 400)
- elif direction is not None and loctaionKey is not None:
- self.direction = direction
- self.locationKey = loctaionKey
- gridMap[self.locationKey.get_key()].updateColor()
- def move(self):
- """
- Take a step, and flip a point
- """
- if gridMap[self.locationKey.get_key()].color is Color.BLACK:
- turn = 90
- else:
- turn = 270
- self.direction = (self.direction + turn) % 360
- if self.direction == Direction.UP:
- self.locationKey.y += -SQ_STEP
- elif self.direction == Direction.DOWN:
- self.locationKey.y += SQ_STEP
- elif self.direction == Direction.LEFT:
- self.locationKey.x += -SQ_STEP
- elif self.direction == Direction.RIGHT:
- self.locationKey.x += SQ_STEP
- gridMap[self.locationKey.get_key()].updateColor()
- def run():
- """
- Thread for ant walking
- """
- ant = Ant(Direction.UP, Coord(int(SQ_SIZE / 2 + 10), int(SQ_SIZE / 2 + 10)))
- for i in range(0,11000):
- ant.move()
- if not i % 1:
- w.update_idletasks()
- sleep(0.01)
- print("POST SCRIPT")
- w.postscript(file="Langton's ant.eps")
- print("Jobs done !")
- if __name__ == '__main__':
- master = Tk()
- w = Canvas(master, width=SQ_SIZE, height=SQ_SIZE)
- w.pack()
- for x in range(0, SQ_SIZE, SQ_STEP):
- for y in range (0, SQ_SIZE, SQ_STEP):
- ID = Coord(x,y)
- gridMap[ID.get_key()] = Point(ID, x, y, Color.WHITE)
- w.after(500, run)
- w.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement