Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. // BALL
  2.  
  3. import java.awt.geom.*;
  4.  
  5. public class Ball {
  6.  
  7. Rectangle rectangle;
  8. // BALL PROPERTIES --
  9. int width = 8; // Here, I changed the width and height of the ball.
  10. int height = 8;
  11. int hue=0;
  12. boolean hasFill = true;
  13. color fillColor = color(hue, 80, 80);
  14. boolean hasStroke = false;
  15. color strokeColor = #FFFFFF;
  16. // velocity
  17. int velX = 5; //Here I changed the velocity of the ball
  18. int velY = 5;
  19. //
  20. int x;
  21. int y;
  22. int ox;
  23. int oy;
  24. int xcentre;
  25. int ycentre;
  26. //
  27.  
  28.  
  29.  
  30.  
  31. Ball(int X, int Y) {
  32. x = X;
  33. y = Y;
  34. rectangle = new Rectangle(width, height, hasStroke, strokeColor, hasFill, fillColor);
  35. rectangle.setPosition(x, y);
  36. }
  37.  
  38.  
  39.  
  40.  
  41. void refresh() {
  42. updatePosition();
  43. rectangle.setPosition(x, y);
  44. rectangle.drawYourself();
  45. }
  46.  
  47.  
  48.  
  49.  
  50. void updatePosition() {
  51. // add velocity to position
  52. x+=velX;
  53. y+=velY;
  54. // collision with limits
  55. if (x<=0 || x>=gameFrameWidth-width) {
  56. velX = -velX;
  57. x = constrain(x, 0, gameFrameWidth-width);
  58. }
  59. if (y<=0 || y>=gameFrameHeight-height) {
  60. velY = -velY;
  61. y = constrain(y, 0, gameFrameHeight-height);
  62. }
  63. xcentre = x+width/2;
  64. ycentre = y+height/2;
  65. // collision with paddle
  66. int result = checkCollisionWithRectangle(paddle.rectangle);
  67. // if collides on top, control direction of ball
  68. if (result == 1) {
  69. if (xcentre < paddle.rectangle.x1+paddle.rectangle.width/2) {
  70. if (velX>0) {
  71. velX = -velX;
  72. }
  73. } else {
  74. if (velX<0) {
  75. velX = -velX;
  76. }
  77. }
  78. }
  79. // collision with bricks
  80. if (result == 0) {
  81. for (int i=0; i<bricks.length; i++) {
  82. if (bricks[i].imAlive) {
  83. int res = checkCollisionWithRectangle(bricks[i].rectangle);
  84. if (res != 0) {
  85. this.hue+=20;
  86. hue%=360;
  87. rectangle.fillColor = color(hue, 80, 80);
  88. rectangle.strokeColor = color(hue, 80, 80);
  89. bricks[i].die();
  90. break;
  91. }
  92. }
  93. }
  94. }
  95. ox = x;
  96. oy = y;
  97. }
  98.  
  99.  
  100. // FUNCION DETECCION DE COLISION --
  101. // result: 0: no collision 1: top 2: right 3: bottom 4: left 5: couldn't detect which side
  102. int checkCollisionWithRectangle (Rectangle R) {
  103. int result = 0;
  104. if (R.doesPointTouchMe(xcentre, ycentre)) {
  105. // which side did it collide
  106. Line2D lineaBola = new Line2D.Float(xcentre, ycentre, ox+width/2, oy+height/2);
  107. result = R.whatSideDoesLineTouch(lineaBola, velX, velY);
  108. // top
  109. if (result==1) {
  110. velY = -velY;
  111. y = R.y1-height;
  112. // right
  113. } else if (result==2) {
  114. velX = -velX;
  115. x = R.x2;
  116. // bottom
  117. } else if (result==3) {
  118. velY = -velY;
  119. y = R.y2;
  120. // left
  121. } else if (result==4) {
  122. velX = -velX;
  123. x = R.x1-width;
  124. } else {
  125. result = 5;
  126. }
  127. }
  128. return result;
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement