NumOne繪制火柴人程序
????????NumOne繪制火柴人的代碼分為四個(gè)部分:歡迎界面、練習(xí)足球和滑板、練習(xí)籃球和足球過(guò)人、瘋狂籃球。由于NumOne的代碼容量為3800個(gè)字符,雖然已經(jīng)滿足絕大多數(shù)的DIY應(yīng)用場(chǎng)景,但面對(duì)像動(dòng)態(tài)繪制火柴人這樣需編寫大量運(yùn)行代碼的程序,容量稍顯不足,所以分為4個(gè)小程序來(lái)實(shí)現(xiàn)。下面是火柴人繪制動(dòng)畫的程序,分為圖形程序以及自動(dòng)生成的js-arduino風(fēng)格的代碼。
一、繪制歡迎界面
? (1)圖形程序

(2)生成的代碼程序
? Liquid_I2C.link(18,19);
? while (true) {
? Liquid_I2C.print('??? Hello jsBlocks!');
? Liquid_I2C.drawRoundRect(32,24,64,40,4,0);
? Liquid_I2C.display();
? delay(200);
? Liquid_I2C.drawRoundRect(44,34,10,16,1,1);
? Liquid_I2C.drawRoundRect(74,34,10,16,1,1);
? Liquid_I2C.display();
? delay(200);
? Liquid_I2C.drawTriangle(48,24,48,16,57,24,1);
? Liquid_I2C.drawTriangle(48,24,48,16,44,20,1);
? Liquid_I2C.drawCircle(46,18,3,1);
? Liquid_I2C.display();
? delay(200);
? Liquid_I2C.drawTriangle(71,24,80,24,80,16,1);
? Liquid_I2C.drawTriangle(84,20,80,24,80,16,1);
? Liquid_I2C.drawCircle(82,18,3,1);
? Liquid_I2C.display();
? delay(1000);
? Liquid_I2C.clearDisplay();
}??
二、火柴人創(chuàng)建
(1) 火柴人繪制函數(shù)
????????創(chuàng)建了一個(gè)可以動(dòng)態(tài)繪制火柴人的函數(shù),函數(shù)共11個(gè)參數(shù):頭部中心中心位置x,頭部位置y,眼睛的狀態(tài)(共三種狀態(tài)1-雙眼顯示,2-顯示左眼,3-顯示右眼),左手狀態(tài)(共2種狀態(tài),1、2的區(qū)別為圓弧方向)、左手角度(與x的角度),右手狀態(tài),右手角度,左腳狀態(tài),左腳角度,右腳狀態(tài),右腳角度。三個(gè)火柴人動(dòng)畫程序種都是用的火柴人函數(shù),三個(gè)程序只是動(dòng)畫實(shí)現(xiàn)部分有區(qū)別。

(2) 生成的代碼程序
// 實(shí)現(xiàn)一個(gè)火柴人
function matchstickman(head_x, head_y, eye_t, l_hand_t, l_hand_a, r_hand_t, r_hand_a, l_leg_t, l_leg_a, r_leg_t, r_leg_a) {
? Liquid_I2C.drawCircle((head_x),(head_y),5,0);
? if ((eye_t) == 1) {
??? Liquid_I2C.drawCircle(((head_x) - 2),(head_y),1,1);
??? Liquid_I2C.drawCircle(((head_x) + 2),(head_y),1,1);
? }
? if ((eye_t) == 2) {
??? Liquid_I2C.drawCircle(((head_x) - 2),(head_y),1,1);
? }
? if ((eye_t) == 3) {
??? Liquid_I2C.drawCircle(((head_x) + 2),(head_y),1,1);
? }
? var body_end_y;
? body_end_y=((head_y) + 26);
? Liquid_I2C.drawLine((head_x),((head_y) + 5),(head_x),(body_end_y));
? var hand_start_y;
? var hand_end_x;
? var hand_end_y;
? hand_start_y=((head_y) + 10);
? hand_end_x=((head_x) - 20 * Math.sin((l_hand_a)/180*Math.PI));
? hand_end_y=((hand_start_y) + 20 * Math.cos((l_hand_a)/180*Math.PI));
? var pass_pnt_x;
? var pass_pnt_y;
? pass_pnt_x=(((head_x) + (hand_end_x)) / 2);
? pass_pnt_y=(((hand_start_y) + (hand_end_y)) / 2);
? if ((l_hand_t) == 1) {
??? Liquid_I2C.drawArc((head_x),(hand_start_y),(hand_end_x),(hand_end_y),((pass_pnt_x) - 4),(pass_pnt_y));
? }
? if ((l_hand_t) == 2) {
??? Liquid_I2C.drawArc((head_x),(hand_start_y),(hand_end_x),(hand_end_y),((pass_pnt_x) + 4),(pass_pnt_y));
? }
? hand_end_x=((head_x) + 20 * Math.sin((r_hand_a)/180*Math.PI));
? hand_end_y=((hand_start_y) + 20 * Math.cos((r_hand_a)/180*Math.PI));
? pass_pnt_x=(((head_x) + (hand_end_x)) / 2);
? pass_pnt_y=(((hand_start_y) + (hand_end_y)) / 2);
? if ((r_hand_t) == 1) {
??? Liquid_I2C.drawArc((head_x),(hand_start_y),(hand_end_x),(hand_end_y),((pass_pnt_x) + 4),(pass_pnt_y));
? }
? if ((r_hand_t) == 2) {
??? Liquid_I2C.drawArc((head_x),(hand_start_y),(hand_end_x),(hand_end_y),((pass_pnt_x) - 4),(pass_pnt_y));
? }
? var leg_end_x;
? var leg_end_y;
? leg_end_x=((head_x) - 20 * Math.sin((l_leg_a)/180*Math.PI));
? leg_end_y=((body_end_y) + 20 * Math.cos((l_leg_a)/180*Math.PI));
? pass_pnt_x=(((head_x) + (leg_end_x)) / 2);
? pass_pnt_y=(((body_end_y) + (leg_end_y)) / 2);
? if ((l_leg_t) == 1) {
??? Liquid_I2C.drawArc((head_x),(body_end_y),(leg_end_x),(leg_end_y),((pass_pnt_x) - 3),(pass_pnt_y));
? }
? if ((l_leg_t) == 2) {
??? Liquid_I2C.drawArc((head_x),(body_end_y),(leg_end_x),(leg_end_y),((pass_pnt_x) + 3),(pass_pnt_y));
? }
? leg_end_x=((head_x) + 20 * Math.sin((r_leg_a)/180*Math.PI));
? leg_end_y=((body_end_y) + 20 * Math.cos((r_leg_a)/180*Math.PI));
? pass_pnt_x=(((head_x) + (leg_end_x)) / 2);
? pass_pnt_y=(((body_end_y) + (leg_end_y)) / 2);
? if ((r_leg_t) == 1) {
??? Liquid_I2C.drawArc((head_x),(body_end_y),(leg_end_x),(leg_end_y),((pass_pnt_x) + 3),(pass_pnt_y));
? }
? if ((r_leg_t) == 2) {
??? Liquid_I2C.drawArc((head_x),(body_end_y),(leg_end_x),(leg_end_y),((pass_pnt_x) - 3),(pass_pnt_y));
? }
}
三、火柴人踢足球和滑滑板
(1) 繪制圖形代碼

