Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var canvas;
- var ctx;
- const H=600, W=1600,imgSize = 512;
- var constantObjects = [],variableObjects = [];
- var may = false;
- var deltaX = 0,deltaY = 0,shId = 0;
- function init() {
- canvas = document.getElementById('myCanvas');
- canvas.height = window.innerHeight;
- canvas.width = window.innerWidth;
- ctx = canvas.getContext('2d');
- variableObjects.push(["sh_left.png",300,0,0,0,0.95, 10, 150]);
- constantObjects.push(["floor.png",0,H]);
- }
- var timerId = setInterval(function() {upd();}, 1000/60);
- function upd(){
- for(var i=0;i<constantObjects.length;i++){
- var img = new Image();
- var cur = constantObjects[i];
- img.src = cur[0];
- drawImg(img, cur[1],cur[2]);
- }
- var rem = [];
- for(var i=0;i<variableObjects.length;i++){
- var img = new Image();
- var cur = variableObjects[i];
- img.src = cur[0];
- if(getTimeMs()-lastClearTime>40){
- lastClearTime = getTimeMs();
- ctx.clearRect(0,0, W, H+200);
- }
- cur[1] += cur[3];
- cur[2] += cur[4]+cur[6];
- cur[3]*= cur[5];
- cur[4]*= cur[5];
- cur[2] = Math.min(H-cur[7], cur[2]);
- cur[2] = Math.max(0, cur[2]);
- if(cur[3]<0 && cur[0]=="sh_right.png") cur[0] = "sh_left.png";
- if(cur[3]>0 && cur[0]=="sh_left.png") cur[0] = "sh_right.png";
- if(cur[0][0]=='f'){
- if(cur[1]<-50 || cur[1]>W){
- rem.push(i);
- }
- }
- img.src = cur[0];
- drawImg(img, cur[1],cur[2]);
- }
- for(var i=0;i<variableObjects.length;i++)
- if(variableObjects[i][0][0]=='s') shId = i;
- for(var i=0; i<rem.length;i++){
- variableObjects.splice(rem[i],1);
- }
- }
- //onmousemove
- //onmousedown
- function getTimeMs() {
- var date = new Date();
- return date.getMilliseconds()+date.getSeconds()*1000+date.getMinutes()*1000*60+date.getHours()*60*60*1000;
- }
- function drawImg(img, x,y){
- img.onload =
- function(){
- ctx.drawImage(img, x, y);
- }
- }
- var lastSpaceTime = 0;
- var lastClearTime = 0;
- onkeypress = function(event){
- var key = event.charCode;
- if(key==32)
- if(getTimeMs()-lastSpaceTime>900){
- lastSpaceTime = getTimeMs();
- variableObjects[shId][4]=-30;
- }
- if(key==100) variableObjects[shId][3]=10;
- if(key==97) variableObjects[shId][3]=-10;
- }
- onkeydown = function(e) {
- var key = e.keyCode;
- if(key==13)
- fire();
- }
- var lastFireActiveTime=0;
- function fire(){
- if(getTimeMs()-lastFireActiveTime<300) return;
- lastFireActiveTime = getTimeMs();
- var curY = variableObjects[shId][2];
- var curX = variableObjects[shId][1];
- if(variableObjects[shId][0][3]=='r') variableObjects.push(['fire_right.png',curX+160, curY+80, 50,0,1,0,0]);
- else variableObjects.push(['fire_left.png',curX, curY+80, -50,0,1,0,0]);
- }
- function getRandomColor() {
- var letters = '0123456789ABCDEF';
- var color = '#';
- for (var i = 0; i < 6; i++)
- color += letters[Math.floor(Math.random() * 16)];
- return color;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement