Factory simulation
Here's a simulation of part of a factory assembly line. A conveyor belt runs continually, carrying pallets containing the product past a station where a test is performed. The aim is not to win prizes for graphics, just to demonstrate that EasyCoder is able to handle quite complex simulations with several things happening at the same time. A key element here is the alias command, which lets one variable be used to represent one of several alternatives.
The entire script is shown below. Those who are interested in such things may like to know that the script itself is about 10k bytes and the compiled version takes up about 70k bytes; similar to one medium-sized image. Compilation is very quick, typically only tens of milliseconds.
As this particular script has no interactivity and always runs the same way you might consider it could simply be replaced by an animated GIF. However, a real example would probably have some kind of user interaction that alters the way it runs. In fact, the appearance does depend on where it runs; on a smartphone the animation reformats itself to fit the screen width.
Graphics - Intro
Graphics example 2 - Calculator
Graphics example 3 - Wacky Drumkit
Graphics example 4 - Solitaire
! ! Boot script Boot require js `https://cdn.jsdelivr.net/gh/easycoder/easycoder.github.io/dist/plugins/svg.js` pre Main variable Script attach Main to `ec-main` put the content of Main into Script run Script
! Test simulation div EasyCoder div Container svg Stage group WheelGroup group WheelGroup1 group WheelGroup2 group PalletGroup group PalletGroup1 group PalletGroup2 group TesterGroup circle Wheel circle Wheel1 circle Wheel2 line Spoke line Spokes1 line Spokes2 rect Conveyor rect PalletRect rect PalletRect1 rect PalletRect2 rect ProductRect rect ProductRect1 rect ProductRect2 rect Barrier rect Barrier1 rect Barrier2 circle Photocell1 circle Photocell2 rect TesterRect line TesterProng1 line TesterProng2 variable Width variable S1 variable S2 variable N variable X variable PX variable Target1X variable Target2X variable BarrierY variable Barrier1Y variable Barrier2Y variable BarrierH variable Barrier1H variable Barrier2H variable PalletX variable Pallet1X variable Pallet2X variable Barrier1X variable Barrier2X variable Photocell1X variable Photocell2X variable TesterX variable TesterY variable TestStationCleared attach Container to `ec-container` img DuckThink if mobile begin attach DuckThink to `ec-duckthink` set style `display` of DuckThink to `none` put `100%` into Width set style `width` of Container to Width put the width of Container into Width end else put the width of Container into Width attach EasyCoder to `easycoder-script` set style `display` of EasyCoder to `block` create Stage in Container create Conveyor in Stage style `x:0;y:140;width:` cat Width cat `;height:20;fill:#ccc` alias WheelGroup to WheelGroup1 alias Wheel to Wheel1 alias Spoke to Spokes1 gosub to CreateWheel move WheelGroup to 60 160 alias WheelGroup to WheelGroup2 alias Wheel to Wheel2 alias Spoke to Spokes2 gosub to CreateWheel take 100 from Width giving X move WheelGroup to X 160 ! Slide-up barriers put 40 into Barrier1X create Barrier1 in Stage style `x:` cat Barrier1X cat `;y:115;width:5;height:45;fill:brown;stroke:#800` put 120 into Barrier2X create Barrier2 in Stage style `x:` cat Barrier2X cat `;y:115;width:5;height:45;fill:brown;stroke:#800` ! Create 2 pallets, each with a product alias PalletGroup to PalletGroup1 alias PalletRect to PalletRect1 alias ProductRect to ProductRect1 alias PalletX to Pallet1X gosub to CreatePallet alias PalletGroup to PalletGroup2 alias PalletRect to PalletRect2 alias ProductRect to ProductRect2 alias PalletX to Pallet2X gosub to CreatePallet set attribute `fill` of ProductRect2 to `yellow` ! Photocells take 12 from Barrier2X giving Photocell1X create Photocell1 in Stage style `cx:` cat Photocell1X cat `;cy:130;r:10;fill:#f00;stroke:gray;stroke-width:2` add 120 to Barrier2X giving Photocell2X create Photocell2 in Stage style `cx:` cat Photocell2X cat `;cy:130;r:10;fill:#f00;stroke:gray;stroke-width:2` ! The tester create TesterGroup in Stage create TesterRect in TesterGroup style `x:0;y:0;width:100;height:100;fill:#00f;stroke:#008;stroke-width:2` create TesterProng1 in TesterGroup style `x1:35;y1:100;x2:35;y2:120;stroke:red;stroke-width:5` create TesterProng2 in TesterGroup style `x1:65;y1:100;x2:65;y2:120;stroke:green;stroke-width:5` take 80 from Barrier2X giving TesterX move TesterGroup to TesterX -80 fork to Rotate set TestStationCleared wait 3 seconds ! Slide the pallet along to barrier 1 Loop: take 60 from Barrier1X giving Target1X fork to SlidePallet1 while Pallet1X is less than Target1X begin wait 2 ticks end ! Wait for the test station to be clear, then raise barrier 2 and lower barrier 1 wait 1 second while not TestStationCleared begin wait 10 ticks end alias Barrier to Barrier1 alias BarrierY to Barrier1Y alias BarrierH to Barrier1H gosub to LowerBarrier ! Slide the pallet to barrier 2. Wait for it to arrive. take 60 from Barrier2X giving Target1X fork to SlidePallet1 take 60 from Photocell1X giving PX while Pallet1X is less than PX begin wait 2 ticks end set attribute `fill` of Photocell1 to `#0f0` ! Raise barrier 1 again alias Barrier to Barrier1 alias BarrierY to Barrier1Y alias BarrierH to Barrier1H gosub to RaiseBarrier clear TestStationCleared ! Lower the tester put -80 into TesterY while TesterY is less than -50 begin move TesterGroup to TesterX TesterY add 1 to TesterY wait 2 ticks end put 0 into N while N is less than 20 begin set attribute `fill` of ProductRect1 to `yellow` wait 2 ticks set attribute `fill` of ProductRect1 to `orange` wait 2 ticks add 1 to N end ! Switch pallets and restart pallet 1 put PX into Pallet2X add 11 to Pallet2X move PalletGroup2 to Pallet2X 70 put -130 into Pallet1X move PalletGroup1 to Pallet1X 70 wait 50 ticks fork to Loop ! Retract the tester while TesterY is not less than -80 begin move TesterGroup to TesterX TesterY take 1 from TesterY wait 2 ticks end ! Lower barrier 2 alias Barrier to Barrier2 alias BarrierY to Barrier2Y alias BarrierH to Barrier2H gosub to LowerBarrier ! Let the pallet float away put Width into Target2X fork to SlidePallet2 while Pallet2X is less than Barrier2X begin wait 2 ticks end set attribute `fill` of Photocell1 to `#f00` ! Monitor Photocell 2 take 65 from Photocell2X giving X while Pallet2X is less than X begin wait 2 ticks end set attribute `fill` of Photocell2 to `#0f0` alias Barrier to Barrier2 alias BarrierY to Barrier2Y alias BarrierH to Barrier2H gosub to RaiseBarrier set TestStationCleared alias Barrier to Barrier1 alias BarrierY to Barrier1Y alias BarrierH to Barrier1H gosub to LowerBarrier while Pallet2X is less than Photocell2X begin wait 2 ticks end set attribute `fill` of Photocell2 to `#f00` stop !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! Slide a pallet to a given location SlidePallet1: while Pallet1X is less than Target1X begin move PalletGroup1 to Pallet1X 70 add 2 to Pallet1X wait 5 ticks end stop SlidePallet2: while Pallet2X is less than Target2X begin move PalletGroup2 to Pallet2X 70 add 2 to Pallet2X wait 5 ticks end stop !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! Raise a barrier RaiseBarrier: put 140 into BarrierY put 20 into BarrierH while BarrierH is not greater than 45 begin set attribute `y` of Barrier to BarrierY set attribute `height` of Barrier to BarrierH take 1 from BarrierY add 1 to BarrierH wait 1 tick end return !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! Lower a barrier LowerBarrier: put 115 into BarrierY put 45 into BarrierH while BarrierH is not less than 20 begin set attribute `y` of Barrier to BarrierY set attribute `height` of Barrier to BarrierH add 1 to BarrierY take 1 from BarrierH wait 1 tick end return !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! Create a pallet CreatePallet: put -61 into PalletX create PalletGroup in Stage create PalletRect in PalletGroup style `x:0;y:50;width:60;height:20;fill:#888;stroke:black;stroke-width:2` create ProductRect in PalletGroup style `x:5;y:0;width:50;height:49;fill:orange;stroke:black;stroke-width:2` move PalletGroup to PalletX 70 return !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! Create a wheel CreateWheel: set the elements of Spoke to 6 create WheelGroup in Stage create Wheel in WheelGroup style `cx:20;cy:20;r:20;fill:#aaa;stroke:rgb(100,50,0);stroke-width:3` index Spoke to 0 create Spoke in WheelGroup style `x1:0;x2:40;y1:20;y2:20;stroke:rgb(100,50,0);stroke-width:2` index Spoke to 1 create Spoke in WheelGroup style `x1:4;x2:36;y1:10;y2:29;stroke:rgb(100,50,0);stroke-width:2;visibility:hidden` index Spoke to 2 create Spoke in WheelGroup style `x1:12;x2:29;y1:3;y2:36;stroke:rgb(100,50,0);stroke-width:2;visibility:hidden` index Spoke to 3 create Spoke in WheelGroup style `x1:20;x2:20;y1:0;y2:40;stroke:rgb(100,50,0);stroke-width:2` index Spoke to 4 create Spoke in WheelGroup style `x1:10;x2:29;y1:36;y2:4;stroke:rgb(100,50,0);stroke-width:2;visibility:hidden` index Spoke to 5 create Spoke in WheelGroup style `x1:3;x2:36;y1:29;y2:12;stroke:rgb(100,50,0);stroke-width:2;visibility:hidden` return !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! Rotate the wheels under the conveyor Rotate: put 0 into S1 put 3 into S2 while true begin alias Spoke to Spokes1 index Spoke to S1 set attribute `visibility` of Spoke to `hidden` index Spoke to S2 set attribute `visibility` of Spoke to `hidden` alias Spoke to Spokes2 index Spoke to S1 set attribute `visibility` of Spoke to `hidden` index Spoke to S2 set attribute `visibility` of Spoke to `hidden` add 1 to S1 add 1 to S2 if S1 is 3 begin put 0 into S1 put 3 into S2 end alias Spoke to Spokes1 index Spoke to S1 set attribute `visibility` of Spoke to `visible` index Spoke to S2 set attribute `visibility` of Spoke to `visible` alias Spoke to Spokes2 index Spoke to S1 set attribute `visibility` of Spoke to `visible` index Spoke to S2 set attribute `visibility` of Spoke to `visible` wait 10 ticks end