# wiggle2.py # # a few more tricks on top of wiggle.py # # new python functions: # # from random import randint # randint(low, high) low <= random num <= high # # from time import sleep # sleep(2) do nothing for 2 sec # # Clicking in the window once it's stopped will quit cleanly. # # Or type "control-C" at the command promopt at any time # to interrupt the program. # # Jim Mahoney | Marlboro College | Sep 2013 | MIT License from graphics import * from time import sleep from random import randint def main(): window = GraphWin("wiggle", 500, 500) dot = Circle(Point(300, 300), 10) dot.setFill("red") dot.draw(window) x = 300 y = 300 for i in range(200): dx = randint(-2, 2) # pixels per timestep dy = randint(-2, 2) for j in range(5): # line = Line(Point(x,y), Point(x+dx, y+dy)) line.setFill("grey") line.draw(window) x = x + dx y = y + dy # sleep(0.02) # in seconds dot.move(dx, dy) # one time step speed = randint(2, 5) dx = dx + randint(-speed, speed) dy = dy + randint(-speed, speed) wait = window.getMouse() main()