html5 canvas 英雄打怪小遊戲
具體參考:
http://www.lostdecadegames.com/how-to-make-a-simple-html5-canvas-game/
// Create the canvas
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = 512;
canvas.height = 480;
document.body.appendChild(canvas);
// Background image
var bgReady = false;
var bgImage = new Image();
bgImage.onload = function () {
bgReady = true;
};
bgImage.src = "images/background.png";
// Hero image
var heroReady = false;
var heroImage = new Image();
heroImage.onload = function () {
heroReady = true;
};
heroImage.src = "images/hero.png";
// Monster image
var monsterReady = false;
var monsterImage = new Image();
monsterImage.onload = function () {
monsterReady = true;
};
monsterImage.src = "images/monster.png";
// Game objects
var hero = {
speed: 256 // movement in pixels per second
};
var monster = {
speed: 128 // we let monster go.
};
var monstersCaught = 0;
// Handle keyboard controls
var keysDown = {};
addEventListener("keydown", function (e) {
keysDown[e.keyCode] = true;
}, false);
addEventListener("keyup", function (e) {
delete keysDown[e.keyCode];
}, false);
// Reset the game when the player catches a monster
var reset = function () {
hero.x = canvas.width / 2;
hero.y = canvas.height / 2;
// Throw the monster somewhere on the screen randomly
monster.x = 32 + (Math.random() * (canvas.width - 64));
monster.y = 32 + (Math.random() * (canvas.height - 64));
};
var flagx = 1;
var flagy = 1;
// Update game objects
var update = function (modifier) {
if (38 in keysDown) { // Player holding up
hero.y -= hero.speed * modifier;
}
if (40 in keysDown) { // Player holding down
hero.y += hero.speed * modifier;
}
if (37 in keysDown) { // Player holding left
hero.x -= hero.speed * modifier;
}
if (39 in keysDown) { // Player holding right
hero.x += hero.speed * modifier;
}
//Here we let the monster go
if(monster.x <= 0){
flagx = 1;
}else if(monster.x >= canvas.width){
flagx = -1;
}
if(monster.y <= 0){
flagy = 1;
}else if(monster.y >= canvas.height){
flagy = -1;
}
monster.x = monster.x + monster.speed * modifier * flagx;
monster.y = monster.y + monster.speed * modifier * flagy;
// Are they touching?
if (
hero.x <= (monster.x + 32)
&& monster.x <= (hero.x + 32)
&& hero.y <= (monster.y + 32)
&& monster.y <= (hero.y + 32)
) {
++monstersCaught;
reset();
}
};
// Draw everything
var render = function () {
if (bgReady) {
ctx.drawImage(bgImage, 0, 0);
}
if (heroReady) {
ctx.drawImage(heroImage, hero.x, hero.y);
}
if (monsterReady) {
ctx.drawImage(monsterImage, monster.x, monster.y);
}
// Score
ctx.fillStyle = "rgb(250, 250, 250)";
ctx.font = "24px Helvetica";
ctx.textAlign = "left";
ctx.textBaseline = "top";
ctx.fillText("Goblins caught: " + monstersCaught, 32, 32);
};
// The main game loop
var main = function () {
var now = Date.now();
var delta = now - then;
update(delta / 1000);
render();
then = now;
};
// Let's play this game!
reset();
var then = Date.now();
setInterval(main, 1); // Execute as fast as possible
相關文章
- 用C++語言寫遊戲——打怪小遊戲C++遊戲
- HTML5 -- CanvasHTMLCanvas
- HTML5的CanvasHTMLCanvas
- canvas手寫辨色力小遊戲Canvas遊戲
- canvas吃豆小遊戲程式碼Canvas遊戲
- canvas小遊戲程式碼例項Canvas遊戲
- HTML5(五)——Canvas APIHTMLCanvasAPI
- HTML5 Canvas 詳解HTMLCanvas
- html5畫布canvasHTMLCanvas
- html5 canvas分層HTMLCanvas
- html5小遊戲(-)待續HTML遊戲
- HTML5系列之canvas用法HTMLCanvas
- HTML5 Canvas API詳解HTMLCanvasAPI
- HTML5的canvas標籤HTMLCanvas
- Canvas drag 實現拖拽拼圖小遊戲Canvas遊戲
- Html5 canvas創意特效合集HTMLCanvas特效
- 淺析HTML5的Canvas——1HTMLCanvas
- html5中canvas繪製矩形HTMLCanvas
- HTML5 canvas圖片爆炸特效HTMLCanvas特效
- HTML5 快速學習二 CanvasHTMLCanvas
- canvas繪製“飛機大戰”小遊戲,真香!Canvas遊戲
- js+canvas仿微信《彈一彈》小遊戲JSCanvas遊戲
- 英雄聯盟官方小遊戲登陸微信 爆款IP的小遊戲該如何做?遊戲
- 打怪獸
- 【黑科技】React-canvas助力HTML5ReactCanvasHTML
- HTML5的SVG和Canvas的使用HTMLSVGCanvas
- html5 canvas學習--繪製文字HTMLCanvas
- HTML5 SVG與Canvas的區別HTMLSVGCanvas
- html5中canvas繪製圓形HTMLCanvas
- html5中canvas繪製線段HTMLCanvas
- HTML5使用canvas繪製圖形HTMLCanvas
- 基於 Canvas 的 HTML5 文字動畫特效CanvasHTML動畫特效
- 用HTML5的canvas畫太陽系HTMLCanvas
- Html5 Canvas動畫基礎(碰撞檢測)HTMLCanvas動畫
- html5 canvas學習--操作與使用影象HTMLCanvas
- html5中canvas線段繪製太陽花HTMLCanvas
- html5中的canvas線性漸變HTMLCanvas
- 使用 HTML5 Canvas 繪製的水滴效果HTMLCanvas