The On-Screen Piano
This program creates a piano on screen that you can play with your mouse. There are several procedures. Some of them do the drawing, while others detect the click of a mouse and compute the key which the mouse is over.
The end the program, click the Stop sign.
Piano.lgo
; check the X and Y coordinates of the mouse to detect the key,
; and play the right tone if a hit was detected
TO DETECT
PLAY [T300]
IF (AND -225 < XCOR XCOR <= -205 0 < YCOR YCOR <= 120) PLAY [O3 C]
IF (AND -205 < XCOR XCOR < -195 0 < YCOR YCOR <= 40) PLAY [O3 C]
IF (AND -205 < XCOR XCOR <= -185 40 < YCOR YCOR <= 120) PLAY [O3 C#]
IF (AND -195 < XCOR XCOR < -165 0 < YCOR YCOR <= 40) PLAY [O3 D]
IF (AND -175 < XCOR XCOR < -155 40 < YCOR YCOR <= 120) PLAY [O3 D#]
IF (AND -165 < XCOR XCOR < -135 0 < YCOR YCOR <= 40) PLAY [O3 E]
IF (AND -155 < XCOR XCOR < -135 40 < YCOR YCOR <= 120) PLAY [O3 E]
IF (AND -135 < XCOR XCOR < -115 0 < YCOR YCOR <= 120) PLAY [O3 F]
IF (AND -115 < XCOR XCOR < -105 0 < YCOR YCOR <= 40) PLAY [O3 F]
IF (AND -115 < XCOR XCOR < -90 40 < YCOR YCOR <= 120) PLAY [O3 F#]
IF (AND -95 < XCOR XCOR < -85 40 < YCOR YCOR <= 120) PLAY [O3 G]
IF (AND -105 < XCOR XCOR < -75 0 < YCOR YCOR <= 40) PLAY [O3 G]
IF (AND -85 < XCOR XCOR < -65 40 < YCOR YCOR <= 120) PLAY [O3 G#]
IF (AND -65 < XCOR XCOR < -55 40 < YCOR YCOR <= 120) PLAY [O3 A]
IF (AND -75 < XCOR XCOR < -45 0 < YCOR YCOR <= 40) PLAY [O3 A]
IF (AND -55 < XCOR XCOR < -35 40 < YCOR YCOR <= 120) PLAY [O3 A#]
IF (AND -45 < XCOR XCOR < -15 0 < YCOR YCOR <= 40) PLAY [O3 B]
IF (AND -35 < XCOR XCOR < -25 40 < YCOR YCOR <= 120) PLAY [O3 B]
IF (AND -15 < XCOR XCOR <= 5 0 < YCOR YCOR <= 120) PLAY [O4 C]
IF (AND 5 < XCOR XCOR < 15 0 < YCOR YCOR <= 40) PLAY [O4 C]
IF (AND 5 < XCOR XCOR < 25 40 < YCOR YCOR <= 120) PLAY [O4 C#]
IF (AND 15 < XCOR XCOR < 45 0 < YCOR YCOR <= 40) PLAY [O4 D]
IF (AND 25 < XCOR XCOR < 35 40 < YCOR YCOR <= 120) PLAY [O4 D]
IF (AND 35 < XCOR XCOR < 55 40 < YCOR YCOR <= 120) PLAY [O4 D#]
IF (AND 45 < XCOR XCOR < 75 0 < YCOR YCOR <= 40) PLAY [O4 E]
IF (AND 55 < XCOR XCOR < 75 40 < YCOR YCOR <= 120) PLAY [O4 E]
IF (AND 75 < XCOR XCOR < 105 0 < YCOR YCOR <= 40) PLAY [O4 F]
IF (AND 75 < XCOR XCOR < 95 40 < YCOR YCOR <= 120) PLAY [O4 F]
IF (AND 95 < XCOR XCOR < 115 40 < YCOR YCOR <= 120) PLAY [O4 F#]
IF (AND 105 < XCOR XCOR < 135 0 < YCOR YCOR <= 40) PLAY [O4 G]
IF (AND 115 < XCOR XCOR < 125 40 < YCOR YCOR <= 120) PLAY [O4 G]
IF (AND 125 < XCOR XCOR < 145 40 < YCOR YCOR <= 120) PLAY [O4 G#]
IF (AND 135 < XCOR XCOR < 165 0 < YCOR YCOR <= 40) PLAY [O4 A]
IF (AND 145 < XCOR XCOR < 155 40 < YCOR YCOR <= 120) PLAY [O4 A]
IF (AND 155 < XCOR XCOR < 175 40 < YCOR YCOR <= 120) PLAY [O4 A#]
IF (AND 165 < XCOR XCOR < 195 0 < YCOR YCOR <= 40) PLAY [O4 B]
IF (AND 175 < XCOR XCOR < 195 40 < YCOR YCOR <= 120) PLAY [O4 B]
IF (AND 195 < XCOR XCOR < 225 0 < YCOR YCOR <= 120) PLAY [O5 C]
END
; draw a white key
TO WHITE.KEY
SETPC 15 (STAMPRECT 30 120 TRUE)
SETPC 7 REPEAT 2 [FD 120 RT 90 FD 30 RT 90]
END
; draw a black key
TO BLACK.KEY
SETPC 0 (STAMPRECT 20 81 TRUE)
SETPC 7 KEY.TOP
END
; draw the key top
TO KEY.TOP
PU FD 3 RT 90 FD 2 LT 90 PD
REPEAT 2 [FD 75 RT 90 FD 14 RT 90]
PU BK 3 RT 90 BK 2 LT 90 PD
END
; draw the key names
TO KEY.NAMES
SETPC 0
PU SETXY [-215 20] PD TT [C]
PU SETXY [-185 20] PD TT [D]
PU SETXY [-155 20] PD TT [E]
PU SETXY [-125 20] PD TT [F]
PU SETXY [-95 20] PD TT [G]
PU SETXY [-65 20] PD TT [A]
PU SETXY [-35 20] PD TT [B]
PU SETXY [-5 20] PD TT [C]
PU SETXY [25 20] PD TT [D]
PU SETXY [55 20] PD TT [E]
PU SETXY [85 20] PD TT [F]
PU SETXY [115 20] PD TT [G]
PU SETXY [145 20] PD TT [A]
PU SETXY [175 20] PD TT [B]
PU SETXY [205 20] PD TT [C]
SETPC 1
PU SETXY [-196 40] PD TT [C#]
PU SETXY [-166 40] PD TT [D#]
PU SETXY [-105 40] PD TT [F#]
PU SETXY [-76 40] PD TT [G#]
PU SETXY [-47 40] PD TT [A#]
PU SETXY [14 40] PD TT [C#]
PU SETXY [44 40] PD TT [D#]
PU SETXY [104 40] PD TT [F#]
PU SETXY [134 40] PD TT [G#]
PU SETXY [163 40] PD TT [A#]
END
TO 5B.KEYS
2B.KEYS RT 90 PU FD 30 LT 90 PD 2B.KEYS BLACK.KEY PU RT 90 FD 10 LT 90 PD
END
TO 2B.KEYS
REPEAT 2 [BLACK.KEY RT 90 PU FD 30 PD LT 90]
END
; draw the instructions
TO INSTRUCTIONS
PU SETXY [-150 -50] PD
TT [CLICK THE KEY TO PLAY]
PU SETXY [25 -50] PD TT [CLICK ON STOP ICON TO STOP]
END
; loop until a button click has been detected
TO POINT
IF BUTTON? [PU SETXY MOUSE PD STOP]
POINT
END
; the main loop detects a mouse click and plays the right note
TO SELECT.KEY
POINT
DETECT
SELECT.KEY
END
; the main program
TO PIANO
CS HT SETBG 3
PU SETXY [-225 0] PD
REPEAT 15 [WHITE.KEY RT 90 FD 30 LT 90]
PU SETXY [-205 40] PD
5B.KEYS RT 90 PU FD 50 PD LT 90 5B.KEYS
KEY.NAMES INSTRUCTIONS
SELECT.KEY
END
piano
Procedure | PIANO |
Description | Creates a clickable on-screen piano |
Level | Intermediate |
Tags | Music, Instruments, Play, Turtle, Drawing, Mouse |