Advertisement
999ms

13.12.2018myProject

Dec 13th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var canvas;
  2. var ctx;
  3. const H=600, W=1600,imgSize = 512;
  4. var constantObjects = [],variableObjects = [];
  5. var may = false;
  6. var deltaX = 0,deltaY = 0,shId = 0;
  7. function init() {
  8.   canvas = document.getElementById('myCanvas');
  9.   canvas.height = window.innerHeight;
  10.   canvas.width  = window.innerWidth;
  11.   ctx = canvas.getContext('2d');
  12.   variableObjects.push(["sh_left.png",300,0,0,0,0.95, 10, 150]);
  13.   constantObjects.push(["floor.png",0,H]);
  14. }
  15. var timerId = setInterval(function() {upd();}, 1000/60);
  16. function upd(){
  17.     for(var i=0;i<constantObjects.length;i++){
  18.       var img = new Image();
  19.       var cur = constantObjects[i];
  20.       img.src = cur[0];
  21.       drawImg(img, cur[1],cur[2]);
  22.     }
  23.     var rem = [];
  24.     for(var i=0;i<variableObjects.length;i++){
  25.       var img = new Image();
  26.       var cur = variableObjects[i];
  27.       img.src = cur[0];
  28.       if(getTimeMs()-lastClearTime>40){
  29.         lastClearTime = getTimeMs();
  30.         ctx.clearRect(0,0, W, H+200);
  31.       }
  32.       cur[1] += cur[3];
  33.       cur[2] += cur[4]+cur[6];
  34.       cur[3]*= cur[5];
  35.       cur[4]*= cur[5];
  36.       cur[2] = Math.min(H-cur[7], cur[2]);
  37.       cur[2] = Math.max(0, cur[2]);
  38.       if(cur[3]<0 && cur[0]=="sh_right.png") cur[0] = "sh_left.png";
  39.       if(cur[3]>0 && cur[0]=="sh_left.png") cur[0] = "sh_right.png";
  40.       if(cur[0][0]=='f'){
  41.         if(cur[1]<-50 || cur[1]>W){
  42.           rem.push(i);
  43.         }
  44.       }
  45.       img.src = cur[0];
  46.       drawImg(img, cur[1],cur[2]);
  47.     }
  48.     for(var i=0;i<variableObjects.length;i++)
  49.       if(variableObjects[i][0][0]=='s') shId = i;
  50.     for(var i=0; i<rem.length;i++){
  51.       variableObjects.splice(rem[i],1);
  52.     }
  53. }
  54.  
  55.  
  56. //onmousemove
  57. //onmousedown
  58.  
  59. function getTimeMs() {
  60.   var date = new Date();
  61.   return date.getMilliseconds()+date.getSeconds()*1000+date.getMinutes()*1000*60+date.getHours()*60*60*1000;
  62. }
  63.  
  64.  
  65.  
  66. function drawImg(img, x,y){
  67.   img.onload =
  68.   function(){
  69.     ctx.drawImage(img, x, y);
  70.   }
  71. }
  72. var lastSpaceTime = 0;
  73. var lastClearTime = 0;
  74. onkeypress = function(event){
  75.   var key = event.charCode;
  76.   if(key==32)
  77.     if(getTimeMs()-lastSpaceTime>900){
  78.         lastSpaceTime = getTimeMs();
  79.         variableObjects[shId][4]=-30;
  80.     }
  81.   if(key==100) variableObjects[shId][3]=10;
  82.   if(key==97) variableObjects[shId][3]=-10;
  83. }
  84. onkeydown = function(e) {
  85.   var key = e.keyCode;
  86.   if(key==13)
  87.     fire();
  88. }
  89. var lastFireActiveTime=0;
  90. function fire(){
  91.   if(getTimeMs()-lastFireActiveTime<300) return;
  92.   lastFireActiveTime = getTimeMs();
  93.   var curY = variableObjects[shId][2];
  94.   var curX = variableObjects[shId][1];
  95.   if(variableObjects[shId][0][3]=='r')  variableObjects.push(['fire_right.png',curX+160, curY+80, 50,0,1,0,0]);
  96.   else variableObjects.push(['fire_left.png',curX, curY+80, -50,0,1,0,0]);
  97. }
  98. function getRandomColor() {
  99.   var letters = '0123456789ABCDEF';
  100.   var color = '#';
  101.   for (var i = 0; i < 6; i++)
  102.   color += letters[Math.floor(Math.random() * 16)];
  103.   return color;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement