과제 업로드용 블로그

francesca1.egloos.com

포토로그



Interactive Video - Week 5

This week's assignment was to make moving image that interact with arrow keys and mouse click.
So I made this simple game using arrow keys and mouse.
I drew shark and fish by myself, and used free source picture for background.




As you see, shark moves by arrow keys, and opens it's mouth when mouse is clicked.
if you get close to fish(which is randomly regenerated every time) with shark's mouth opened, shark eats fish and you earn 1 point. you can see your score at bottom left corner.

And this is my code.
(I uploaded image and font files too, at the bottom of this post.)


PImage Bg;
PImage ShL01;
PImage ShL02;
PImage ShR01;
PImage ShR02;
PImage ShOL;
PImage ShOR;
PImage fish;
int sharkX=200;
int sharkY=200;
int sharkLR=0;
int fishX=int(random(100,400));
int fishY=int(random(100,400));
int yumPop=0;
int score=0;
boolean sharkOpen=false;
boolean yum=false;
void setup(){
  size(500,500);
  Bg=loadImage("ocean.png");
  ShL01=loadImage("shark_01.png");
  ShL02=loadImage("shark_02.png");
  ShR01=loadImage("shark_01_r.png");
  ShR02=loadImage("shark_02_r.png");
  ShOL=loadImage("shark_open01.png");
  ShOR=loadImage("shark_open02.png");
  fish=loadImage("fish.png");
}
//setup files

void draw(){
  background(0);
  imageMode(CORNER);
  image(Bg,0,0,500,500);
  //Background draw
  
    if(sharkLR==0){
      if(sharkOpen==true){
        imageMode(CENTER);
        image(ShOL,sharkX,sharkY,200,100);
      }
        
      if(sharkOpen==false){
        if(frameCount%30<15){
          imageMode(CENTER);
          image(ShL01,sharkX,sharkY,200,100);
        }
        if(frameCount%30>=15){
          imageMode(CENTER);
          image(ShL02,sharkX,sharkY,200,100);
        }
      }
    }
    
    if(sharkLR==1){ 
      if(sharkOpen==true){
        imageMode(CENTER);
        image(ShOR,sharkX,sharkY,200,100);
      }
      
      if(sharkOpen==false){
        if(frameCount%30<15){
          imageMode(CENTER);
          image(ShR01,sharkX,sharkY,200,100);
        }
        if(frameCount%30>=15){
          imageMode(CENTER);
          image(ShR02,sharkX,sharkY,200,100);
        }
      }
    }
    
  if(mousePressed == true){
    sharkOpen=true;
  }
  if(mousePressed == false){
    sharkOpen=false;
  }
  
  if(keyPressed == true){
    if(keyCode==UP){
      sharkY-=5;
      if(sharkY<50){
        sharkY=50;
      }
    }
  }
  if(keyPressed == true){
    if(keyCode==DOWN){
      sharkY+=5;
      if(sharkY>450){
        sharkY=450;
      }
    }
  }
  if(keyPressed == true){
    if(keyCode==LEFT){
      sharkX-=5;
      sharkLR=0;
      if(sharkX<100){
        sharkX=100;
      }
    }
  }
  if(keyPressed == true){
    if(keyCode==RIGHT){
      sharkX+=5;
      sharkLR=1;
      if(sharkX>400){
        sharkX=400;
      }
    }
  }
  //shark code
  
  if(sharkOpen==true){
    if(fishX-10<sharkX){
      if(fishX+10>sharkX){
        if(fishY+10>sharkY){
          if(fishY+10>sharkY){
            yum=true;
          }
        }
      }
    }
  }
  if(yum==false){
    imageMode(CENTER);
    image(fish,fishX,fishY,30,30);
  }
  if(yum==true){
    fill(255);
    textSize(20);
    text("Yum!",fishX,fishY);
    yumPop=frameCount%30;
    if(yumPop==29){
      yum=false;
      fishX=int(random(100,400));
      fishY=int(random(100,400));
      score+=1;
    }
  }
  //fish code
  
  textSize(30);
  text(score,90,490);
  textSize(30);
  text("score",10,490);    
  //print score
}




덧글

댓글 입력 영역