Post by B+ on Nov 24, 2018 4:28:12 GMT
I started out attempting the Honeycomb Rosetta challenge but thought this twist more interesting:
'Honeycomb Rosetta started for JB v2 2018-11-23 B+
' I changed mid course following Rosetta specs to making this Hexagonal keys of type writer.
' So the letters are not scrambled, you can type or click them more than once and type out a
' fine little message under the keyboard, quit when you fill up 2 lines or before.
' all caps for globals
global H$, XMAX, YMAX, PI, PI3, SQR3, SIDE, LMAX, SELECTED$, ROWS, YOFF, LKEY$, LASTI, MAXSELECTED
' important constants for Hexagon making,
' hexagon side is 30 pixels with rows and cols and offsets > sets screen size
H$ = "gr" : PI = acs(-1) : PI3 = PI/3 : SQR3 = sqr(3) : SIDE = 30
cols = 12 : ROWS = 8 : LMAX = cols * ROWS
dim lx(LMAX), ly(LMAX) 'save positions of hex centers by index
xoff = 100 - 1.5 * SIDE : YOFF = 100 - SIDE * SQR3
LKEY$ = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789.,?!:;'@#$%^&*=_+-<>(){}[]\/| "
' The screen size depends on number of rows and cols
XMAX = 2 * xoff + cols * 1.5 * SIDE + 1.5 * SIDE
YMAX = 2 * YOFF + (ROWS + 3) * SIDE * SQR3
MAXSELECTED = int((XMAX - 20) / 16) 'about how many letters will fit across the screen with font size
nomainwin
WindowWidth = XMAX + 8
WindowHeight = YMAX + 32
UpperLeftX = (DisplayWidth - XMAX) / 2
UpperLeftY = (DisplayHeight - YMAX) / 2
open "Honeycomb - Rosetta Challenge" for graphics_nsb_nf as #gr
#gr "setfocus"
#gr "trapclose quit"
#gr "when leftButtonUp lButtonUp"
#gr "when characterInput charIn"
#gr "font consolus bold 22"
#gr "down"
#gr "color black"
#gr "size 1"
'initialize screen and hex key positions
for y = 1 to ROWS
for x = 1 to cols
n = n + 1
if x mod 2 = 0 then yoff2 = .5 * SIDE * SQR3 else yoff2 = 0
scan
cx = x * 1.5 * SIDE + xoff : cy = y * SIDE * SQR3 + YOFF + yoff2
lx(n) = cx : ly(n) = cy
call drawHex n, "green"
next
next
wait
sub lButtonUp H$, mx, my 'must have handle and mouse x,y
'calc distance from button and if within radius that was clicked
for i = 1 to LMAX
if sqr((mx - lx(i))^2 + (my - ly(i))^2) <= SIDE * .5 * SQR3 then
'notice str$(i) + " is close!"
if LASTI <> 0 then call drawHex LASTI, "green"
call drawHex i, "blue"
exit sub
end if
next
end sub
sub drawHex i, c$
#gr "size 1"
#gr "backcolor ";c$
#gr "place ";lx(i) + .5;" ";ly(i);"; circlefilled ";SIDE * .5 * SQR3
#gr "color black"
#gr "size 10"
call Hex lx(i), ly(i)
call stext lx(i) - 10, ly(i) + 11, mid$(LKEY$, i, 1)
if c$ = "blue" then
SELECTED$ = SELECTED$ + mid$(LKEY$, i, 1)
call stext 10, (ROWS + 2) * SIDE * SQR3 + YOFF, mid$(SELECTED$, 1, MAXSELECTED - 1)
call stext 10, (ROWS + 3) * SIDE * SQR3 + YOFF, mid$(SELECTED$, MAXSELECTED)
LASTI = i
end if
end sub
sub charIn H$, c$
place = instr(LKEY$, c$)
if place > 0 and place <= LMAX then
if LASTI <> 0 then call drawHex LASTI, "green"
call drawHex place, "blue"
end if
end sub
sub Hex x0, y0
for i = 0 to 6
x1 = x0 + SIDE * cos(i * PI3)
y1 = y0 + SIDE * sin(i * PI3)
if i > 0 then #gr "line ";lastx;" ";lasty;" ";x1;" ";y1
lastx = x1 : lasty = y1
next
end sub
sub stext x, y, message$ 'note: have to reset fore or back color after ink
#gr "place ";x;" ";y;";|";message$
end sub
sub quit H$
close #H$
end
end sub