def setup():
size(600, 600)
background(50)
def draw():
draw_me_a_line()
def draw_me_a_line(colour=200):
stroke(colour)
line(random(width), random(height),
random(width), random(height))
SimpleForm.py
class Form:
def __init__(self, x, y, r): # Constructor
self.x = x; self.y = y # Set x and y position
self.rad = r # Set radius
self.x_speed = random(-10, 10) # Set random x speed
self.y_speed = random(-10, 10) # Set random y speed
def update_me(self):
self.x = (self.x + self.x_speed) % width # Moves x and wrap
self.y = (self.y + self.y_speed) % height # Moves y and wrap
def draw_me(self):
stroke(200)
point(self.x, self.y) # Draw a dot
noStroke(); fill(200,50)
circle(self.x, self.y, self.rad) # Draw a circle
def line_to(self, other):
stroke(200)
line(self.x, self.y, other.x, other.y)
运行
from SimpleForm import Form
def setup():
size(600, 600)
background(50)
global f1, f2 # Make f1 and f2 visible
f1 = Form(random(width), random(height), 10)
f2 = Form(random(width), random(height), 10)
def draw():
f1.update_me() # Update f1 position
f2.update_me() # Update f2 position
f1.draw_me() # Draw f1
f2.draw_me() # Draw f2
f1.line_to(f2) # Draw line from f1 to f2