Home > MicroWorlds Logo Links & Examples > MicroWorlds Code Pages > move_turtle3 Code

move_turtle3 Code

move_turtle3.txt 2 KB


;  Purpose: To play with keyboard controls
;  Copyright © 2004 by Rupert Russell
;  e-mail: rupert.russell1@gmail.com
;  URL: http://www.rupert.id.au/microworlds/
;  Created: October 22, 2004

;  This program is free software; you can redistribute it and/or modify
;  it under the terms of the GNU General Public License as published by
;  the Free Software Foundation; either version 2 of the License, or
;  (at your option) any later version.
;
;  This program is distributed in the hope that it will be useful,
;  but WITHOUT ANY WARRANTY; without even the implied warranty of
;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;  GNU General Public License for more details.
;
;  You should have received a copy of the GNU General Public License
;  along with this program; if not, write to the Free Software
;  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
; ----------------------------------------------------------------------

to startup
   ; this procedure is called each time the project is loaded
   ; it sets up default values

   make "true 1
   make "false 0

   make "step 50
   make "pensize 5
   setpensize :pensize
   note 127 1

   t_info, st
       ; show the instructions turtle
   make "show_info :true
   setsh_all_off

   make "offset 5
   make "browns 999
   make "blues 100
   make "greens 60
   make "yellows 45
   make "reds 12
   make "magentas 115

   greens
           ; set the default brush to greens
   line_up_turtles
   
end

to move_turtle

   pd
   make "ch ascii readchar

   if :ch = 38 [t1, seth 0 fd :step]    
; north
   if :ch = 40 [t1, seth 180 fd :step]
 ; south
   if :ch = 39 [t1, seth 90 fd :step]  
 ; east
   if :ch = 37 [t1, seth 270 fd :step]  
; west

   ; >. key increases the step size the 
   if :ch = 46 [
      make "step (:step + 1) 
      cc 
      show (se "step "size :step) 
      show se "pensize :pensize
   ]

   ; <, key reduces the step size the 
   if :ch = 44 [ 
      if :step > 1 [
         make "step (:step - 1) 
         cc 
         show (se "step "size :step) 
         show se "pensize :pensize
      ]
   ]

   ; increase pensize
   if :ch = 120 [
      if :pensize < 100 [
         make "pensize :pensize + 1
         setpensize :pensize
         cc 
         show (se "step "size :step) 
         show se "pensize :pensize
      ]
   ]

   ; reduce pensize
   if :ch = 122 [ 
      if :pensize > 1 [
         make "pensize :pensize - 1
         cc 
         show (se "step "size :step) 
         show se "pensize :pensize
      ]
      setpensize :pensize
   ]

   setc (random :offset) + :colour 
; change the colour

   move_turtle
end

to go

   t_info, ht
  ; hide the instructions turtle
   snapshot     
; record the current screen
   make "show_info :true
   move_turtle
end

to line_up_turtles

  ; align and distribute the 15 coloured turtles
  make "x_offset 132
  make "y_offset -198
  make "count 0
  repeat 15 [
    run (word char 32  "t_show_unknown_colour_  
      :count ", char 32 "setx char 32 :x_offset + :count * 8)
    run (word char 32  "t_show_unknown_colour_  
       :count ", char 32 "sety char 32 :y_offset)
  make "count :count + 1
 ]
end

to setsh_all_off

  ; set all turtles to "off
  t_browns, setsh "browns_off
  t_random, setsh "random_off
  t_blues,  setsh "blues_off
  t_greens, setsh "greens_off
  t_yellows, setsh "yellows_off
  t_reds, setsh "reds_off
  t_magentas, setsh "magentas_off
  t_unknown, setsh "unknown_off
end

to blues

  setsh_all_off
  t_blues, setsh "blues_on
  make "offset 5
  make "colour :blues
  show_colours :colour :offset
end

to magentas
  setsh_all_off
  t_magentas, setsh "magentas_on
  make "offset 8
  make "colour :magentas
  show_colours :colour :offset
end

to reds

  setsh_all_off
  t_reds, setsh "reds_on
  make "offset 5
  make "colour :reds
  show_colours :colour :offset
end

to yellows

  setsh_all_off
  t_yellows, setsh "yellows_on
  make "offset 3
  make "colour :yellows
  show_colours :colour :offset
end

to browns

  setsh_all_off
  t_browns, setsh "browns_on
  make "offset 5
  make "colour :browns
  show_colours :colour :offset
end

to greens

  setsh_all_off
  t_greens, setsh "greens_on
  make "offset 5
  make "colour :greens
  show_colours :colour :offset
end

to random_colour

  ; every line is a different random colour
  setsh_all_off
  t_random, setsh "random_on
  make "offset 999
  make "colour :blues
  show_random_colours 
end

to unknown

  ; pick a random range of colours
  setsh_all_off
  t_unknown, setsh "unknown_on
  make "offset 15
  make "colour random 999
  show_colours :colour :offset
end

to show_random_colours

   
 ; set the colour of each turtle in the plalet to a random colour
    make "count 0
    repeat 15 [
      run (word "t_show_unknown_colour_  :count ", 
         char 32 "setc char 32 random 999)
    make "count :count + 1
   ]
 
     ; show all the coloured turtles
    make "count 0
    repeat 15 [
      run (word "t_show_unknown_colour_  :count ", char 32 "st)
    make "count :count + 1
   ]
end

to show_colours :colour :offset

   ; displays the current colours using a pallet of up to 15 turtles

   ; hide all the turtles in the pallet
   make "count 0
   repeat 15 [
      run (word "t_show_unknown_colour_  :count ", char 32 "ht)
    make "count :count + 1
   ]

    ; set the colour of each turtle in the pallet
    make "count 0
    repeat :offset [
      run (word "t_show_unknown_colour_  :count ", 
         char 32 "setc char 32 :colour + :count)
    make "count :count + 1
   ]

  
  ; show only the correct number of turtles in the current pallet
    make "count 0
    repeat :offset [
      run (word "t_show_unknown_colour_  :count ", char 32 "st)
    make "count :count + 1
   ]
end

to ?

   ; toggle the instructions 
   ifelse :show_info = :true [
      t_info, st
            ; show the instructions
      make "show_info :false
; don't show them next time
   ]
   [
      t_info, ht             
; hide the instructions
      make "show_info :true
 ; show them next time
   ]
  
end


APA citation:
Russell, R. (2016, July 04, 04:17 pm). move_turtle3 code.
     Retrieved April 17, 2024, from http://www.rupert.id.au/microworlds/code/move_turtle3.php



398 Visits since October 23, 2004