daily pastebin goal
22%
SHARE
TWEET

Untitled

a guest Dec 14th, 2014 1,808 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <canvas id="ctx" width="500" height="500" style="border:1px solid #000000;"></canvas>
  2.  
  3. <script>  
  4. var ctx = document.getElementById("ctx").getContext("2d");
  5. ctx.font = '30px Arial';
  6.  
  7. var HEIGHT = 500;
  8. var WIDTH = 500;
  9. var message = 'Bouncing';
  10.  
  11. //player
  12. var x = 50;
  13. var spdX = 30;
  14. var y = 40;
  15. var spdY = 5;
  16. var name = 'P';
  17.  
  18. //enemy
  19. var enemy_x = 150;
  20. var enemy_spdX = 10;
  21. var enemy_y = 350;
  22. var enemy_spdY = 15;
  23. var enemy_name = 'E';
  24.  
  25.  
  26. setInterval(update,40);
  27.  
  28. function update(){
  29.         //player
  30.         x += spdX;
  31.         y += spdY;
  32.         ctx.fillText(name,x,y);
  33.                
  34.                
  35.         if(x < 0 || x > WIDTH){
  36.                 console.log(message);
  37.                 spdX = -spdX;
  38.         }
  39.         if(y < 0 || y > HEIGHT){
  40.                 console.log(message);
  41.                 spdY = -spdY;
  42.         }
  43.        
  44.         //enemy
  45.         enemy_x += enemy_spdX;
  46.         enemy_y += enemy_spdY;
  47.         ctx.fillText(enemy_name,enemy_x,enemy_y);
  48.                
  49.                
  50.         if(enemy_x < 0 || enemy_x > WIDTH){
  51.                 console.log(message);
  52.                 enemy_spdX = -enemy_spdX;
  53.         }
  54.         if(enemy_y < 0 || enemy_y > HEIGHT){
  55.                 console.log(message);
  56.                 enemy_spdY = -enemy_spdY;
  57.         }
  58. }
  59.  
  60.  
  61.  
  62.  
  63.  
  64. </script>
RAW Paste Data
Top