(2)生成的代碼程序
Liquid_I2C.link(18,19);
for (var a = 0; a < 100; a=a+1) {
? for (var i = -20; i <= 150; i=i+1) {
??? Liquid_I2C.clearDisplay();
??? matchstickman(i, 10, 3, 1, Math.sin(((i) * 30)/180*Math.PI) * 30, 2, Math.sin(((i) * 30)/180*Math.PI) * 30, 2, Math.sin(((i) * 30)/180*Math.PI) * 30, 1, Math.sin(((i) * 30)/180*Math.PI) * 30);
??? Liquid_I2C.drawCircle(((i) + 17 + Math.sin(((i) * 15)/180*Math.PI) * 10),50,3,1);
??? Liquid_I2C.display();
? }
? for (var i = 150; i >= -20; i=i-1) {
??? Liquid_I2C.clearDisplay();
??? matchstickman(i, 10, 2, 2, 45, 1, 45, 1, 30, 2, Math.sin(((i) * 30)/180*Math.PI) * 30 + 30);
??? Liquid_I2C.drawArc(((i) - 20),50,((i) + 20),50,(i),54);
??? Liquid_I2C.drawCircle(((i) - 10),55,2,1);
??? Liquid_I2C.drawCircle(((i) + 10),55,2,1);
??? Liquid_I2C.display();
? }
}
三、火柴人練習(xí)籃球和足球過(guò)人
(1) 繪制圖形代碼

(2)生成的代碼程序
Liquid_I2C.link(18,19);
for (var a = 0; a < 10; a=a+1) {
? for (var i = -20; i <= 150; i=i+1) {
??? Liquid_I2C.clearDisplay();
??? matchstickman(i, 10, 3, 1, Math.sin(((i) * 15)/180*Math.PI) * 30 - 60, 2, Math.sin(((i) * 15)/180*Math.PI) * 30, 2, Math.sin(((i) * 15)/180*Math.PI) * 30, 1, Math.sin(((i) * 15)/180*Math.PI) * 30);
??? Liquid_I2C.drawCircle(((i) + 15),(38 + Math.sin(((i) * 15)/180*Math.PI) * 12),3,1);
??? Liquid_I2C.display();
? }
? for (var i = 150; i >= -20; i=i-1) {
??? Liquid_I2C.clearDisplay();
??? matchstickman(i, Math.sin(((i) * 10)/180*Math.PI) * 30, 2, 2, Math.sin(((i) * 30)/180*Math.PI) * 30, 1, Math.sin(((i) * 30)/180*Math.PI) * 30, 1, Math.sin(((i) * 30)/180*Math.PI) * 30, 2, Math.sin(((i) * 30)/180*Math.PI) * 30);
??? Liquid_I2C.drawCircle((((i) - 17) - Math.sin(((i) * 25)/180*Math.PI) * 10),(Math.sin(((i) * 10)/180*Math.PI) * 30 + 45),3,1);
??? Liquid_I2C.display();
? }
}
三、火柴人瘋狂打籃球
(1) 繪制圖形代碼

5、動(dòng)態(tài)改變火柴人、繪制籃球
(2)生成的代碼程序
Liquid_I2C.link(18,19);
for (var a = 0; a < 100; a=a+1) {
? for (var i = 0; i <= 100; i=i+1) {
??? Liquid_I2C.clearDisplay();
??? matchstickman(64, 10, Math.random_int(1,4), 2, Math.sin(((i) * 15)/180*Math.PI) * Math.random_int(10,60) - 60, 2, Math.sin(((i) * 15)/180*Math.PI) * 30 - Math.random_int(10,100), 1, Math.sin(((i) * 15)/180*Math.PI) * 10 + 10, 1, Math.sin(((i) * 15)/180*Math.PI) * 10 + 10);
??? Liquid_I2C.drawCircle((Math.abs(64 + Math.sin(((i) * 15)/180*Math.PI) * 20)),(30 + Math.abs(Math.cos(((i) * 15)/180*Math.PI) * 30)),3,1);
??? Liquid_I2C.display();
? }
}