daily pastebin goal
27%
SHARE
TWEET

Untitled

a guest Jan 20th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var canvas = document.getElementById('canvas');
  2. var ctx = canvas.getContext('2d');
  3.  
  4. var ship = new Ship();
  5. var bullets = [];
  6. function allUpdate(){
  7.     drawBackground();
  8.     ship.update();
  9.     //for loop to draw all bullets
  10.     for(var i =0;i < bullets.length;i++){
  11.         var bullet = bullets[i];
  12.         bullet.update();
  13.     }
  14.  
  15.     window.requestAnimationFrame(allUpdate);
  16. }
  17. window.requestAnimationFrame(allUpdate);
  18.  
  19. window.onkeydown = function(event){
  20.     var left = 37;
  21.     var up = 38;
  22.     var right = 39;
  23.     var down = 40;
  24.  
  25.     if(event.keyCode == up)
  26.         ship.dY = -1;
  27.     if(event.keyCode == down)
  28.         ship.dY = 1;
  29.     if(event.keyCode ==  left){
  30.         ship.isRotating = true;
  31.         ship.theta = -5;  
  32.     }
  33.     if(event.keyCode == right){
  34.         ship.isRotating = true;
  35.         ship.theta = 5;
  36.     }
  37.     if(event.key == " "){
  38.         var x = ship.x + ship.width;
  39.         var y = ship.y + ship.height/2;
  40.         var bullet = new Bullet(x,y);
  41.         bullets.push(bullet);
  42.     }
  43.  
  44. }
  45. window.onkeyup = function(event){
  46.         var left = 37;
  47.         var up = 38;
  48.         var right = 39;
  49.         var down = 40;
  50.         if(event.keyCode == left || event.keyCode == right){
  51.             ship.isRotating = false;
  52.             ship.theta = 0;
  53.         }
  54.  
  55.     }
  56.  
  57. var Bullet = function(x,y){
  58.     this.x = x;
  59.     this.y = y;
  60.     this.r = 5;
  61.     this.dX = 2;
  62.     this.dY = 2;
  63.  
  64.     this.update = function(){
  65.         ctx.fillStyle = "blue";
  66.         ctx.beginPath();
  67.         ctx.arc(this.x+=this.dX,this.y,this.r,0,Math.PI *2,false);
  68.         ctx.closePath();
  69.         ctx.fill();
  70.     }
  71.  
  72. }
  73.  
  74.  
  75. function Ship(){
  76.     this.isRotating = false;
  77.     this.theta = 0; //in degrees
  78.     this.width = 30;
  79.     this.height = 10;
  80.     this.x = 0;
  81.     this.y = canvas.height/2;
  82.     this.dX = 1;
  83.     this.dY = 0;
  84.     this.fillStyle = "white";
  85.     this.update = function() {
  86.         ctx.fillStyle = "white";
  87.         ctx.fillRect(this.x+=this.dX,this.y+=this.dY,this.width,this.height);
  88.         ctx.translate(this.x,this.y+(this.height/2));
  89.         ctx.rotate((this.theta) * Math.PI/180);
  90.         ctx.translate(-this.x,-(this.y +this.height/2));  
  91.     }
  92.  
  93. }
  94.  
  95. function drawBackground(){
  96.     ctx.fillStyle = "black";
  97.     ctx.fillRect(0,0,canvas.width,canvas.height);
  98. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top