Guest User

Untitled

a guest
Dec 22nd, 2014
1,050
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  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. var player = {
  12. x:50,
  13. spdX:30,
  14. y:40,
  15. spdY:5,
  16. name:'P',
  17. };
  18.  
  19.  
  20. var enemy = {
  21. x:150,
  22. spdX:10,
  23. y:350,
  24. spdY:15,
  25. name:'E',
  26. };
  27. var enemy2 = {
  28. x:150,
  29. spdX:10,
  30. y:350,
  31. spdY:15,
  32. name:'E',
  33. };
  34. setInterval(update,40);
  35.  
  36. function updateEntity(something){
  37. something.x += something.spdX;
  38. something.y += something.spdY;
  39. ctx.fillText(something.name,something.x,something.y);
  40.  
  41.  
  42. if(something.x < 0 || something.x > WIDTH){
  43. console.log(message);
  44. something.spdX = -something.spdX;
  45. }
  46. if(something.y < 0 || something.y > HEIGHT){
  47. console.log(message);
  48. something.spdY = -something.spdY;
  49. }
  50. }
  51.  
  52.  
  53. function update(){
  54. updateEntity(enemy);
  55. updateEntity(enemy2);
  56. updateEntity(player);
  57.  
  58. }
  59.  
  60.  
  61.  
  62.  
  63.  
  64. </script>
Add Comment
Please, Sign In to add comment