var twit = new Array(); var twit_count = 0; var canvas, ctx; var particles = []; var palyer; var enemys = []; var bullets = []; var NUM_ENEMYS = 5; var NUM_PARTICLES = 20; var NUM_BULLETS = 3; var mouse_X; var mouse_Y; var mouse_CLICK; var scr = 0; var gameover = 0; var img = new Image(); //img.src = "player.gif"; //img.src = "twitter.gif"; //img.src = ""; img.src = "chara.gif"; //var img2 = new Image(); //img2.src = "enemy.gif"; var img2 = new Image(); img2.src = "back.gif"; var img3 = new Image(); img3.src = "hit.gif"; var img4 = new Image(); img4.src = "tikyuu.gif"; function Player(){ this.x = 10; this.y = canvas.height / 2; this.y_speed = 0; } function Enemy() { this.x = canvas.width; this.y = Math.random() * canvas.height; this.xvel = -Math.random() * 5; this.xvel = this.xvel - 5; this.yvel = 0; this.type = Math.round(Math.random() * 2 + 0.5); this.hp = 1; this.flg = 0; } function Bullet() { this.x = 0; this.y = 0; this.flg = 0; this.xvel = 5; this.twit = -1; } function Particle() { this.x = Math.random() * canvas.width; this.y = Math.random() * canvas.height; this.size = Math.random() * 5; this.color = Math.random() * 255; this.xvel = -Math.random() * 5 ; this.yvel = Math.random() * 5 - 2.5; this.type = Math.round(Math.random() * 7 + 0.5); } // 各パーツの更新 Player.prototype.update = function() { if(mouse_Y > this.y) { this.y_speed = this.y_speed + 1; }else if(mouse_Y < this.y) { this.y_speed = this.y_speed - 1; } if(this.y_speed > 8){ this.y_speed = 8; }else if(this.y_speed < -8) { this.y_speed = -8; } this.y = this.y + this.y_speed; } Enemy.prototype.update = function() { this.x += this.xvel; this.y += this.yvel; if (this.x < -160 || this.y > canvas.height){ this.x = canvas.width -1; this.y = Math.random() * canvas.height; this.yvel = 0; this.flg = 0; } } Bullet.prototype.update = function() { this.x += this.xvel; if (this.x > canvas.width){ this.x = -30; this.y = Math.random() * canvas.height; this.flg = 0; } } Particle.prototype.update = function() { this.x += this.xvel; if (this.x < -48){ this.x = canvas.width -1; this.y = Math.random() * canvas.height; } if (this.y > canvas.height || this.y < 0) { this.yvel = -this.yvel; } } // メインの更新 function loop() { scr = scr + 1; document.getElementById('debug1').innerHTML = "Score: "+ scr; ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.beginPath(); var grad = ctx.createLinearGradient(200,0, 140,140); grad.addColorStop(0,'rgb(192, 120, 137)'); // 赤 grad.addColorStop(0.3,'rgb(165, 207, 129)'); // 緑 grad.addColorStop(0.7,'rgb(128, 128, 182)'); // 紫 grad.addColorStop(1,'rgb(245,245,255)'); ctx.fillStyle = grad; ctx.rect(0,0, canvas.width,canvas.height); ctx.fill(); for(var i = 0; i < NUM_PARTICLES; i++) { particles[i].update(); if(particles[i].type < 4) { ctx.drawImage(img2,80+(particles[i].type * 48),0,48,48,particles[i].x,particles[i].y,48,48); }else { ctx.beginPath(); ctx.strokeStyle = "rgb(255, 255, 255)"; ctx.lineWidth = particles[i].size; ctx.moveTo(particles[i].x, particles[i].y); ctx.lineTo(particles[i].x - particles[i].xvel, particles[i].y - particles[i].yvel); ctx.stroke(); ctx.closePath(); } } player.update(); ctx.drawImage(img,0,0,144,96,player.x,player.y,144,96); for(var i = 0; i < NUM_ENEMYS; i++) { enemys[i].update(); ctx.drawImage(img,0,32+(enemys[i].type * 64),160,64,enemys[i].x-16,enemys[i].y-16,160,64); if( enemys[i].x < player.x+100 && enemys[i].x+120 > player.x && enemys[i].y < player.y+60 && enemys[i].y+40 > player.y) { //ctx.drawImage(img3,player.x-20,player.y-15); ctx.drawImage(img3,0,0,30,31,player.x-20,player.y-15,160,160); gameover = 1; } //弾と敵との当たり判定 for(var j = 0; j < NUM_BULLETS; j++){ if(bullets[j].flg == 1) { if( enemys[i].x < bullets[j].x+140 && enemys[i].x+120 > bullets[j].x && enemys[i].y < bullets[j].y+20 && enemys[i].y+50 > bullets[j].y) { scr += 50; //ctx.drawImage(img,96,0,224,80,player.x,player.y,224,80); ctx.drawImage(img3,0,0,30,31,enemys[i].x-30,enemys[i].y-30,160,160); enemys[i].flg = 0; enemys[i].yvel = 10; bullets[j].flg = 0; } } } } for(var j = 0; j < NUM_BULLETS; j++){ if(mouse_CLICK == 1 && bullets[j].flg == 0) { bullets[j].flg = 1; var twit_rand = Math.round(Math.random() * twit_count + 0.5); bullets[j].twit = twit_rand; bullets[j].x = player.x+100; bullets[j].y = player.y+10; mouse_CLICK = 0; } if(bullets[j].flg == 1) { bullets[j].update(); ctx.drawImage(img,0,224,160,32,bullets[j].x,bullets[j].y-20,160,32); /* //ctx.shadowBlur = 10; //ctx.shadowColor = "#ffff00"; ctx.beginPath(); ctx.strokeStyle = "rgb(255, 255, 0)"; ctx.lineWidth = 3; ctx.moveTo(bullets[j].x, bullets[j].y); ctx.lineTo(bullets[j].x - 10, bullets[j].y); ctx.stroke(); ctx.closePath(); */ /* フォントスタイルを定義 */ ctx.font = "16px 'MS Pゴシック'"; //ctx.strokeStyle = "rgb(100,100,100)"; ctx.fillStyle = "rgb(100,100,100)"; ctx.fillText(twit[bullets[j].twit], bullets[j].x, bullets[j].y); //var twit_rand = Math.round(Math.random() * twit_count + 0.5); //ctx.strokeText(twit[bullets[j].twit], bullets[j].x, bullets[j].y); } } if(gameover == 0) { setTimeout(loop, 30); }else { ctx.font = "60px 'MS Pゴシック'"; ctx.strokeStyle = "#ff0000"; ctx.fillStyle = "#990000"; ctx.fillText("gamE oveR", canvas.width / 2 -140, canvas.height /2 -40); ctx.strokeText("gamE oveR", canvas.width / 2 -140, canvas.height /2 -40,1); ctx.font = "60px 'MS Pゴシック'"; ctx.strokeStyle = "#afffff"; ctx.fillStyle = "#7eeeee"; ctx.fillText("scorE "+scr, canvas.width / 2 -120, canvas.height /2 ); ctx.strokeText("scorE "+scr, canvas.width / 2 -120, canvas.height /2,1); } } //初期ロード function load() { canvas = document.getElementById("cv"); ctx = canvas.getContext("2d"); player = new Player(); for(var i = 0; i < NUM_ENEMYS; i++) { enemys[i] = new Enemy(); } for(var i = 0; i < NUM_PARTICLES; i++) { particles[i] = new Particle(); } for(var i = 0; i < NUM_BULLETS; i++){ bullets[i] = new Bullet(); } loop(); } function mouseClicked() { mouse_CLICK = 1; } function mouseUp() { } function mouseMove(x,y) { mouse_X = x; mouse_Y = y-80; document.getElementById('debug2').innerHTML = "MousePos: x="+mouse_X + " y="+mouse_Y; } window.onload = function(){ load(); }