Advertisement
Guest User

Untitled

a guest
May 21st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. int PadSX_Y, PadDX_Y, hWd, directionX, p1_Score, p2_Score;
  2. float Player_Movement, Player2_Movement, Player_Speed, Ball_Speed, BallX, BallY, directionY, difficulty;
  3.  
  4. void setup(){
  5. fullScreen(1);
  6. background(0);
  7. stroke(255);
  8. rectMode(CENTER);
  9. ellipseMode(CENTER);
  10. textSize(80);
  11.  
  12. PadSX_Y = height/2;
  13. PadDX_Y = PadSX_Y;
  14. hWd = width/2;
  15. Player_Movement=0;
  16. Player2_Movement=0;
  17. Player_Speed = 5;
  18. Ball_Speed = 5;
  19. BallX = width/2;
  20. BallY = height/2;
  21. directionY=0.25;
  22. directionX=-1;
  23. difficulty=0.05;
  24. p1_Score=0;
  25. p2_Score=0;
  26. }
  27.  
  28. void drawField(){
  29. //Creazione dei Muri
  30. strokeWeight(10);
  31. text(p1_Score, width*0.4, height*0.3);
  32. text(p2_Score, width*0.6-40, height*0.3);
  33.  
  34. line(width*0.1, height*0.2, width*0.9, height*0.2);//Muro Nord
  35. line(width*0.1, height*0.2, width*0.1, height*0.8);//Muro Ovest
  36. line(width*0.1, height*0.8, width*0.9, height*0.8);//Muro Sud
  37. line(width*0.9, height*0.8, width*0.9, height*0.2);//Muro Est
  38. //Linea tratteggiata del centro campo
  39. strokeWeight(2);
  40. stroke(130);
  41. line(hWd, height*0.23, hWd, height*0.29);
  42. line(hWd, height*0.35, hWd, height*0.41);
  43. line(hWd, height*0.47, hWd, height*0.53);
  44. line(hWd, height*0.59, hWd, height*0.65);
  45. line(hWd, height*0.71, hWd, height*0.77);
  46. stroke(255);
  47. }
  48.  
  49. void draw(){
  50. background(0);
  51. drawField(); //Disegno Campo
  52.  
  53. //Disegno Padella Sinistra
  54. rect(width*0.125, PadSX_Y, 15, height*0.12);
  55. //Disegno Padella Destra
  56. rect(width*0.875, PadDX_Y, 15, height*0.12);
  57. //Disegno Palla
  58. rect(BallX, BallY, 30, 30);
  59.  
  60. //Movimento Giocatore 1
  61. if(!keyPressed){
  62. Player2_Movement=0;
  63. }
  64. if( !(PadSX_Y>height*0.8-height*0.06 && Player2_Movement >0) && !(PadSX_Y<height*0.2+height*0.06 && Player2_Movement<0)){ //Questo if รจ scritto ammerda, sistemare
  65. PadSX_Y += Player2_Movement;
  66. }
  67.  
  68. //Movimento Giocatore 2
  69. if(!keyPressed){
  70. Player_Movement=0;
  71. }
  72. if(!(PadDX_Y>height*0.8-height*0.06 && Player_Movement >0) && !(PadDX_Y<height*0.2+height*0.06 && Player_Movement<0)){ //Pure questo
  73. PadDX_Y += Player_Movement;
  74. }
  75.  
  76. //Movimento Palla
  77. BallX += directionX * Ball_Speed;
  78. BallY += directionY*Ball_Speed;
  79.  
  80. //Controllo Collisioni
  81. padCollision();
  82. wallCollision();
  83. if(BallX<width*0.125-(15/2)){
  84. roundReset("p1");
  85. }else{
  86. if(BallX>width*0.875+(15/2)){
  87. roundReset("p2");
  88. }
  89. }
  90.  
  91. }
  92.  
  93. void roundReset(String winner){
  94. if(winner=="p1"){
  95. p2_Score++;
  96. BallX=width/2;
  97. BallY=height/2;
  98. directionX=1;
  99. }else{
  100. p1_Score++;
  101. BallX=width/2;
  102. BallY=height/2;
  103. directionX=-1;
  104. }
  105. directionY=0.25;
  106. }
  107.  
  108. void padCollision(){
  109. if( (BallX <width*0.125+30 && PadSX_Y+height*0.06>BallY && PadSX_Y-height*0.06<BallY ) ){
  110. float newDirection = map(BallY, PadSX_Y-height*0.06, PadSX_Y+height*0.06, -2, 2);
  111. directionY = newDirection;
  112. Ball_Speed+=difficulty;
  113. directionX*=-1;
  114. }
  115. }
  116.  
  117. void wallCollision(){
  118. if( BallY<height*0.2+15 || BallY>height*0.8-15){
  119. directionY*=-1;
  120. }
  121. }
  122.  
  123. void keyPressed(){
  124.  
  125. if(keyPressed){
  126. if(key == CODED){
  127. if(keyCode == DOWN){
  128. Player_Movement = 0;
  129.  
  130. Player_Movement = 1*Player_Speed;
  131.  
  132. }else{
  133. if(keyCode == UP){
  134. Player_Movement = 0;
  135. Player_Movement = -1*Player_Speed;
  136. }
  137. }
  138. }
  139. if(key == 's'){
  140. Player2_Movement = 0;
  141. Player2_Movement = 1*Player_Speed;
  142. println("Check S");
  143. }else{
  144. if(key== 'w'){
  145. Player2_Movement=0;
  146. Player2_Movement = -1*Player_Speed;
  147. println("Check W");
  148. }
  149. }
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement