to setup ca set-default-shape turtles "circle" draw-walls cct 5 ;; create some turtles [ randomize ] ;; set random coordinates and random direction end ; draws the boundaries (walls) of the "billiard table" to draw-walls ; draw left and right walls ask patches with [abs pxcor = screen-edge-x] [ set pcolor blue ] ; draw top and bottom walls ask patches with [abs pycor = screen-edge-y] [ set pcolor blue ] end ; set random coordinates and random direction to randomize setxy (random-float screen-size-x) (random-float screen-size-y) rt random-float 360 if pcolor = blue ; if it's on the wall... [ randomize ] ; ...try again end to go ;; turtle procedure every 0.5 - speed / 10 [ ifelse leave-trace? ;; the turtle puts its pen up or down depending on the [ pd ] ;; value of the LEAVE-TRACE? switch [ pu ] bounce fd 1 ] end ;; this procedure checks the coordinates and makes the turtles ;; reflect according to the law that the angle of reflection is ;; equal to the angle of incidence to bounce ;; turtle procedure ; check: hitting left or right wall? if (abs pxcor-of patch-ahead 1 = screen-edge-x) ; if so, reflect heading around x axis [ set heading (- heading) ] ; check: hitting top or bottom wall? if (abs pycor-of patch-ahead 1 = screen-edge-y) ; if so, reflect heading around y axis [ set heading (180 - heading) ] end @#$#@#$#@ GRAPHICS-WINDOW 293 10 631 369 20 20 8.0 1 10 1 1 1 CC-WINDOW 7 115 286 410 Command Center BUTTON 70 10 128 43 NIL setup NIL 1 T OBSERVER T BUTTON 141 10 196 43 go go T 1 T TURTLE T SWITCH 21 64 150 97 leave-trace? leave-trace? 1 1 -1000 SLIDER 163 64 255 97 Speed Speed 0.0 5.0 4.7 0.1 1 NIL @#$#@#$#@ WHAT IS IT? ------------ This demo shows how to make turtles bounce off the walls. @#$#@#$#@ default true 0 Polygon -7566196 true true 150 5 40 250 150 205 260 250 circle true 0 Circle -7566196 true true 10 10 283 @#$#@#$#@ NetLogo 2.0beta4 @#$#@#$#@ setup set leave-trace? true repeat 40 [ ask turtles [ go ] ] @#$#@#$#@ @#$#@#$#@