► |  | Teachpacks

1 | Ht DP Teachpacks
2 | Ht DC Teachpacks
3 | Ht DP/ 2e Teachpacks

▼ | 1 | Ht DP Teachpacks

1.1 | Manipulating Images: "image.ss"
1.2 | Simulations and Animations: "world.ss"
1.3 | Converting Temperatures: "convert.ss"
1.4 | Guessing Numbers: "guess.ss"
1.5 | Master Minding: "master.ss"
1.6 | Simple Drawing: "draw.ss"
1.7 | Hangman: "hangman.ss"
1.8 | Managing Control Arrows: "arrow.ss"
1.9 | Manipulating Simple HTML Documents: "docs.ss"
1.10 | Working with Files and Directories: "dir.ss"
1.11 | Graphing Functions: "graphing.ss"
1.12 | Simple Graphical User Interfaces: "gui.ss"
1.13 | An Arrow GUI: "arrow-gui.ss"
1.14 | Controlling an Elevator: "elevator.ss"
1.15 | Queens: "show-queen.ss"
1.16 | Matrix Operations: "matrix.ss"

On this page:

control-left-right
control-up-down
control

Version: 4.1.4
top← prev up next →

#### 1.8`` Managing Control Arrows: "arrow.ss"

The teachpack implements a controller for moving shapes across a canvass. A shape is a class of data for which move and draw operations can be drawn.

(control-left-right shape n move draw) → true
shape : Shape
n : number?
move : (-> number? Shape Shape)
draw : (-> Shape true)

Moves shape n pixels left (negative) or right (positive).

(control-up-down shape n move draw) → true
shape : Shape
n : number?
move : (-> number? Shape Shape)
draw : (-> Shape true)

Moves shape n pixels up (negative) or down (positive).

(control shape n move-lr move-ud draw) → true
shape : Shape
n : number?
move-lr : (-> number? Shape Shape)
move-ud : (-> number? Shape Shape)
draw : (-> Shape true)

Moves shape N pixels left or right and up or down, respectively.

Example:

; A s h a p e i s a s t r u c t u r e:
; (m a k e-p o s n n u m n u m)
; R A D : t h e r a d i u s o f t h e s i m p l e d i s k m o v i n g a c r o s s a c a n v a s
(define RAD 10)
; m o v e : n u m b e r s h a p e -> s h a p e o r f a l s e
; t o m o v e a s h a p e b y d e l t a a c c o r d i n g t o t r a n s l a t e
; e f f e c t: t o r e d r a w i t
(define (move delta sh)
(cond
[(and (clear-solid-disk sh RAD)
(draw-solid-disk (translate sh delta) RAD))
(translate sh delta)]
[else false]))
; t r a n s l a t e : s h a p e n u m b e r -> s h a p e
; t o t r a n s l a t e a s h a p e b y d e l t a i n t h e x d i r e c t i o n
(define (translate sh delta)
(make-posn (+ (posn-x sh) delta) (posn-y sh)))
; d r a w-i t : s h a p e -> t r u e
; t o d r a w a s h a p e o n t h e c a n v a s: a d i s k w i t h r a d i u s
(define (draw-it sh)
(draw-solid-disk sh RAD))
; R U N:
; t h i s c r e a t e s t h e c a n v a s
(start 100 50)
; t h i s c r e a t e s t h e c o n t r o l l e r G U I
(control-left-right (make-posn 10 20) 10 move draw-it)

top← prev up next →