Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Ball b;
- Rekt r1,r2;
- float wid = 100;
- float smR = 15;
- float bgR = 20;
- float D = 20;
- float xx;
- float ez;
- float minez = 0.005;
- float maxez = 0.05;
- float limy;
- boolean play = false;
- float W, H;
- float lb, rb;
- int rez1 = 0;
- int rez2 = 0;
- void setup(){
- size(500, 800);
- H = height;
- W = height*0.8;
- lb = 0;
- rb = width;
- b = new Ball();
- r1=new Rekt(width/2,height);
- r2=new Rekt(width/2,0);
- xx = width/2;
- rectMode(CENTER);
- limy=height*0.6;
- play = false;
- //background(244);
- }
- void mouseClicked(){
- if(!play){
- play = true;
- b.pos.set(width/2, height/2);
- }
- }
- void draw(){
- fill(244);
- noStroke();
- rect(width/2,height/2,W,H);
- if(play)
- b.move();
- b.display();
- if(frameCount > 1)
- r1.display();
- r1.move();
- r2.display();
- r2.move2();
- // samo za razmak od ruba
- fill(244);
- noStroke();
- // rect(width/2,height,W,12);
- // rect(width/2,0,W,12);
- // podrucje oko polja
- //noStroke();
- //fill(30, 200, 140);
- //rect(lb/2, H/2, lb, H);
- //rect(lb+W+lb/2, H/2, lb, H);
- if(b.pos.y>limy){
- ez = minez;
- }
- else{
- ez=map(b.pos.y,0,limy,maxez,minez);
- }
- xx = easy(b.pos.x,xx,ez);
- // stop igra
- if(b.pos.y + b.Ry - 50> H || b.pos.y - b.Ry + 50 < 0){
- play = false;
- if(b.pos.y > H/2)
- rez2++;
- else
- rez1++;
- b.pos.set(width/2, height/2);
- b.fels.mult(0);
- b.vel.x = random(-0.1,0.1);
- b.vel.y = random(-3,3);
- b.vel.normalize();
- b.vel.mult(9);
- b.dd = 0;
- }
- }
- class Ball{
- PVector pos;
- PVector ppos;
- PVector vel,pvel;
- float scx,scy;
- float Rx, Ry;
- float d, dx, dy;
- float dd;
- PVector fels;
- Ball(){
- pos = new PVector(width/2,height/2);
- ppos = pos.get();
- vel = new PVector(random(-0.1,0.1),random(-3,3));
- vel.normalize();
- vel.mult(9);
- pvel=vel.get();
- Rx = bgR;
- Ry = bgR;
- }
- void move(){
- ppos = pos.get();
- pos.add(vel);
- Rx = bgR;
- Ry = bgR;
- // odbijanje od ploce
- if(pos.y<bgR+D &&
- ppos.y>bgR+D &&
- abs(r2.pos.x-pos.x)<wid
- ||
- pos.y>height-bgR-D &&
- ppos.y<height-bgR-D &&
- abs(r1.pos.x-pos.x)<wid){
- vel.y*=-1;
- if(pos.y < H/2)
- pos.y = pos.y + (bgR+D-pos.y)*2;
- else
- pos.y = pos.y - (pos.y-height+bgR+D)*2;
- if(pos.y<height/2){
- PVector p;
- dd = r2.pos.x - r2.ppos.x;
- dd /= W;
- }
- if(pos.y>height/2){
- PVector p;
- dd = r1.pos.x - r1.ppos.x;
- dd /= W;
- }
- }
- // odbijanje od zida
- if(pos.x>rb-bgR){
- pos.x = pos.x - (pos.x-rb+bgR)*2;
- vel.x*=-1;
- dd = 0;
- }
- if(pos.x<lb+bgR){
- pos.x = pos.x + (lb+bgR-pos.x)*2;
- vel.x*=-1;
- dd = 0;
- }
- fels = new PVector(dd, 0);
- vel.add(fels);
- }
- void display(){
- fill(33);
- noStroke();
- pushMatrix();
- translate(pos.x - dx,pos.y - dy);
- //rotate(vel.heading());
- ellipse(0,0, bgR*2, bgR*2);
- popMatrix();
- }
- }
- class Rekt{
- PVector pos;
- PVector ppos;
- Rekt(float x,float y){
- ppos=new PVector(x,y);
- pos=new PVector(x,y);
- }
- void move(){
- ppos.x=easy(pos.x,ppos.x,0.4);
- pos.x=mouseX;
- if(pos.x-wid<lb)
- pos.x = lb+wid;
- if(pos.x+wid>rb)
- pos.x = rb-wid;
- }
- void move2(){
- pos.x = xx;
- if(pos.x-wid<lb)
- pos.x = lb+wid;
- if(pos.x+wid>rb)
- pos.x = rb-wid;
- }
- void display(){
- fill(33);
- noStroke();
- rect(pos.x,pos.y,wid*2,2*D,4);
- }
- }
- float power(float p, float g) {
- if (p < 0.5)
- return 0.5 * pow(2*p, g);
- else
- return 1 - 0.5 * pow(2*(1 - p), g);
- }
- float easy(float target, float k, float easing) {
- float d = target - k;
- if (abs(d) > 1) {
- k += d * easing;
- }
- return k;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement