03.06.2013 Views

Turbo Prolog

Turbo Prolog

Turbo Prolog

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Program 34 uses standard graphics predicates to construct a "doodle pad." A border is<br />

drawn around the screen, and you can draw by pressing the CD (for Up), []J (for<br />

Down), El (for Left), and El (for Right) keys. Try the program out before examining<br />

the code. Keys other than ([), []J, !Il and [[) are ignored, except that pressing 0<br />

exits the program.<br />

/1 Program 3t; 1/<br />

predicates<br />

move(char,integer,integer,integer)<br />

start<br />

changestate(integer,integer)<br />

goal<br />

start<br />

clauses<br />

start:graphics(l,l,t;),<br />

line(1000,1000,1000,31000,2),<br />

line(1000,31000,31000,31000,2),<br />

line(31000,31000,31000,1000,2),<br />

line(31000,1000,1000,1000,2),<br />

changestate(15000,15000).<br />

changestate(X,Y):readchar(Z),move(Z,X,Y,Xl,Yl),changestate(Xl,Yl).<br />

move('r',X,31000,X,31000):- !.<br />

move('r',X,Yold,X,Ynew):- !,Ynew=Yold+l00,dot(X,Yold,3).<br />

move('l',X,1000,X,1000):- !.<br />

move('l',X,Yold,X,Ynew):- !,Ynew=Yold-l00,dot(X,Yold,3).<br />

move('v',1000,Y,1000,Y):- !.<br />

move('v';Xold,Y,Xnew,Y):- !,Xnew=Xold-l00,dot(Xold,y,3).<br />

move('d',31000,Y,31000,Y):- !.<br />

move('d',Xold,Y,Xnew,Y):- !,Xnew=Xold+l00,dot(Xold,Y,3).<br />

move('I',_,_,_,_):- !,exit.<br />

move(_,X,y,X,y).<br />

Turtle Graphics Commands<br />

Standard predicates that produce effects similar to Program 34 are built into <strong>Turbo</strong><br />

<strong>Prolog</strong>-the Turtle Graphics commands.<br />

When you enter graphics mode the screen is cleared, and a turtle appears in the<br />

middle of the screen facing the top of the screen vertically and with a "pen" attached to<br />

its tail. As the turtle is directed to move by various standard predicates, the "pen"<br />

leaves a trail on the screen.<br />

The effect of these predicates depends on<br />

• The position of the turtle<br />

• The direction of the turtle<br />

• Whether the "pen" is drawing (activated) or not<br />

• The color of the pen<br />

The standard predicate pendown activates the pen and penup deactivates it. Immediately<br />

after a call to graphics, the pen is activated. The color of the trail left by the pen<br />

Tutorial VI: Graphics and Sound 93

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!