Programming with Lhogho

Opening a Graphics Screen
                                    
                                                                                             
     
     
         

      

Opening a graphics screen and drawing a shape it probably something something you will want to do quite a lot.

(All the code here can copied and pasted into your editor to try it out.)

The first version.

The first bit of code is a fully working programme, but all it does is open a graphics screen ready for drawing. You can use it as a template for all your programmes.

;*************************  (Everything following a  semicolon is a comment and is ignored by Lhogho.)
load "turtle.lgo

animation
while keep.running
  [   
  ]


;*************************

For historical reasons, the triangle you see in the middle of the screen is called a 'turtle'. The Logo programing is famous for this particular graphics system called turtle graphics. 

The first two lines in this programme load the Turtle graphics library and initialise it. The next 3 lines starting with:  
while keep.running
create the programme loop. This means that everything between the two brackets [ ] is repeated over and over many times a second until the window is closed.

If we draw the same thing over and over then we get a static picture. If we change it slightly from one time to the next, we get a moving picture.

Next, we are actually going to draw something.  Lets take our template and add some turtle graphics.
I'm not going to explain all the commands because there are many places you can look them up. The FSMLogo help is a good place to start.


;*************************

load "turtle.lgo
setpc [0 0 40setbg [70 0 70]     ; ****change the pen colour and the background colour
                                                 ; we only need to do this once, so it is outside the programme loop
animation
while keep.running
  [
   cs fd 100                  ;we need to do this every time the screen is redrawn, so it is inside the loop

  ]

;*************************

A static picture using a function.

;*************************
load "turtle.lgo
setpc [0 0 40setbg [70 0 70]      ; ****change the pen colour and the background colour
                                                 ; we only need to do this once, so it is outside the programme loop

to sq :side                                  ;function to draw a square
   repeat 4[fd :side rt 90]
end

animation
while keep.running
  [
    
sq 200                                ;we need to do this every time the screen is redrawn, so it is inside the loop
  ]

;*************************

It starts to move.

;*************************
load "turtle.lgo
setpc [0 0 40setbg [70 0 70
      ; ****change the pen colour and the background colour
                                                 ; we only need to do this once, so it is outside the programme loop

make "angle 0                             ;set up a variable to store an angle

to sq :side                                  ;function to draw a square
   repeat 4[fd :side rt 90]
end
 
animation

while keep.running
   [
   make "angle :angle + 0.5          ;adjust the 0.5 to change the speed of rotation
   cs rt :angle                           ;we need to do this every time the screen is redrawn, so it is inside the loop
   
sq 200                                         ; draw a square of side 200
   ]

;*************************

The final version.

Finally, lets hide the turtle.

;*************************
load "turtle.lgo
setpc [0 0 40setbg [70 0 70
      ; ****change the pen colour and the background colour
ht                                                ; we only need to do this once, so it is outside the programme loop

make "angle 0                             ;set up a variable to store an angle

to sq :side                                  ;function to draw a square
   repeat 4[fd :side rt 90]
end
 
animation

while keep.running
   [
   make "angle :angle + 0.5
            ;adjust the 0.5 to change the speed of rotation   
   cs
rt :angle                           ;we need to do this every time the screen is redrawn, so it is inside the loop
   
sq 200                                         ; draw a square of side 200
   ]
;*************************