############################################################################ # # File: worms.icn # # Subject: Program to simulate moving worm with mutable colors # # Author: Ralph E. Griswold # # Date: November 24, 1998 # ############################################################################ # # This file is in the public domain. # ############################################################################ # # This program demonstrates the use of mutable colors to produce a # "moving worm". # ############################################################################ # # Requires: Version 9 graphics # ############################################################################ # # Links: graphics # ############################################################################ link graphics $define BackGround "black" $define LightColor "yellow" $define Side 15 # worm width $define Length 5 # units per worm $define Pause 50 # animation delay $define Gap 1 # units between worms $define Lights 5 # number of lights in segment $define Width (10 * Side) # width of window $define Height (5 * Side) # height of window global colors procedure main() local x, y, unit, loci, locus, i WOpen("size=" || Width || "," || Height, "bg=" || BackGround) | stop("*** cannot open window") # Get mutable colors. colors := [] every 1 to Lights do put(colors, NewColor(BackGround)) | stop("*** cannot get mutable color") # Array of locations. loci := [ [[1, 1], [2, 5], [3, 1], [6, 3], [8, 4], [10, 1]], # 1 [[1, 2], [3, 5], [4, 1], [6, 4], [8, 3], [10, 2]], # 2 [[1, 3], [3, 4], [5, 1], [6, 5], [8, 2], [10, 3]], # 3 [[1, 4], [3, 3], [6, 1], [7, 5], [8, 1], [10, 4]], # 4 [[1, 5], [3, 2], [6, 2], [8, 5], [9, 1], [10, 5]] # 5 ] # Assign mutable colors to locations. every i := 1 to Length do every locus := !loci[i] do light ! push(locus, i) # Run the worms. until WQuit() do { every Color(colors[1 to Length - Gap], LightColor) every Color(colors[Length - Gap + 1 to Length], BackGround) WDelay(Pause) put(colors, get(colors)) # rotate colors } end # Create grid. procedure light(color, r, c) Fg(colors[color]) FillRectangle(Side * (r - 1), Side * (c - 1), Side, Side) return end