js实现canvas消灭新冠病毒小游戏代码
代码语言:html
所属分类:游戏
代码描述:js实现canvas消灭新冠病毒小游戏代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<html> <head> <style> body { background: #111; margin: 0; padding: 0 } canvas { background: #000; display: block; cursor: none } </style> </head> <body style=""> <canvas id="canvas" width="1280" height="720" style="width: 950px; height: 534px; margin-left: 0px; margin-top: 23px;"></canvas><script> "use strict" var stage = { w: 1280, h: 720 } var _pexcanvas = document.getElementById("canvas"); _pexcanvas.width = stage.w; _pexcanvas.height = stage.h; var ctx = _pexcanvas.getContext("2d"); var pointer = { x: stage.w/2, y: stage.h/4 } var scale = 1; var portrait = true; var loffset = 0; var toffset = 0; var mxpos = 0; var mypos = 0; // ------------------------------------------------------------------------------- Gamy var againprog = 0; var healthprog = 0; function newGame() { score = 0; health = 100; enemies = []; enemies.push(new Enemy()); enemies.push(new Enemy()); enemies.push(new Enemy()); againprog = 0; } function drawHeart(x, y, w) { ctx.beginPath(); ctx.arc(x-w/4, y, w/4, 0.75*Math.PI, 0); ctx.arc(x+w/4, y, w/4, 1*Math.PI, 2.25*Math.PI); ctx.lineTo(x, y+w/1.5); ctx.closePath(); ctx.fill(); } var Cannon = function(x, y, tx, ty) { this.x = x; this.y = y; this.tx = tx; this.ty = ty; this.r = 10; } var cannons = []; var gameover = false; cannons.push(new Cannon(stage.w, stage.h, stage.w/2, stage.h/2)); var firetm = 0; var fireact = true; var health = 100; var score = 0; var arm = { x: stage.w, y: stage.h }; var arm2 = { x: 0, y: stage.h }; var danger = false; var dangera = 0; var Enemy = function() { this.x = stage.w/2; this.y = stage.h/2; this.r = 10; this.tx = Math.floor(Math.random()*stage.w); this.ty = Math.floor(Math.random()*stage.h); this.des = false; this.eyeX = 0.4; this.eyeY = 0.25; this.eyeR = 0.25; this.sp = 50; this.spl = 1.4; this.op = 1; this.danger = false; this.nuked = false; } var enemies = []; // for (var i = 0; i < 10; i++) { // enemies[i] = new Enemy(); // } enemies.push(new Enemy()); enemies.push(new Enemy()); enemies.push(new Enemy()); var entm = 0; var ga = 0; var steptime = 0; var Star = function() { this.a = Math.random()*Math.PI*2; this.v = 3+Math.random()*5; this.x = stage.w/2; this.y = stage.h/2; this.r = 0.2; } var Power = function() { this.type = Math.floor(Math.random()*2)+1; this.a = Math.random()*Math.PI*2; this.v = 3+Math.random()*5; this.x = stage.w/2; this.y = stage.h/2; this.r = 0.2; this.dis = false; this.op = 1; } var powers = []; var powertm = 0; var powermax = Math.random()*800+300; // powermax = 10; var stars = []; for (var i = 0; i < 200; i++) { stars[i] = new Star(); var st = stars[i]; var move = Math.random()*400; st.x += Math.sin(st.a)*move; st.y += Math.cos(st.a)*move; } // powers.push(new Power()); function enginestep() { steptime = Date.now(); ctx.clearRect(0, 0, stage.w, stage.h); ctx.fillStyle = "#ffffff"; for (var i = 0; i < stars.length; i++) { var st = stars[i]; st.x += Math.sin(st.a)*st.v; st.y += Math.cos(st.a)*st.v; st.r += st.v/200; ctx.beginPath(); ctx.arc(st.x, st.y, st.r, 2*Math.PI, 0); ctx.fill(); if (st.x > stage.w || st.x < 0 || st.y < 0 || st.y > stage.h) { stars[i] = new Star(); } } if (!gameover) { danger = false; powertm++; if (powertm > powermax) { powers.push(new Power()); powertm = 0; powermax = Math.random()*1200+600; // powermax = 10; } .........完整代码请登录后点击上方下载按钮下载查看
网友评论0