Digital Fidget Spinner

Post Reply
User avatar
rbytes
Posts: 1798
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone X
iPad 4
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Digital Fidget Spinner

Post by rbytes » Thu Apr 05, 2018 2:48 am

Fidget spinners are all the rage right now - so before they die out like the hula hoop and the disco floor, I present the Smart Basic digital version. Check the informative comments at the start of the code to learn how to operate it. It has several advantages over the real item.
1. You don't have to hold it while it spins.
2. You can flick or swipe it, but there is an easier way to spin it.
3. It will eventually coast to a stop, but it is as close to frictionless as it can be!

You will need to download the image attachments (except for the screen shots).

Works on all devices.

Code: Select all

/*
Digital Fidget Spinner
by rbytes, April 2018
Spin it with a downward swipe
on its right side to spin it clockwise, or
on its left side to spin it counterclockwise.
Slow it by pressing the spinner hub, or stop
the spin automatically with the brake button.
The spinner also has a friction algorithm 
that eventually slows it to a stop without
you doing anything.
Helpful hints:
1. You can increase the speed of the spinner
with multiple downward swipes. The touch detection
does NOT measure how fast you are moving your finger.
It simply increases spin speed as long as it detects a
touch. Handy for the very tired or the very lazy!
You can just hold your finger on the screen and rev up
to maximum speed!
2. You don't actually need to touch the spinner to
spin it. Anywhere on screen to the left or right of the
hub will work just as well.
3. And you don't actually have to touch the hub to 
slow down the spin. Anywhere from screen top to bottom
works, as long as it in within the same width band as
the hub.
4. The screen background color varies with the spin speed.
5. I had to cheat to get the blurred high-speed spinning effect.
There were just too many artifacts due to the screen refresh rate
(such as the backward-spinning hubcaps you see in a TV car chase.)
So I wrote a short Smart Basic routine to rotate and stamp the spinner
Many times to create a blurred image of it. As the spin speed increases,
the alpha of the blurred image sprite increases from 0 to 1, making
it fully visible. As speed decreases, the alpha gradually reduces
from 1 to 0, making the blurred spinner sprite fade out as the
spinner slows down.
6. The program runs on all iOS devices.
*/

OPTION SPRITE POS CENTRAL
SET BUTTONS CUSTOM
OPTION ANGLE DEGREES
SET TOOLBAR OFF
GET SCREEN SIZE sw,sh
/*sw=568!sh=320  iPhone test*/
rw=sw/1024!rh=sh/768
n$="spinner"
m$= "spinnera"
SPD=1
GRAPHICS
pal(SPD*10000,.5,1,.9)
GRAPHICS CLEAR pal.r,pal.g,pal.b
FIELD "spd" TEXT SPD AT 890*rw,300*rh SIZE 100*rw,40*rh
FIELD "spd" HIDE  ' you can view the sprite delay value if you wish, by uncommenting this line.
DRAW COLOR 0,0,0
SET BUTTONS FONT SIZE 20*rw
BUTTON "brake" TEXT "BRAKE" AT 900*rw,400*rh SIZE 90*rw,40*rh
BUTTON "quit" TEXT "QUIT" AT 900*rw,470*rh SIZE 90*rw,40*rh
SPRITE n$ LOAD "spinner2.png"
SPRITE n$ AT sw/2,sh/2 SCALE 1*(rw+rh)/2
SPRITE n$ SHOW
SPRITE n$ PLAY
SPRITE m$ LOAD "spinnera.png"
SPRITE m$ AT sw/2,sh/2 SCALE 1*(rw+rh)/2
SPRITE m$ ALPHA 0
SPRITE m$ SHOW
SPRITE "hub" LOAD "hub2.png"
SPRITE "hub" AT sw/2,sh/2 SCALE .5*(rw+rh)/2
SPRITE "hub" SHOW
SPD=.1
LOOP:
GET TOUCH 0 AS tx,ty
IF tx<>-1 THEN
  brake=0
  cnt=1
  SPRITE "spinner" PLAY
  IF NOT mids THEN SPD=SPD/2
  IF tx<sw/2-70*rw THEN      
    mids=0
    bwd=1
    IF fwd=1 THEN
      fwd=0
      SPD=.001
    ENDIF
    rot=-.5
  ENDIF
  IF tx>=sw/2-70*rw AND tx=<sw/2+70*rw THEN
    mids=1
    IF SPD <.1 THEN
      SPD=SPD*1.5
    ENDIF
  ENDIF
  IF tx>sw/2+70*rw THEN    
    mids=0
    fwd=1
    IF bwd=1 THEN
      bwd=0
      SPD=.001
    ENDIF
    rot=.5
  ENDIF
  SPRITE "spinner" DA rot
  SPRITE "spinner" DELAY SPD
