2020年3月20日 星期五

程式設計Week03

進擊的胖豬老大

程式碼:

PImage img1,img2,img3; //設三個參數

int userX=200,userY=200;

void setup( )

{
  size(500,500);
  img1=loadImage("pig1.png");  //分別讀入圖pig1,pig2,pig3
  img2=loadImage("pig3.png");
  img3=loadImage("pig2.png");
}

void draw( )

{
  background(125);
  if(keyPressed && keyCode= =LEFT)  //如果按左鍵,豬向左跑,使用img2圖
  {
     userX-=3;
     image(img2,userX,userY,200,200);
  }


  else if(keyPressed && keyCode= =RIGHT)  //如果案右鍵,豬向右跑,

                                                                              使用img3圖
  {
    userX+=3;
    image(img3,userX,userY,200,200);
  }


  else image(img1,userX,userY,200,200);   //豬向上或向下跑使用img1圖
  if(keyPressed && keyCode==UP)userY-=3;
  if(keyPressed && keyCode==DOWN)userY+=3;

}

沒有留言:

張貼留言