Advertisement
Guest User

Untitled

a guest
Jun 26th, 2015
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Ball b;
  3. Rekt r1,r2;
  4. float wid = 100;
  5. float smR = 15;
  6. float bgR = 20;
  7. float D = 20;
  8. float xx;
  9. float ez;
  10. float minez = 0.005;
  11. float maxez = 0.05;
  12. float limy;
  13. boolean play = false;
  14. float W, H;
  15. float lb, rb;
  16. int rez1 = 0;
  17. int rez2 = 0;
  18.  
  19.  
  20. void setup(){
  21.   size(500, 800);
  22.   H = height;
  23.   W = height*0.8;
  24.   lb = 0;
  25.   rb = width;
  26.   b = new Ball();
  27.   r1=new Rekt(width/2,height);
  28.   r2=new Rekt(width/2,0);
  29.   xx = width/2;
  30.   rectMode(CENTER);
  31.   limy=height*0.6;
  32.   play = false;
  33.     //background(244);
  34.  
  35.   }
  36.  
  37.   void mouseClicked(){
  38.     if(!play){
  39.       play = true;
  40.       b.pos.set(width/2, height/2);
  41.     }
  42.   }
  43.  
  44.   void draw(){
  45.     fill(244);
  46.     noStroke();
  47.     rect(width/2,height/2,W,H);
  48.     if(play)
  49.       b.move();
  50.     b.display();
  51.  
  52.     if(frameCount > 1)
  53.       r1.display();
  54.  
  55.     r1.move();
  56.  
  57.     r2.display();
  58.     r2.move2();
  59.  
  60.     // samo za razmak od ruba
  61.     fill(244);
  62.     noStroke();
  63.     // rect(width/2,height,W,12);
  64.     // rect(width/2,0,W,12);
  65.  
  66.     // podrucje oko polja
  67.     //noStroke();
  68.     //fill(30, 200, 140);
  69.     //rect(lb/2, H/2, lb, H);
  70.     //rect(lb+W+lb/2, H/2, lb, H);
  71.  
  72.     if(b.pos.y>limy){
  73.      ez = minez;
  74.    }
  75.    else{
  76.      ez=map(b.pos.y,0,limy,maxez,minez);
  77.    }
  78.  
  79.    xx = easy(b.pos.x,xx,ez);
  80.  
  81.  
  82.    // stop igra
  83.    if(b.pos.y + b.Ry - 50> H || b.pos.y - b.Ry + 50 < 0){
  84.     play = false;
  85.  
  86.  
  87.     if(b.pos.y > H/2)
  88.      rez2++;
  89.    else
  90.     rez1++;
  91.  
  92.    b.pos.set(width/2, height/2);
  93.     b.fels.mult(0);
  94.    b.vel.x = random(-0.1,0.1);
  95.   b.vel.y = random(-3,3);
  96.   b.vel.normalize();
  97.   b.vel.mult(9);
  98.   b.dd = 0;
  99.  
  100. }
  101. }
  102.  
  103. class Ball{
  104.     PVector pos;
  105.     PVector ppos;
  106.     PVector vel,pvel;
  107.     float scx,scy;
  108.     float Rx, Ry;
  109.     float d, dx, dy;
  110.     float dd;
  111.     PVector fels;
  112.  
  113.     Ball(){
  114.         pos = new PVector(width/2,height/2);
  115.         ppos = pos.get();
  116.         vel = new PVector(random(-0.1,0.1),random(-3,3));
  117.         vel.normalize();
  118.         vel.mult(9);
  119.         pvel=vel.get();
  120.         Rx = bgR;
  121.         Ry = bgR;
  122.     }
  123.  
  124.     void move(){
  125.         ppos = pos.get();
  126.         pos.add(vel);
  127.         Rx = bgR;
  128.         Ry = bgR;
  129.  
  130.         // odbijanje od ploce
  131.         if(pos.y<bgR+D &&
  132.         ppos.y>bgR+D &&
  133.         abs(r2.pos.x-pos.x)<wid
  134.         ||
  135.         pos.y>height-bgR-D &&
  136.         ppos.y<height-bgR-D &&
  137.         abs(r1.pos.x-pos.x)<wid){
  138.  
  139.             vel.y*=-1;
  140.  
  141.             if(pos.y < H/2)
  142.                 pos.y = pos.y + (bgR+D-pos.y)*2;
  143.             else
  144.                 pos.y = pos.y - (pos.y-height+bgR+D)*2;
  145.  
  146.  
  147.             if(pos.y<height/2){
  148.                 PVector p;
  149.                 dd = r2.pos.x - r2.ppos.x;
  150.                 dd /= W;
  151.             }
  152.             if(pos.y>height/2){
  153.                 PVector p;
  154.                 dd = r1.pos.x - r1.ppos.x;
  155.                 dd /= W;
  156.             }
  157.         }
  158.  
  159.         // odbijanje od zida
  160.         if(pos.x>rb-bgR){
  161.             pos.x = pos.x - (pos.x-rb+bgR)*2;
  162.             vel.x*=-1;
  163.             dd = 0;
  164.         }
  165.         if(pos.x<lb+bgR){
  166.             pos.x = pos.x + (lb+bgR-pos.x)*2;
  167.             vel.x*=-1;
  168.             dd = 0;
  169.         }
  170.  
  171.         fels = new PVector(dd, 0);
  172.  
  173.         vel.add(fels);
  174.  
  175.     }
  176.  
  177.     void display(){
  178.         fill(33);
  179.         noStroke();
  180.         pushMatrix();
  181.         translate(pos.x - dx,pos.y - dy);
  182.         //rotate(vel.heading());
  183.         ellipse(0,0, bgR*2, bgR*2);
  184.         popMatrix();
  185.     }
  186. }
  187.  
  188. class Rekt{
  189.     PVector pos;
  190.     PVector ppos;
  191.     Rekt(float x,float y){
  192.         ppos=new PVector(x,y);
  193.         pos=new PVector(x,y);
  194.     }
  195.     void move(){
  196.         ppos.x=easy(pos.x,ppos.x,0.4);
  197.         pos.x=mouseX;
  198.         if(pos.x-wid<lb)
  199.         pos.x = lb+wid;
  200.         if(pos.x+wid>rb)
  201.         pos.x = rb-wid;
  202.     }
  203.  
  204.     void move2(){
  205.         pos.x = xx;
  206.         if(pos.x-wid<lb)
  207.         pos.x = lb+wid;
  208.         if(pos.x+wid>rb)
  209.         pos.x = rb-wid;
  210.     }
  211.  
  212.     void display(){
  213.      fill(33);
  214.      noStroke();
  215.      rect(pos.x,pos.y,wid*2,2*D,4);
  216.  }
  217. }
  218.  
  219. float power(float p, float g) {
  220.   if (p < 0.5)
  221.     return 0.5 * pow(2*p, g);
  222.   else
  223.     return 1 - 0.5 * pow(2*(1 - p), g);
  224. }
  225.  
  226. float easy(float target, float k, float easing) {
  227.  float d = target - k;
  228.  if (abs(d) > 1) {
  229.   k += d * easing;
  230. }
  231. return k;
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement