在线小游戏,不知能不能做成原生页面

上网、浏览、聊天、下载等
回复
头像
chengqia
帖子: 22
注册时间: 2010-02-15 11:34
来自: 南京市
联系:

在线小游戏,不知能不能做成原生页面

#1

帖子 chengqia » 2012-11-18 15:45

代码: 全选

// Setup requestAnimationFrame
requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||  
                        window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;

// Create the canvas
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = 1250;
canvas.height = 580;
document.body.appendChild(canvas);

// Background image
var bgReady = false;
var bgImage = new Image();
bgImage.onload = function () {
	bgReady = true;
};
bgImage.src = "img/background.png";

// Hero image
var heroReady = false;
var heroImage = new Image();
heroImage.onload = function () {
	heroReady = true;
};
heroImage.src = "img/hero.png";

// Monster image
var monsterReady = false;
var monsterImage = new Image();
monsterImage.onload = function () {
	monsterReady = true;
};
//var hostReady = false;
//var hostImage = new Image()
//hostImage.onload =function (){
//	hostReady =true;
//};

monsterImage.src = "img/monster.png";
//hostImage.src = "img/host.png
// Game objects
var hero = {
	speed: 512 // movement in pixels per second
};
var monster = {
	speed: 256};
//var host = {
//	speed: 1
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 = 550 + (Math.random() * (canvas.width - 64));
	monster.y = 50 + (Math.random() * (canvas.height - 64));
};

// Update game objects

var update = function (modifier) {
//	host.x -=hoster.speed * modifier;
	monster.x -=monster.speed * modifier;
	if (monster.x < 0){
		alert ("Game is over!")};
//	monster.y += monster.speed * 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;
	}

	// 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);
//	if (hostReady) {
//		ctx.drawImage(hostImage, host.x, host.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;
	requestAnimationFrame(main);
};

// Let's play this game!
	hero.x = canvas.width / 2;
	hero.y = canvas.height / 2;
reset();
var then = Date.now();
main();
头像
chengqia
帖子: 22
注册时间: 2010-02-15 11:34
来自: 南京市
联系:

Re: 在线小游戏,不知能不能做成原生页面

#2

帖子 chengqia » 2012-11-18 15:48

:em21 :em21 :em21 泪奔了,居然是这样,而且包含不了图片
回复