Block-Slider

Block-Slider preview image

1 collaborator

Turtlezero2-white-048 James Steiner (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 6.2.2 • Viewed 47 times • Downloaded 1 time • Run 0 times
Download the 'Block-Slider' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

globals [
  ;; mouse-state variables
  click?
  down-patch
  up-patch
  ;; display variable
  pixel ;; set to fraction of patch = 1 pixel, i.e. 1 / patch-size
]

breed [ blocks block ]

blocks-own
[ state ;; "sliding" or anything else
  start ;; where block came from
  destination ;; where block is going
  travel-time;; how long it takes to get there
  arrival-time ;; arrival-time - timer used to slide blocks
]

to startup setup end

to-report color-from-who report 15 + 10 * (who mod 13) end

to setup
  clear-all
  set pixel 1 / patch-size
  ask patches
  [ sprout-blocks 1
    [ set color color-from-who
      set pcolor color + 2 ;; pale border around square
      set shape "square" ;; a funky square,
      set heading 90 * random 4 ;; display purposes only, for that funky look
    ]
  ]
  ;; remove one block
  ask one-of blocks [ set pcolor black  die ]
  set click? false
  reset-ticks
end 

;; use reporters to wrap tests in meaningful names.
;; this is what we mean by "self-documenting code"

to-report mouse-went-down? report (mouse-down? and not click?) end

to-report mouse-went-up? report (not mouse-down? and click?) end

to-report mouse-clicked? report (is-patch? down-patch and up-patch = down-patch) end

to do-click-action
  ;; if a turtle that is next to an empty patch is clicked,
  ;; start sliding that turtle
  if ( any? (blocks-on up-patch) with [ state != "sliding" ] )
  [ ;; get this block
    let this-block one-of blocks-on up-patch
    ;; gather empty patches
    let empty-patches [ neighbors4 with [ pcolor != gray and not any? turtles-here ] ] of this-block
    ;; any
    if any? empty-patches
    [ let empty-patch one-of empty-patches
      ask this-block
      [ set state "sliding"
        ask my-links [ die ]
        set destination empty-patch
        set start patch-here
        set travel-time slide-duration
        set arrival-time timer + (travel-time * .001)
        ask destination [ set pcolor black + .1 ]
        ask start [ set pcolor black ]
      ]
    ]
  ]
end 

to go-animation
  ask blocks with [ state = "sliding" ]
  [ ifelse ( timer >= arrival-time )
    [ ;; arrived
      set state "waiting"
      move-to destination
      ;; visuals
      set pcolor color + 2
      create-links-with ( (turtles-on neighbors4 ) with [ color = [ color ] of myself ])
      [ set shape "plain"
        set color [ color ] of myself - 2
        set thickness min (list 1 (5 * pixel) )
      ]
    ]
    [

      let ratio (arrival-time - timer) / ( travel-time * .001)
      set xcor [ pxcor ] of start * ratio + [ pxcor ] of destination * (1 - ratio)
      set ycor [ pycor ] of start * ratio + [ pycor ] of destination * (1 - ratio)
    ]
  ]
end 

to do-mouse-down
  set click? true
  set down-patch patch mouse-xcor mouse-ycor
  set up-patch nobody
end 

to do-mouse-up
  set click? false
  set up-patch patch mouse-xcor mouse-ycor
  if mouse-clicked? [ do-click-action ]
  set down-patch nobody
  set up-patch nobody
end 

to go
  ( ifelse
    ( mouse-went-down? ) [ do-mouse-down ]
    ( mouse-went-up? ) [ do-mouse-up ]
   )
   go-animation
end 


There are 2 versions of this model.

Uploaded by When Description Download
James Steiner 3 months ago Ne sliding code, slides occur over several GO, works better in commons, enables other effects, etc. Download this version
James Steiner 3 months ago Initial upload Download this version

Attached files

File Type Description Last updated
Block-Slider.png preview Preview for 'Block-Slider' 3 months ago, by James Steiner Download

This model does not have any ancestors.

This model does not have any descendants.