Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. var r,g,b;
  2.  
  3. var ball = {
  4. x: 300,
  5. y: 200,
  6. xspeed: 4,
  7. yspeed: -3
  8. }
  9.  
  10. function setup() {
  11. createCanvas(600, 400);
  12.  
  13.  
  14. }
  15.  
  16. function draw() {
  17. background(0);
  18. move();
  19. bounce();
  20. display();
  21. colorChange();
  22. r = random(255);
  23. g = random(255);
  24. b = random(255);
  25.  
  26. }
  27.  
  28.  
  29.  
  30. function bounce() {
  31.  
  32. if (ball.x > width || ball.x < 0) {
  33. ball.xspeed = ball.xspeed * -1;
  34. }
  35.  
  36. if (ball.y > height || ball.y < 0) {
  37. ball.yspeed = ball.yspeed * -1;
  38. }
  39. }
  40.  
  41. function display() {
  42. stroke(255);
  43. strokeWeight(4);
  44. fill(200,0,200);
  45. ellipse(ball.x, ball.y, 24, 24);
  46.  
  47.  
  48.  
  49. }
  50.  
  51. function move() {
  52. ball.x = ball.x + ball.xspeed;
  53. ball.y = ball.y + ball.yspeed;
  54.  
  55. }
  56.  
  57.  
  58. function colorChange() {
  59. if (ball.x >= width || ball.x <= 0 || ball.y >= height || ball.y<= 0) {
  60. fill(r,g,b);
  61. ellipse();
  62.  
  63.  
  64. }
  65.  
  66.  
  67.  
  68. }
  69.  
  70. var ball = {
  71. x: 300,
  72. y: 200,
  73. xspeed: 4,
  74. yspeed: -3,
  75. color: { r: 200, g: 0, b: 200 } // else you could use an array [200, 0, 200]
  76. }
  77.  
  78. fill(ball.color.r, ball.color.g, ball.color.b); // or if using the array fill(ball.color[0], ball.color[1], ball.color[2]);
  79.  
  80. function colorChange() {
  81. ball.color.r = random(255);
  82. ball.color.g = random(255);
  83. ball.color.b = random(255);
  84. }
  85.  
  86. function bounce() {
  87. if (ball.x > width || ball.x < 0) {
  88. ball.xspeed *= -1;
  89. colorChange();
  90. }
  91.  
  92. if (ball.y > height || ball.y < 0) {
  93. ball.yspeed *= -1;
  94. colorChange();
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement