Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float x, y;
- float vx, vy;
- float REx, REy, RDx, RDy, Rl, Ra;
- boolean w, s, cima, baixo;
- int PE, PD;
- void setup(){
- size(1000, 600);// width, height
- bola_reset( int(random(-1, 1)) );
- Rl = 30;
- Ra = 135;
- REx = 5;
- REy = height/2 - Ra/2;
- RDx = width - 5 - Rl;
- RDy = REy;
- background(0);
- strokeWeight(4);
- textSize( 40 );
- }
- void draw(){
- background(0);
- stroke(255);
- line( 500, 0, 500, height );
- fill(255);
- noStroke();
- ellipse( x, y, 25, 25 );
- rect( REx, REy, Rl, Ra );
- rect( RDx, RDy, Rl, Ra );
- text( PE, 250, 50 );
- text( PD, 750, 50 );
- x = x + vx;
- y = y + vy;
- if ( (x < REx + Rl) && (y > REy) && (y < REy + Ra) ){//colisão com raquete esquerda
- vx = vx * -1;
- x = REx + Rl;//restituindo a posição da bolinha para corrigir o bug do zig-zag...
- }
- if( (x > RDx) && (y > RDy) && (y < RDy + Ra ) ){
- vx = vx * -1;
- x = RDx;//restituindo a posição da bolinha para corrigir o bug do zig-zag...
- }
- if( y < 0 ) vy = vy * -1;
- if( y > height ) vy = vy * -1;
- if( x < 0 ){// bola saiu pela esquerda.
- bola_reset( -1 );
- PD = PD + 1;
- }
- if( x > width ){// bola saiu pela direita.
- bola_reset( 1 );
- PE = PE + 1;
- }
- if( w ) REy = REy - 7;
- if( s ) REy = REy + 7;
- if( cima ) RDy = RDy - 7;
- if( baixo ) RDy = RDy + 7;
- }
- //
- void keyPressed(){
- if( key == 'w' ) w = true;
- if( key == 's' ) s = true;
- if( keyCode == UP ) cima = true;
- if( keyCode == DOWN ) baixo = true;
- }
- void keyReleased(){
- if( key == 'w' ) w = false;
- if( key == 's' ) s = false;
- if( keyCode == UP ) cima = false;
- if( keyCode == DOWN ) baixo = false;
- }
- void bola_reset(int Q ){
- x = 500;
- y = 300;
- float angulo = random(-QUARTER_PI, QUARTER_PI);//direção aleatória
- if( Q > 0 ) angulo += PI;
- float v = 4; //velocidade constante
- vx = v * cos( angulo );//random(-4, 4);
- vy = v * sin( angulo );//random(-4, 4);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement