2020年6月19日 星期五

孤獨月宮的花園

200619 week17

玩電玩學程式!!!!!!!

虎克定律的彈簧












void setup()
{
  size(300,300);
}
void draw()
{
  background(#002966);
  stroke(#b7efff);
  line(100,150,200,150);
  
  fill(#b7efff);
  ellipse(100,150,13,13);
  ellipse(200,150,13,13);
}

*紅色是程式碼改變的部分

可以拉~~~長












void setup()
{
  size(300,300);
}
float x=100,y=150;
void draw()
{
  background(#002966);
  stroke(#b7efff);
  line(x,y,200,150);
  
  fill(#b7efff);
  ellipse(x,y,13,13);
  ellipse(200,150,13,13);
}
void mouseDragged()
{
  x=mouseX;y=mouseY;
}


拉~~~長後回到原來的位置












void setup()
{
  size(300,300);
}
float x=100,y=150;
void draw()
{
  background(#002966);
  stroke(#b7efff);
  line(x,y,200,150); 
  fill(#b7efff);
  ellipse(x,y,13,13);
  ellipse(200,150,13,13);
  if( !mousePressed)
  {
    float dx=x-200;
    float dy=y-150;
    float len=sqrt(dx*dx+dy*dy);
    x-=dx*(len-100)/len;
    y-=dy*(len-100)/len;
  }
}
void mouseDragged()
{
  x=mouseX;y=mouseY;
}

拉~~~長後回到原來的位置(有彈性版本)












void setup()
{
  size(300,300);
}
float x=100,y=150;
void draw()
{
  background(#002966);
  stroke(#b7efff);
  line(x,y,200,150); 
  fill(#b7efff);
  ellipse(x,y,13,13);
  ellipse(200,150,13,13);
  if( !mousePressed)
  {
    float dx=x-200;
    float dy=y-150;
    float len=sqrt(dx*dx+dy*dy);
    x-=dx*(len-100)/len*0.1;
    y-=dy*(len-100)/len*0.1;
  }
}
void mouseDragged()
{
  x=mouseX;y=mouseY;
}

彈簧會伸縮












void setup()
{
  size(300,300);
}
float x=100,y=150;
float vx=0,vy=0;
void draw()
{
  background(#002966);
  stroke(#b7efff);
  line(x,y,200,150); 
  fill(#b7efff);
  ellipse(x,y,13,13);
  ellipse(200,150,13,13);
  if( !mousePressed)
  {
    float dx=x-200;
    float dy=y-150;
    float len=sqrt(dx*dx+dy*dy);
    float f=len-100;
    dx/=len;//單位長度
    dy/=len;
    vx -=f*dx*0.0001;//不同彈性係數,彈力愈強,動愈快
    vy -=f*dy*0.0001;//0.1快,0.01慢
    x+=vx;
    y+=vy;
  }
}
void mouseDragged()
{
  x=mouseX;y=mouseY;

}


餵魚?/貓追老鼠?

void setup()
{
  size(400,300);
}
float x=100,y=150;
void draw()
{
  background(#002966);
  ellipse(x,y,50,50);//這是飼料?老鼠?
  
  float dx=x- mouseX;
  float dy=y- mouseY;
  float len= sqrt(dx*dx+dy*dy);
  x -=dx*0.1;
  y -=dy*0.1;
}

魚與飼料?/貓與老鼠?(簡單來說就是你的滑鼠)
保持一定距離












void setup()
{
  size(400,300);
}
float x=100,y=150;
void draw()
{
  background(#002966);
  ellipse(x,y,50,50);
  
  float dx=x- mouseX;
  float dy=y- mouseY;
  float len= sqrt(dx*dx+dy*dy);
  x-=dx*(len-100)/len*0.1;
  y-=dy*(len-100)/len*0.1;//保持100的距離
}


讀檔












#include <stdio.h>
int main()
{         ///這是你的檔案名稱
    FILE *fout=fopen ("my_first_file.txt","w+");

    printf("Hello World\n");
    fprintf(fout,"Hello World\n");
}


心得:今天課堂中看了好多遊戲的程式碼,也學了簡單的療癒遊戲、如何讀檔,很開心!

沒有留言:

張貼留言