2016年3月17日 星期四

week04,遊戲製作(射擊/打棒球)!

有手指頭!!


import de.voidplus.leapmotion.*;
LeapMotion leap;
void setup(){
  size(600,400);
  leap=new LeapMotion(this);
}
void draw(){
  background(255);
  for(Hand hand:leap.getHands())
  {
    Finger finger_index=hand.getIndexFinger();
    finger_index.draw();
  }
}


會旋轉的方塊!!!


import de.voidplus.leapmotion.*;
LeapMotion leap;
void setup(){
  size(600,400,P3D);
  leap=new LeapMotion(this);
}
void draw(){
  background(255);
  for(Hand hand:leap.getHands())
  {
    Finger finger_index=hand.getIndexFinger();
    finger_index.draw();
    PVector pos=finger_index.getPosition();
    PVector dir=finger_index.getDirection();
    println(pos);
    println(dir);
  }
 lights();
 fill(255,0,0);
 translate(width/2,height/2);
 rotateY(radians(frameCount));
 box(300,100,100);
}

import de.voidplus.leapmotion.*;
LeapMotion leap;
void setup(){
  size(600,400,P3D);
  leap=new LeapMotion(this);
}
PVector pos=null,dir=null;
void draw(){
  background(255);
  for(Hand hand:leap.getHands()){
    Finger finger_index=hand.getIndexFinger();
    finger_index.draw();
    pos=finger_index.getPosition();
    dir=finger_index.getDirection();
  }
 lights();
 fill(255,0,0);
 pushMatrix();
    translate(width/2,height/2);
    for(int i=0;i<10;i++){
      if(dir!=null) translate(dir.x*10*i,dir.y*10*i,dir.z*10*i);
      sphere(40);
    }
    popMatrix();
 //rotateY(radians(frameCount));
 //box(300,100,100);
}

可以發射子彈囉!


import de.voidplus.leapmotion.*;
PVector pos=null,dir=null;
PVector []bullet=new PVector[10];
PVector []bullet_dir=new PVector[10];
int bulletNow=0;
LeapMotion leap;
void setup(){
  size(600,400,P3D);
  leap=new LeapMotion(this);
  for(int i=0;i<10;i++){
    bullet[i]=new PVector(0,0,0);
    bullet_dir[i]=new PVector(0,0,0);
  }
}
void draw(){
  background(255);
  for(Hand hand:leap.getHands()){
    Finger finger_index=hand.getIndexFinger();
    finger_index.draw();
    pos=finger_index.getPosition();
    dir=finger_index.getDirection();
  }
 lights();
 fill(255,0,0);
 translate(width/2,height/2);
 pushMatrix();
    for(int i=0;i<10;i++){
      if(dir!=null) translate(dir.x*10*i,dir.y*10*i,dir.z*10*i);
      sphere(40);
    }
    popMatrix();
    pushMatrix();
      fill(0,0,255);
      for(int i=0;i<10;i++){
        pushMatrix();
          translate(bullet[i].x,bullet[i].y,bullet[i].z);
          sphere(41);
          bullet[i].x+=bullet_dir[i].x;
          bullet[i].y+=bullet_dir[i].y;
          bullet[i].z+=bullet_dir[i].z;
        popMatrix();
      }
      popMatrix();
}
void keyPressed(){
  bullet_dir[bulletNow].x=dir.x*5;
  bullet_dir[bulletNow].y=dir.y*5;
  bullet_dir[bulletNow].z=dir.z*5;
  bulletNow++;
}

我在偷子彈!!!

import de.voidplus.leapmotion.*;
PVector pos=null,dir=null;
PVector []bullet=new PVector[10];
PVector []bullet_dir=new PVector[10];
int bulletNow=0;
LeapMotion leap;
void setup(){
  size(600,400,P3D);
  leap=new LeapMotion(this);
  for(int i=0;i<10;i++){
    bullet[i]=new PVector(0,0,0);
    bullet_dir[i]=new PVector(0,0,0);
  }
}
void draw(){
  background(255);
  for(Hand hand:leap.getHands()){
    Finger finger_index=hand.getIndexFinger();
    finger_index.draw();
    pos=finger_index.getPosition();
    dir=finger_index.getDirection();
  }
 lights();
 fill(255,0,0);
 translate(width/2,height/2);
 pushMatrix();
    for(int i=0;i<10;i++){
      if(dir!=null) translate(dir.x*10*i,dir.y*10*i,dir.z*10*i);
      sphere(40);
    }
    popMatrix();
    pushMatrix();
      fill(0,0,255);
      for(int i=0;i<10;i++){
        pushMatrix();
          translate(bullet[i].x,bullet[i].y,bullet[i].z);
          sphere(41);
          bullet[i].x+=bullet_dir[i].x;
          bullet[i].y+=bullet_dir[i].y;
          bullet[i].z+=bullet_dir[i].z;
        popMatrix();
      }
      popMatrix();
}
void keyPressed(){
  bullet[bulletNow].x=0;  //子彈坐標
  bullet[bulletNow].y=0;
  bullet[bulletNow].z=0;
  bullet_dir[bulletNow].x=dir.x*5;
  bullet_dir[bulletNow].y=dir.y*5;
  bullet_dir[bulletNow].z=dir.z*5;
  bulletNow++;
  if(bulletNow>=10){
    bulletNow=0;      
  }
}

圓球飛出來碰裝到之後,螢幕呈現紅色!!

import de.voidplus.leapmotion.*;
LeapMotion leap;
PVector pos,dir,ball;
void setup(){
  size(600,400,P3D);
  leap=new LeapMotion(this);
  pos=new PVector(300,200,0);
  ball=new PVector(0,0,0);
}
void draw(){
  ball.x+=2;
  ball.y+=2;
  for(Hand hand:leap.getHands()){
    pos=hand.getPosition();
  }
  background(255);
  fill(255,0,0);
  pushMatrix();
    translate(pos.x,pos.y,pos.z);
    box(30,30,30);
  popMatrix();
  pushMatrix();
    translate(ball.x,ball.y);
    sphere(30);
    if(dist(ball.x,ball.y,pos.x,pos.y)<30)
    background(255,0,0);
  popMatrix();
}

====
今天講解到我的小遊戲喔!
import de.voidplus.leapmotion.*;
LeapMotion leap;
PVector pika,ball,ballv;
void setup(){
size(600,400,P3D);
leap=new LeapMotion(this);
pika=new PVector(width/2,height/2);
ball=new PVector(100,100,0);
ballv=new PVector(2,2,0);
noStroke();
}
void draw(){
lights();
background(255);
for(Hand hand:leap.getHands()){
PVector pos=hand.getPosition();
pika.x=pos.x;
pika.y=pos.y;
}
pushMatrix();
translate(pika.x,pika.y);
fill(#FAE600);
sphere(60);
popMatrix();
pushMatrix();
translate(ball.x,ball.y);
fill(#FDFFBF);
sphere(50);
ball.x+=ballv.x;
ball.y+=ballv.y;
if(dist(ball.x,ball.y,pika.x,pika.y)<50){
float x=ball.x-pika.x,y=ball.y-pika-y,value=sqrt(x*x+y*y);
ballv.x=2*x/value;
ballv.y=2*y/value;
}
if(ball.x>width-50||ball.x<50) ballv.x=-ballv.x;
if(ball.y>height-50||ball.y<50) ballv.y=-ballv.y;
popMatrix();
}

沒有留言:

張貼留言