#!/usr/bin/env python #-------------------------------------------------------------------- # animat4_new.py - by C.Fin 7/2013 - For Python2 & 3 #-------------------------------------------------------------------- try: import Tkinter as tk except ImportError: import tkinter as tk from random import randint import time #------------------------------------------------------------------------------ WID, HEI = 640, 480 # canvas dimension VERTEX = 4 # shape vertex 3 to n RINGS = 5 # shape trail 1 to n NSHAPES = 3 # number of shapes #------------------------------------------------------------------------------ def rand_velx(): return randint(4, 10) def rand_vely(): return randint(3, 8) def new_color(): return "#%02x%02x%02x" % tuple(randint(80, 255) for _ in range(3)) #------------------------------------------------------------------------------ class BouncingPoint: def __init__(self): self.x = randint(0, WID-1) self.y = randint(0, HEI-1) self.vx = rand_velx() self.vy = rand_vely() def move(self): x = self.x + self.vx y = self.y + self.vy if x < 0: x = 0 ; self.vx = rand_velx() if y < 0: y = 0 ; self.vy = rand_vely() if x >= WID: x = WID-1 ; self.vx = -rand_velx() if y >= HEI: y = HEI-1 ; self.vy = -rand_vely() self.x = x self.y = y #------------------------------------------------------------------------------ class Shape: def __init__(self, canvas): self.canvas = canvas self.points = [ BouncingPoint() for _ in range(VERTEX) ] self.rings = [ self.create_ring() for _ in range(RINGS) ] self.ring_pointer = 0 self.time = time.time() self.color = new_color() def create_ring(self): return [self.canvas.create_line(0, 0, 0, 0, fill="black") for _ in range(VERTEX)] def move(self): for point in self.points: point.move() new_time = time.time() if new_time - self.time >= 3: self.time = new_time self.color = new_color() lines = self.rings[self.ring_pointer] for j in range(VERTEX-1): self.canvas.itemconfig(lines[j], fill=self.color) self.canvas.coords( lines[j], self.points[j].x, self.points[j].y, self.points[j+1].x, self.points[j+1].y) self.canvas.itemconfig(lines[-1], fill=self.color) self.canvas.coords( lines[-1], self.points[-1].x, self.points[-1].y, self.points[0].x, self.points[0].y) self.ring_pointer = (self.ring_pointer + 1) % RINGS #------------------------------------------------------------------------------ class GUI: def __init__(self): self.form = tk.Tk() self.form.title("Animat4 - By C.Fin 2013") self.form.resizable(False, False) self.canvas = tk.Canvas( self.form, highlightthickness=0, width=WID, height=HEI, bg="black") self.canvas.pack() #------------------------------------------------------------------------------ class Animat4: def __init__(self): self.gui = GUI() self.shapes = [ Shape(self.gui.canvas) for _ in range(NSHAPES) ] def draw(self): self.gui.form.after(40, self.draw) for shape in self.shapes: shape.move() def start(self): self.draw() self.gui.form.mainloop() #------------------------------------------------------------------------------ app = Animat4() app.start()