% Name: PrInCe_
% Date: Mar. 26, 2003
% Purpose: Draws a "turtle" and moves it around the
% screen based on the users commands
% Inputs: Direction (direction), Distance (distance), User Command
% (command)
% Output: Turtle's movements shown on the "turtle window"
% fuction getCommand
% purpose: Gets the command from the user
% parameters: key
% return value: key
function getCommand : string (1)
var key : string (1)
put "What would you like to do? " ..
getch (key)
result key
end getCommand
% procedure goLeft
% purpose: changes the direction of the turtle to make it go left
% input parameters: none
% output parameters: none
procedure goLeft (var direction : int)
delay (500)
direction := direction + 90
if direction >= 360 then
direction := 0
end if
end goLeft
% procedure goRight
% purpose: changes the direction of the turtle to make it go right
% input parameters: none
% output parameters: none
procedure goRight (var direction : int)
delay (500)
direction := direction - 90
if direction = - 90 then
direction := 270
end if
end goRight
% procedure goForward
% purpose: moves the turtle forward depending on the direction being faced
% and the distance determined by the user
% input parameters: distance
% output parameters: none
procedure goForward (var x, y, win1, direction : int)
var distance : int
loop
locate (7, 1)
put "How far would you like to go ? " ..
get distance
exit when distance >= 0
locate (7, 31)
put " "
end loop
Window.Select (win1)
if direction = 270 then
Draw.Line (x, y, x + distance, y, black)
x := x + distance
elsif direction = 90 then
Draw.Line (x, y, x - distance, y, black)
x := x - distance
elsif direction = 180 then
Draw.Line (x, y, x, y - distance, black)
y := y - distance
else
Draw.Line (x, y, x, y + distance, black)
y := y + distance
end if
end goForward
% procedure quitProgram
% purpose: closes the program when the user decides to quit
% input parameters: none
% output parameters: none
procedure quitProgram (var win1, win2 : int)
Window.Close (win1)
Window.Select (win2)
Window.Close (win2)
Window.Select (0)
end quitProgram
% ---------- Main ----------
var direction : int := 0
var x, y : int
var command : string (1)
var win1, win2 : int
% turtle window
win1 :=
Window.Open ("graphics:1013;550,position:0;0,,noecho,nocursor,title:Turtle Window")
% command window
win2 := Window.Open ("graphics:1013;155,position:0;590,title:Command Window")
colorback (53)
cls
Window.Select (win2)
x := 506
y := 275
Window.Select (win1)
colorback (brightgreen)
cls
loop
Window.Select (win2)
cls
put "This turtle obeys only 4 commands"
put "f - go forward"
put "r - turn right"
put "l - turn left"
put "q - quit"
command := getCommand
if command = "l" or command = "L" then
goLeft (direction)
elsif command = "r" or command = "R" then
goRight (direction)
elsif command = "f" or command = "F" then
goForward (x, y, win1, direction)
elsif command = "q" or command = "Q" then
quitProgram (win1, win2)
end if
exit when command = "q" or command = "Q"
end loop
|