MrThoe

PaddleBall

Sep 17th, 2020
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. var radius;
  2. var x, y, Vx, Vy;
  3. var padX, padY;
  4.  
  5. /*Only runs once*/
  6. function setup() {
  7. createCanvas(300, 400);
  8. radius = 35;
  9. x = 20;
  10. y = 40;
  11. Vx = 1;
  12. Vy = 2;
  13. padX = 0;
  14. padY = height - 50;
  15. }
  16.  
  17. /*Runs on an infinite loop*/
  18. function draw() {
  19. background(120);
  20. ball();
  21. paddle();
  22. bounce();
  23. }
  24.  
  25. function bounce(){
  26. if(abs(x-padX)< 50+radius && abs(padY-y)<radius/2){
  27. //reverse y of the ball;
  28. Vy *= -1;
  29. }
  30. }
  31.  
  32. function paddle(){
  33. padX = mouseX - 50;
  34. rect(padX, padY, 100, 20);
  35. }
  36.  
  37. function ball(){
  38. ellipse(x, y, radius, radius);
  39. y+=Vy; //y = y + Vy;
  40. x+=Vx;
  41. if(y > height){
  42. y = 0;
  43. }
  44. if(x > width){
  45. x = 0;
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment