joemckay

Untitled

Dec 31st, 2015
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. ball ball;
  2. ArrayList <wall> walls = new ArrayList <wall>();
  3. void setup() {
  4. size(500, 700);
  5. ball = new ball();
  6. walls.add(new wall(100, 500, 200, 30));
  7. walls.add(new wall(200, 300, 200, 30));
  8. }
  9.  
  10. void draw() {
  11. background(255);
  12. for (int i = 0; i < walls.size(); i++) {
  13. wall dwall = walls.get(i);
  14. dwall.drawWall();
  15. dwall.ballCollision();
  16. }
  17. ball.drawBall();
  18. }
  19. class ball {
  20. float x, y, Dx, Dy;
  21. int r;
  22. float alt;
  23. float maxAlt;
  24. float speed;
  25. float maxSpeed;
  26. boolean goingUp;
  27. ball() {
  28. speed = 1;
  29. maxSpeed = 30;
  30. maxAlt = 500;
  31. goingUp = false;
  32. alt = 0;
  33. x = 300;
  34. y = 50;
  35. r = 50;
  36. Dx = 0;
  37. Dy = maxSpeed;
  38. }
  39.  
  40. void drawBall() {
  41. noStroke();
  42. fill(100, 50, 255);
  43. ellipse(x, y, r, r);
  44. moveBall();
  45. }
  46.  
  47. void moveBall() {
  48. if (mouseX < width / 2 && mousePressed) {
  49. Dx = -3;
  50. } else if (mouseX > width / 2 && mousePressed) {
  51. Dx = 3;
  52. } else {
  53. Dx = 0;
  54. }
  55. x = x + Dx;
  56. if (speed < maxSpeed) {
  57. speed += .1;
  58. }
  59.  
  60. y += speed; // this is the same as y = y + speed
  61. }
  62. }
  63.  
  64. class wall {
  65. float x, y, w, h;
  66. color wallColor;
  67.  
  68. wall(int _x, int _y, int _w, int _h) {
  69. wallColor = color(0, 0, 0);
  70. x = _x;
  71. y = _y;
  72. w = _w;
  73. h = _h;
  74. }
  75. void drawWall() {
  76. fill(wallColor);
  77. rect(x, y, w, h);
  78. }
  79. void ballCollision() {
  80. if ( ball.x + ball.r / 2 > x &&
  81. ball.x - ball.r / 2 < x + w &&
  82. ball.y + ball.r / 2 > y &&
  83. ball.y - ball.r / 2 < y + h) {
  84. ball.speed *= -1; // when it hits the wall, make speed reverse
  85. ball.y = y - ball.r / 2; // keeps it from getting "stuck" in the paddle
  86. }
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment