atm959

Mudkip Canvas Drawer Code

Jan 20th, 2017
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.addEventListener("load", onLoad);
  2.  
  3. var canvas;
  4. var context;
  5. var myImage;
  6. var mudkips = [];
  7. var frameCount = 0;
  8.  
  9. function Mudkip(){
  10.     this.x = Math.random() * (canvas.width - 64);
  11.     this.y = Math.random() * (canvas.height - 64);
  12.     this.spd = (Math.random() * 2) + 1;
  13.    
  14.     this.setRandomSpeed = function(){
  15.         this.spd = (Math.random() * 2) + 1;
  16.     }
  17.    
  18.     this.setRandomX = function(){
  19.         this.x = Math.random() * (canvas.width - 64);
  20.     }
  21.    
  22.     this.update = function(){
  23.         this.y += this.spd;
  24.         if(this.y > canvas.height){
  25.             this.y = -64;
  26.             this.setRandomX();
  27.             this.setRandomSpeed();
  28.         }
  29.     }
  30.    
  31.     this.display = function(){
  32.         context.drawImage(myImage, this.x, this.y, 64, 64);
  33.     }
  34. }
  35.  
  36. function onLoad(event){
  37.     var song = document.getElementById("song");
  38.     song.play();
  39.     canvas = document.getElementById("mudkipCanvas");
  40.     context = canvas.getContext("2d");
  41.     myImage = document.createElement("img");
  42.     myImage.src = "MudkipImage/Mudkip.png";
  43.    
  44.     for(i = 0; i < 100; i++){
  45.         mudkips.push(new Mudkip());
  46.     }
  47.    
  48.     animate();
  49. }
  50.  
  51. //This is called 60 times per second, or once every frame.
  52. function animate(){
  53.     frameCount++;
  54.     document.getElementById("frameCount").innerHTML = "This has been running for " + frameCount + " frames.";
  55.     context.fillStyle = "white";
  56.     context.fillRect(0, 0, canvas.width, canvas.height);
  57.    
  58.     for(i = 0; i < mudkips.length; i++){
  59.         mudkips[i].update();
  60.         mudkips[i].display();
  61.     }
  62.    
  63.     //This tells us to run the animate() function again on the next frame.
  64.     requestAnimationFrame(animate);
  65. }
Advertisement
Add Comment
Please, Sign In to add comment