Working with Colour

To change the colour of the pen you need to use the setpencolor command.

setpencolor (list 0 0 255)

This will change the pen color to blue.

Computer languages like Logo tend to work in American and so use color not colour.

Computer colours are listed by three numbers which give their RGB values.
RGB is short for Red Green and Blue.
These numbers take values between 0 and 255.

For black use:

setpencolor (list 0 0 0)

For white (first turn the screen white and then use) use:

setpencolor (list 255 255 255)

This program uses a for loop to generate a square which has a side equal to the counter on the repeat loop. The repeat loop counter also sets the color of the line used to draw the square.

repeat 255 [setpencolor (list repcount repcount repcount) for [ x 0 4 1] [fd repcount rt 90]]

This program uses two routines, circle and colcircle, to draw a set of nested coloured circles. colcirc 200 200

to circle :rad :steps
ht pu
right 90 setxy :rad 0 left 90 pd
make "step 2*PI*:rad/:steps
make "bend 360/:steps
repeat :steps [ fd :step lt :bend]
st
end

:rad is the radius of the circle
:step is the number of straight pieces that you will break the circumference of the circle into.
The turtle is then raised and moved away from the origin.
You then calculate how long each section needs to be and how much the turtle needs to bend between sections.

to colcirc :rad :steps
for [ r 0 :rad 1][
make "c (remainder :r 255)
make "c1 (remainder :r*7 255)
make "c2 (remainder :r*11 255)
setpencolor (list :c :c1 :c2 )
circle :r :steps ]
end

This program draws successive circles of increasing radii. The colours are set using the pencolor command acting on the remainders from dividing r by 3 different numbers. This program also uses a for loop.

The pattern of white dots has a cause. Can you work out what is causing it?

colcirc 200 1000

Filling Shapes with Colour

This code draws a square and fills it in. Remember to "save and exit" once you have put it in.

to s :size
repeat 4 [ fd :size rt 90]
pu rt 45 fd :size/2 setfc [0 200 0] fill bk :size/2 lt 45 pd
end

To run the code type:

s 100

The colour works by moving the turtle inside the square, setting the fillcolour, filling in and then returning to the point that the turtle was at previously.

Setting the Fill Color using an Index

This code also draws a square and fills it in. This time the code draws a square about the current turtle position. The code has two arguments; :size sets the edge of the square and :col 0-15 sets the fill color.

to s :size :col
pu bk :size/2 pd
lt 90
fd :size/2
repeat 3 [rt 90 fd :size]
rt 90 fd :size/2
pu rt 90 fd :size/2 pd
setfc :col fill
end

Test the code by typing s 200 2 etc.

Turmite with core code;
if :col = 0 [ s :size 1 lt 90 pu fd :size pd ]
if :col = 1 [ s :size 2 rt 90 pu fd :size pd ]
if :col = 2 [ s :size 3 rt 90 pu fd :size pd ]
if :col = 3 [ s :size 1 pu fd :size pd ]
See below for details.

Reading the Color from the Pixel Beneath the Turtle

Reading a color from the screen is an essential skill for basic games. This code reads the color from the turtle position and uses this to decide what color to leave behind and which direction to move the turtle off in.
This program is actually a Turmite; these are a whole class of little programs which were very important in the development of computer science.

to main :size
cs
setpensize [1 1]
setpencolor 7
setfloodcolor 0
fill
repeat 100000 [
make "col pixel
if :col = 0 [ s :size 1 lt 90 pu fd :size pd ]
if :col = 1 [ s :size 0 rt 90 pu fd :size pd ]
]
end

The pixel command returns the color value of the pixel at the turtle position.
The program then draws a square using s of the correct color before moving off in a given direction.
This particular turmite seems to wander at random for a few 1000 moves before locking into a repeating pattern.
The argument :size simply sets the cell size.


Turmites are capable of extremely complex behaviour. This example locks into a steady pattern that expands outwards forever. The example above shows regions of very ordered behaviour and other regions of seeming chaos. The interaction of order and chaos makes these fascinating systems to study.

Last updated 23rd February 2010