ELSE
  IF SPD>.1 THEN
    SPD=.1
    cnt=0
    SPRITE "spinner" STOP
ENDIF
ENDIF
IF brake THEN
  mids=1
  IF SPD <.1 THEN
    SPD=SPD*1.5
  ENDIF
  ENDIF
IF SPD=>.1 THEN SPRITE "spinner" STOP
SPRITE "spinner" ALPHA SPD*1000
SPRITE "spinnera" ALPHA 1-SPD*1000
IF BUTTON_PRESSED("quit") THEN END
IF BUTTON_PRESSED("brake") THEN brake=1
IF cnt THEN SPD+=.005*SPD
IF SPD>.05 THEN!SPRITE "spinner" STOP!cnt=0!ENDIF
IF SPD<.0001 THEN SPD=.0001
IF SPD<.001 THEN pal(SPD*10000,.5,1,.9)
GRAPHICS CLEAR pal.r,pal.g,pal.b
SPRITE "spinner" DELAY SPD
FIELD "spd" TEXT SPD
PAUSE .1
GOTO LOOP

'r' ===== Palette function by Dutchman
DEF pal(Tint,SATURATION,Brighten,Intensity)
' ---- adjust parameter-values between zero and 1.
tint%=1
Sat=ABS(SATURATION)
IF Sat>1 THEN Sat%=1
Intensity=ABS(Intensity)
IF Intensity>1 THEN Intensity%=1
Brighten=ABS(Brighten)
IF Brighten>1 THEN Brighten%=1
' ---- divide in 6 sections
tint*=6 ! section=INT(0.5+tint) ! tint%=1
ON Section GOTO S1,S2,S3,S4,S5,S6
S1: ! B=1 ! R=0 ! G=tint ! GOTO Sat
S2: ! G=1 ! R=0 ! B=1-tint ! GOTO Sat
S3: ! G=1 ! B=0 ! R=tint ! GOTO Sat
S4: ! R=1 ! B=0 ! G=1-tint ! GOTO Sat
S5: ! R=1 ! G=0 ! B=tint ! GOTO Sat
S6: ! B=1 ! G=0 ! R=1-tint
Sat: ' with constant luminance
' ---- apply Saturation and Brighten
IF Sat<1 THEN
  Y=0.3*R+0.59*G+0.11*B
  U=B-Y ! U*=sat
  V=R-Y ! v*=sat
  W=G-Y ! w*=sat
  R=Y+V ! G=Y+W ! B=Y+U
  IF Brighten<>0 THEN
    Ysup=1-MAX(MAX(R,G),B)' max white to add
    Ysup*=Brighten
    R+=Ysup ! G+=Ysup ! B+=Ysup
  ENDIF
ENDIF
' ---- apply Intensity
IF Intensity<1 THEN
  R*=Intensity
  G*=Intensity
  B*=Intensity
ENDIF
Y=0.3*R+0.59*G+0.11*B
END DEF ' Palette
''
Attachments
9DF992EB-7B3A-4DC7-BA5E-08743E914566.png
9DF992EB-7B3A-4DC7-BA5E-08743E914566.png (1.85 MiB) Viewed 1516 times
EA6383B6-8EF9-4691-9B94-302318D6B5F5.png
EA6383B6-8EF9-4691-9B94-302318D6B5F5.png (2.01 MiB) Viewed 1516 times
FF41EEF6-B5B4-4335-9712-2965EF3B2034.png
FF41EEF6-B5B4-4335-9712-2965EF3B2034.png (2.09 MiB) Viewed 1516 times
spinnera.png
spinnera.png (2.13 MiB) Viewed 1516 times
spinner2.png
spinner2.png (1.1 MiB) Viewed 1516 times
hub2.png
hub2.png (285.15 KiB) Viewed 1516 times
####### Living the colorful life #######

Post Reply