Advertisement
Guest User

Pong Workshop 2B PCD-2020-SP

a guest
Jan 18th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. float x, y;
  2. float vx, vy;
  3. float REx, REy, RDx, RDy, Rl, Ra;
  4. boolean w, s, cima, baixo;
  5. int PE, PD;
  6.  
  7.  
  8. void setup(){
  9. size(1000, 600);// width, height
  10.  
  11. bola_reset( int(random(-1, 1)) );
  12.  
  13. Rl = 30;
  14. Ra = 135;
  15. REx = 5;
  16. REy = height/2 - Ra/2;
  17. RDx = width - 5 - Rl;
  18. RDy = REy;
  19.  
  20. background(0);
  21. strokeWeight(4);
  22. textSize( 40 );
  23. }
  24.  
  25. void draw(){
  26. background(0);
  27. stroke(255);
  28. line( 500, 0, 500, height );
  29. fill(255);
  30. noStroke();
  31. ellipse( x, y, 25, 25 );
  32. rect( REx, REy, Rl, Ra );
  33. rect( RDx, RDy, Rl, Ra );
  34. text( PE, 250, 50 );
  35. text( PD, 750, 50 );
  36.  
  37. x = x + vx;
  38. y = y + vy;
  39.  
  40. if ( (x < REx + Rl) && (y > REy) && (y < REy + Ra) ){//colisão com raquete esquerda
  41. vx = vx * -1;
  42. x = REx + Rl;//restituindo a posição da bolinha para corrigir o bug do zig-zag...
  43. }
  44. if( (x > RDx) && (y > RDy) && (y < RDy + Ra ) ){
  45. vx = vx * -1;
  46. x = RDx;//restituindo a posição da bolinha para corrigir o bug do zig-zag...
  47. }
  48. if( y < 0 ) vy = vy * -1;
  49. if( y > height ) vy = vy * -1;
  50.  
  51. if( x < 0 ){// bola saiu pela esquerda.
  52. bola_reset( -1 );
  53. PD = PD + 1;
  54. }
  55. if( x > width ){// bola saiu pela direita.
  56. bola_reset( 1 );
  57. PE = PE + 1;
  58. }
  59.  
  60.  
  61. if( w ) REy = REy - 7;
  62. if( s ) REy = REy + 7;
  63. if( cima ) RDy = RDy - 7;
  64. if( baixo ) RDy = RDy + 7;
  65. }
  66. //
  67. void keyPressed(){
  68. if( key == 'w' ) w = true;
  69. if( key == 's' ) s = true;
  70. if( keyCode == UP ) cima = true;
  71. if( keyCode == DOWN ) baixo = true;
  72. }
  73. void keyReleased(){
  74. if( key == 'w' ) w = false;
  75. if( key == 's' ) s = false;
  76. if( keyCode == UP ) cima = false;
  77. if( keyCode == DOWN ) baixo = false;
  78. }
  79.  
  80. void bola_reset(int Q ){
  81. x = 500;
  82. y = 300;
  83. float angulo = random(-QUARTER_PI, QUARTER_PI);//direção aleatória
  84. if( Q > 0 ) angulo += PI;
  85. float v = 4; //velocidade constante
  86. vx = v * cos( angulo );//random(-4, 4);
  87. vy = v * sin( angulo );//random(-4, 4);
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement