首页
登录 | 注册

processing学习笔记2--弹球游戏1.0


      上次那个球体碰撞的代码,实质上就构建了一个 圆球类(不是太完善,现在觉得还是 不应该 用 angle 来作为 运动的 依据),现在 我用这个类 做了一个 简单的 弹球游戏,今晚只做到 1.0版,还没有加 球洞,下次假期有时间再加吧。总的来说,这个小游戏的核心 就是 hitcircle 的 使用方法。下次希望改善这个类 的运动依据,直接用 x,y的增减来控制 运动。最近刚看到一本书 叫《代码本色》,很不错,上面讲的很多东西 都超有趣。
int M=5;  //number of balls
float S=10;  //speed
int R=20;
int sizeX=800;
int sizeY=600;
Circle [] mycircle=new Circle[M+1];
Circle hitcircle;

void setup()
{
  size(sizeX,sizeY);
  smooth();
  hitcircle=new Circle(100,300,0);
  mycircle[0] =new Circle(600,300,1);
  for(int i=1;i<=2;i++)
  {
    mycircle[i] =new Circle(640,240+i*40,1);
  }
  for(int i=3;i<=M;i++)
  {
    mycircle[i] =new Circle(680,220+(i-2)*40,1);
  }
}

void draw()
{
  background(0);
  hitcircle.move();
  hitcircle.judge();
  hitcircle.display();  
  for(int i=0;i<=M;i++)
     {
       mycircle[i].move();
       mycircle[i].judge();
       mycircle[i].display();  
     }
  if(mousePressed)
     {
         if(hitcircle.speed<=0)
         {
           hitcircle.speed=S; 
           float k;
           k=asin((mouseY-hitcircle.y)/dist(mouseX,mouseY,hitcircle.x,hitcircle.y));  
           if(mouseX>hitcircle.x) 
              hitcircle.angle=k;
           else
              hitcircle.angle=PI-k;
         }
    }
}

class Circle
{
  float x;
  float y;
  int r;
  int c;
  float speed;
  float angle;
  int flag;
  
Circle(float xx,float yy,int cc)
  {
    x=xx;
    y=yy;
    r=R;
    c=cc;
    speed=0;
    angle=0;
  }
  
  void move()
  {
    if(speed>0)
    {
      x+=speed*cos(angle);
      y+=speed*sin(angle);
    }
   speed=speed-0.05;
  }
  
  void judge()
  {
    if(y+r>=sizeY||y-r<=0)
      angle=2*PI-angle;
    if(x+r>=sizeX||x-r<=0)
      angle=3*PI-angle;
    for(int i = 0; i<=M; i++)
    {
      for(int j = 0; j<=M; j++)
      {
        if(i!=j)
         {
             if(dist(mycircle[i].x,mycircle[i].y,mycircle[j].x,mycircle[j].y)<=mycircle[i].r+mycircle[j].r)
              {
                     float m;
                     m=atan((mycircle[i].y-mycircle[j].y)/(mycircle[i].x-mycircle[j].x));
                     mycircle[i].angle=PI-mycircle[i].angle-2*m;
                     mycircle[j].angle=PI-mycircle[j].angle-2*m;
                     mycircle[i].speed=((mycircle[i].speed>mycircle[j].speed)?mycircle[i].speed:mycircle[j].speed);
               }
         }
      }
     if(dist(mycircle[i].x,mycircle[i].y,hitcircle.x,hitcircle.y)<=mycircle[i].r+hitcircle.r)
              {  
                     float m;
                     m=atan((mycircle[i].y-hitcircle.y)/(mycircle[i].x-hitcircle.x));
                     mycircle[i].angle=PI-mycircle[i].angle-2*m;
                     hitcircle.angle=PI-hitcircle.angle-2*m;;
                     mycircle[i].speed=hitcircle.speed;
               }     
    }      
  }
  
  void display()
  {
    if(c==1)
        fill(255,0,0);
     else
        fill(0,255,0);
    ellipse(x,y,2*r,2*r);
  }
}


    希望对刚开始学或者有同样想法的人有一定的帮助。也希望各位看官能给与建议。O(∩_∩)O



2020 jeepxie.net webmaster#jeepxie.net
10 q. 0.008 s.
京ICP备10005923号