今天第一個程式:
第一個:
void setup() { size(500,400); } int x=250,y=200; void draw() { background(215,184,200); fill(167,167,175); stroke(110,110,113); strokeWeight(3); rect(240,155,20,20); fill(204,102,156); strokeWeight(3); ellipse(x,y,55,55); line(x,y,mouseX,mouseY); }
第二個(控制線的長度,增加粗度):void setup() { size(500,400); } int x=250,y=200; void draw() { background(215,184,200); float dx=mouseX-x,dy=mouseY-y; float angle=atan2(dy,dx); strokeWeight(20); line(x,y,x+cos(angle)*40,y+sin(angle)*40); fill(167,167,175); stroke(110,110,113); strokeWeight(3); rect(240,155,20,20); fill(204,102,156); strokeWeight(3); ellipse(x,y,55,55); ///line(x,y,mouseX,mouseY); }
第三個:void setup() { size(500,400); } int x=250,y=200; void draw() { background(215,184,200); float dx=mouseX-x,dy=mouseY-y; float angle=atan2(dy,dx); myTank(x,y,angle); } void myTank(float x,float y,float angle) { translate(x,y); rotate(angle); fill(167,167,175); stroke(110,110,113); strokeWeight(4); rect(25,-10,20,20); fill(204,102,156); stroke(125,84,105); strokeWeight(3); ellipse(0,0,55,55); ///line(x,y,mouseX,mouseY); }
第四個(在畫面中移動):void setup() { size(500, 400); } void keyPressed() { if(keyCode==RIGHT)x+=1; if(keyCode==LEFT)x-=1; if(keyCode==UP)y-=1; if(keyCode==DOWN)y+=1; } float x=250, y=200; void draw() { //background(215,184,200); background(219); stroke(210); strokeWeight(1); for (int i=0; i<30; i++) { line(0, i*30, 500, i*30); line(i*30, 0, i*30, 500); } float dx=mouseX-x, dy=mouseY-y; float angle=atan2(dy, dx); myTank(x, y, angle); } void myTank(float x, float y, float angle) { translate(x, y); rotate(angle); fill(167, 167, 175); stroke(110, 110, 113); strokeWeight(4); rect(25, -10, 20, 20); fill(204, 102, 156); stroke(125, 84, 105); strokeWeight(3); ellipse(0, 0, 55, 55); ///line(x,y,mouseX,mouseY); }
第五個(主體是Tank,會一直在中間):void setup() { size(500, 400); } void keyPressed() { if(keyCode==RIGHT)x+=1; if(keyCode==LEFT)x-=1; if(keyCode==UP)y-=1; if(keyCode==DOWN)y+=1; } float x=250, y=200; void draw() { //background(215,184,200); background(219); translate(-x,-y); translate(width/2,height/2); float mx=mouseX+x-width/2,my=mouseY+y-height/2; ellipse(mx,my,8,8); stroke(210); strokeWeight(1); for (int i=0; i<30; i++) { line(0, i*30, 500, i*30); line(i*30, 0, i*30, 500); } float dx=mx-x, dy=my-y; float angle=atan2(dy, dx); line(x,y,mx,my); myTank(x, y, angle); } void myTank(float x, float y, float angle) { translate(x, y); rotate(angle); fill(167, 167, 175); stroke(110, 110, 113); strokeWeight(4); rect(25, -10, 20, 20); fill(204, 102, 156); stroke(125, 84, 105); strokeWeight(3); ellipse(0, 0, 55, 55); ///line(x,y,mouseX,mouseY); }
第六個(因為keyPressed的更新太慢會卡卡的,所以改在draw裡面去做設定):void setup() { size(500, 400); } void keyPressed() { if (keyCode==RIGHT)vx=1; if (keyCode==LEFT)vx=-1; if (keyCode==UP)vy=-1; if (keyCode==DOWN)vy=1; } void keyReleased() { if(keyCode==LEFT||keyCode==RIGHT) vx=0; if(keyCode==UP||keyCode==DOWN) vy=0; } float x=250, y=200, vx=0, vy=0; void draw() { x+=vx; y+=vy; //background(215,184,200); background(219); translate(-x, -y); translate(width/2, height/2); float mx=mouseX+x-width/2, my=mouseY+y-height/2; ellipse(mx, my, 8, 8); stroke(210); strokeWeight(1); for (int i=0; i<30; i++) { line(0, i*30, 500, i*30); line(i*30, 0, i*30, 500); } float dx=mx-x, dy=my-y; float angle=atan2(dy, dx); line(x, y, mx, my); myTank(x, y, angle); } void myTank(float x, float y, float angle) { translate(x, y); rotate(angle); fill(167, 167, 175); stroke(110, 110, 113); strokeWeight(4); rect(25, -10, 20, 20); fill(204, 102, 156); stroke(125, 84, 105); strokeWeight(3); ellipse(0, 0, 55, 55); ///line(x,y,mouseX,mouseY); }
第七個(可以射出子彈,但只有一顆):void setup() { size(500, 400); } void keyPressed() { if (keyCode==RIGHT)vx=1; if (keyCode==LEFT)vx=-1; if (keyCode==UP)vy=-1; if (keyCode==DOWN)vy=1; } void keyReleased() { if (keyCode==LEFT||keyCode==RIGHT) vx=0; if (keyCode==UP||keyCode==DOWN) vy=0; } float angle,bulletX=0, bulletY=0, bulletVX=0, bulletVY=0; float x=250, y=200, vx=0, vy=0; void draw() { x+=vx; y+=vy; //background(215,184,200); background(219); translate(-x, -y); translate(width/2, height/2); float mx=mouseX+x-width/2, my=mouseY+y-height/2; ellipse(mx, my, 8, 8); stroke(210); strokeWeight(1); for (int i=0; i<30; i++) { line(0, i*30, 500, i*30); line(i*30, 0, i*30, 500); } float dx=mx-x, dy=my-y; angle=atan2(dy, dx); line(x, y, mx, my); pushMatrix(); myTank(x, y, angle); popMatrix(); if (bulletVX!=0&&bulletVY!=0) { bulletX+=bulletVX; bulletY+=bulletVY; ellipse(bulletX, bulletY, 20, 20); } } void mousePressed() { bulletX=x; bulletY=y; bulletVX=cos(angle); bulletVY=sin(angle); } void myTank(float x, float y, float angle) { translate(x, y); rotate(angle); fill(167, 167, 175); stroke(110, 110, 113); strokeWeight(4); rect(25, -10, 20, 20); fill(204, 102, 156); stroke(125, 84, 105); strokeWeight(3); ellipse(0, 0, 55, 55); ///line(x,y,mouseX,mouseY); }
第八個(準備一個陣列,這樣就有很多顆可以射(但只有100顆)):void setup() { size(500, 400); } void keyPressed() { if (keyCode==RIGHT)vx=1; if (keyCode==LEFT)vx=-1; if (keyCode==UP)vy=-1; if (keyCode==DOWN)vy=1; } void keyReleased() { if (keyCode==LEFT||keyCode==RIGHT) vx=0; if (keyCode==UP||keyCode==DOWN) vy=0; } float angle; int bulletN=0; float []bulletX=new float[100]; float []bulletY=new float[100]; float []bulletVX=new float[100]; float []bulletVY=new float[100]; float x=250, y=200, vx=0, vy=0; void draw() { x+=vx; y+=vy; //background(215,184,200); background(219); translate(-x, -y); translate(width/2, height/2); float mx=mouseX+x-width/2, my=mouseY+y-height/2; ellipse(mx, my, 8, 8); stroke(210); strokeWeight(1); for (int i=0; i<30; i++) { line(0, i*30, 500, i*30); line(i*30, 0, i*30, 500); } float dx=mx-x, dy=my-y; angle=atan2(dy, dx); line(x, y, mx, my); pushMatrix(); myTank(x, y, angle); popMatrix(); for (int i=0; i<bulletN; i++) { bulletX[i]+=bulletVX[i]; bulletY[i]+=bulletVY[i]; ellipse(bulletX[i], bulletY[i], 20, 20); } } void mousePressed() { int i=bulletN; bulletX[i]=x; bulletY[i]=y; bulletVX[i]=cos(angle); bulletVY[i]=sin(angle); bulletN++; } void myTank(float x, float y, float angle) { translate(x, y); rotate(angle); fill(167, 167, 175); stroke(110, 110, 113); strokeWeight(4); rect(25, -10, 20, 20); fill(204, 102, 156); stroke(125, 84, 105); strokeWeight(3); ellipse(0, 0, 55, 55); ///line(x,y,mouseX,mouseY); }
第九個(把子彈回收,這樣超過100的時候就不會當機):void setup() { size(500, 400); } void keyPressed() { if (keyCode==RIGHT)vx=1; if (keyCode==LEFT)vx=-1; if (keyCode==UP)vy=-1; if (keyCode==DOWN)vy=1; } void keyReleased() { if (keyCode==LEFT||keyCode==RIGHT) vx=0; if (keyCode==UP||keyCode==DOWN) vy=0; } float angle; int bulletN=0; float []bulletX=new float[100]; float []bulletY=new float[100]; float []bulletVX=new float[100]; float []bulletVY=new float[100]; float x=250, y=200, vx=0, vy=0; void draw() { x+=vx; y+=vy; //background(215,184,200); background(219); translate(-x, -y); translate(width/2, height/2); float mx=mouseX+x-width/2, my=mouseY+y-height/2; ellipse(mx, my, 8, 8); stroke(210); strokeWeight(1); for (int i=0; i<30; i++) { line(0, i*30, 500, i*30); line(i*30, 0, i*30, 500); } float dx=mx-x, dy=my-y; angle=atan2(dy, dx); line(x, y, mx, my); pushMatrix(); myTank(x, y, angle); popMatrix(); for (int i=0; i<bulletN; i++) { bulletX[i]+=bulletVX[i]; bulletY[i]+=bulletVY[i]; ellipse(bulletX[i], bulletY[i], 20, 20); } } void mousePressed() { int i=bulletN; bulletX[i]=x; bulletY[i]=y; bulletVX[i]=cos(angle); bulletVY[i]=sin(angle); bulletN++; if (bulletN==100) { for (int k=0; k<50; k++) { bulletX[k]=bulletX[k+50]; bulletY[k]=bulletY[k+50]; bulletVX[k]=bulletVX[k+50]; bulletVY[k]=bulletVY[k+50]; } bulletN=50; } println(bulletN); } void myTank(float x, float y, float angle) { translate(x, y); rotate(angle); fill(167, 167, 175); stroke(110, 110, 113); strokeWeight(4); rect(25, -10, 20, 20); fill(204, 102, 156); stroke(125, 84, 105); strokeWeight(3); ellipse(0, 0, 55, 55); ///line(x,y,mouseX,mouseY); }
第十個:第十一個:第十二個:第十三個:第十四個:
快捷鍵:
ctrl+R 執行
ctrl+T 自動排版
ctrl+N 新檔案
Git 指令
cd desktopgit clone https://github.com/mickey1132/2024-Interactioncd 2024-Interactiongit statusgit add .git statusgit config --global user.email mickylin1132@gmail.comgit config --global user.name mickey1132git commit -m WeekXXgit push
沒有留言:
張貼留言