Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. var ball;
  2. var dx = 4;
  3. var dy = 4;
  4.  
  5. function start(){
  6. ball = new Circle(20);
  7. ball.setPosition(100, 100);
  8. add(ball);
  9.  
  10. setTimer(draw, 20);
  11. }
  12.  
  13. function draw(){
  14. checkWalls();
  15. ball.move(dx, dy);
  16. }
  17.  
  18. function checkWalls(){
  19. // Bounce off right wall
  20. if(ball.getX() + ball.getRadius() > getWidth()){
  21. dx = -dx;
  22. }
  23.  
  24. // Bounce off left wall
  25. if(ball.getX() - ball.getRadius() < 0){
  26. dx = -dx;
  27. }
  28.  
  29. // Bounce off bottom wall
  30. if(ball.getY() + ball.getRadius() > getHeight()){
  31. dy = -dy;
  32. }
  33.  
  34. // Bounce off top wall
  35. if(ball.getY() - ball.getRadius() < 0){
  36. dy = -dy;
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement