# 期中報告自畫像 --- ## 期中報告 ### 目標 * 畫一個自畫像,隨著滑鼠游標移動中 * 按一下可以跳動,跟自由落體一樣的往下跳 --- 滑鼠上下移動,右眼會跟著放大縮小 按一下,人物的右眼會像自由落體一樣的掉下來 ![](https://i.imgur.com/87v6Fgz.gif) ```javascript= function setup() { createCanvas(windowWidth, windowHeight); background(0); } var mode=1 function mousePressed(){ mode++ if(mode>3){ mode =1 } } var v=612 var t=315 var v_x=3 var v_y=3 var count=0 function draw() { if(mode==1){ background(0); for (var x = 0;x<width;x+=50){ for (var y =0;y<height;y+=50){ count =count+1 stroke(random(256),random(100),random(100)) strokeWeight(3) if(count){ fill(random(256),random(100),random(100)) ellipse(x,y,40) if (random()<0.5){ fill(0) ellipse(x,y,20) if (random()<0.5){ fill(random(256),random(100),random(100)) ellipse(x,y,10) } } } } } stroke(0) //外誆線 strokeWeight(1) //外誆線粗度 fill(139,69,19) //髮色 arc(635, 325+30, 150, 200, 180, PI + QUARTER_PI, CHORD); //頭髮 fill(255,228,181) //膚色 rect(620, 355, 30,50) //脖子 ellipse(635, 325, 100); //臉 beginShape() //嘴巴 vertex(610,350) vertex(635,355) vertex(660,350) vertex(610,350) endShape() fill(0) //眼瞳顏色 strokeWeight(6) //用線條畫出眼白 stroke(255) //眼白顏色 ellipse(612,315,20+mouseY/10); //右眼 ellipse(658,315,20+mouseX/10) //左眼 } if(mode==2){ background(0); fill(0) //眼瞳顏色 strokeWeight(6) //用線條畫出眼白 stroke(255) //眼白顏色 ellipse(v,t,20,20); //左眼 v =v+v_x t =t+v_y v_y =v_y+0.5 if(t>height){ v_y=-v_y*0.8 t=height } } } ``` ### 背景 ```javascript= function draw() { if(mode==1){ background(0); for (var x = 0;x<width;x+=50){ for (var y = 0;y<height;y+=50){ count =count+1 stroke(random(256),random(100),random(100)) strokeWeight(3) if(count){ fill(random(256),random(100),random(100)) ellipse(x,y,40) if (random()<0.5){ fill(0) ellipse(x,y,20) if (random()<0.5){ fill(random(256),random(100),random(100)) ellipse(x,y,10) } } } } } ``` ### 臉 ```javascript= stroke(0) //外誆線 strokeWeight(1) //外誆線粗度 fill(139,69,19) //髮色 arc(635, 355, 150, 200, 180, PI + QUARTER_PI, CHORD); //頭髮 fill(255,228,181) //膚色 rect(620, 355, 30,50) //脖子 ellipse(635, 325, 100); //臉 beginShape() //嘴巴 vertex(610,350) vertex(635,355) vertex(660,350) vertex(610,350) endShape() fill(0) //眼瞳顏色 strokeWeight(6) //用線條畫出眼白 stroke(255) //眼白顏色 ellipse(612,315,20+mouseY/10); //右眼 ellipse(658,315,20) //左眼 ``` ### 點一下自由落體 右眼會像自由落體一樣的掉落 ```javascript= if(mode==2){ background(0); fill(0) //眼瞳顏色 strokeWeight(6) //用線條畫出眼白 stroke(255) //眼白顏色 ellipse(v,t,20,20); //右眼 v =v+v_x t =t+v_y v_y =v_y+0.5 if(t>height){ v_y=-v_y*0.8 t=height } } ```