Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将JavaScript游戏转换为HTML_Javascript_Html - Fatal编程技术网

将JavaScript游戏转换为HTML

将JavaScript游戏转换为HTML,javascript,html,Javascript,Html,我一直在尝试将JavaScript游戏转换为HTML,我尝试的任何东西都不会在HTML中显示我的对象。这是我的JavaScript代码 /*var myGameArea = { canvas : document.createElement("canvas"), start : function() { this.canvas.width = 480; this.canvas.height = 270; this.context =

我一直在尝试将JavaScript游戏转换为HTML,我尝试的任何东西都不会在HTML中显示我的对象。这是我的JavaScript代码

/*var myGameArea = {
    canvas : document.createElement("canvas"),
    start : function() {
        this.canvas.width = 480;
        this.canvas.height = 270;
        this.context = this.canvas.getContext("2d");
        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
        this.frameNo = 0;
        this.interval = setInterval(updateGameArea, 20);
        },
    clear : function() {
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
    }
}*/

var Beaver = function(x, y) {
    this.x = x;
    this.y = y;
   this.img = getImage("https://www.kasandbox.org/third_party/javascript-khansrc/live-editor/build/images/creatures/Hopper-Happy.png");
    this.sticks = 0;
};



//The Beaver Drawing Prototipe Function
Beaver.prototype.draw = function() {
   ctx = myGameArea.context;
    ctx.fill(255, 0, 0);
    this.y = constrain(this.y,  0, height - 50);
  image(this.img, this.x, this.y, 40, 40);
};

// Define Hop and Fall Functions
Beaver.prototype.hop = function () {
    this.img = getImage("https://www.kasandbox.org/third_party/javascript-khansrc/live-editor/build/images/creatures/Hopper-Jumping.png");
    this.y -= 5;
};

Beaver.prototype.fall = function () {
    this.img = getImage("https://www.kasandbox.org/third_party/javascript-khansrc/live-editor/build/images/creatures/Hopper-Happy.png");
    this.y += 5;
};

//Stick Collecting Test Function
/* Beaver.prototype.checkForStickGrab = function(stick) {
    //Beaver shoud colide stick if:
        //the stick's center x position is between the two sides of beaver 
        if ((stick.x >= this.x && stick.x <= (this.x + 40)) && 

        //the stick's center y position is between te top and the bottom of beaver
            (stick.y >= this.y && stick.y <= (this.x + 40 ))) {
                stick.y = -400;
                this.sticks++;
        }
}; */ 

Beaver.prototype.checkForStickGrab = function(stick) {
    if ((stick.x >= this.x && stick.x <= (this.x + 40)) &&
        (stick.y >= this.y && stick.y <= (this.y + 40))) {
        stick.y = -400;
        this.sticks++;
    }
};

//creating the stick oject 
var Stick = function (x, y) {
    this.x = x;
    this.y = y;
};

//stick drawing function
Stick.prototype.draw = function() {
    fill(112, 81, 48);
    //switch to a mode where the rect is drawn from the center and not in the upper left corner of coortinates x and y
    rectMode(CENTER);
    rect(this.x, this.y, 5, 40);
};

//Inserting the Character into the Environment
var beaver = new Beaver(200, 300);

//init sticks
var sticks = [];
for (var i = 0; i < 40; i++) {
    sticks.push(new Stick(i * 40 +300, random(20, 260)));
}



//Initializing initial position of the grass blocks 
var grassXs = [];
for (var i = 0; i < 25; i++) {
    grassXs.push(i*20);
}
//Forest Environment
draw = function() {
  //ctx = myGameArea.context;

    //draws the sky
    background(0, 115, 255);

   ill(85, 255, 0);
   text("Score: " +beaver.sticks, 20, 20);
   if (beaver.sticks/sticks.length >= 0.90) {
       text("YOU WIN!!!", width/2, height/2);
       noLoop(); 

   } 

    //draws the ground
    fill(130, 79, 43);
    rectMode(CORNER);
    rect(-1, height * 0.90, width + 1, 49);


    //draw the grass
    for (var i = 0; i < grassXs.length; i++) {
        image(getImage("https://www.kasandbox.org/third_party/javascript-khansrc/live-editor/build/images/cute/GrassBlock.png"), grassXs[i], height*0.85, 20, 40);
        grassXs[i] -= 1;
        if (grassXs[i] <= -20) {
            grassXs[i] = width;
        }
    }

      for (var i = 0; i < sticks.length; i++) {
        sticks[i].draw();
        beaver.checkForStickGrab(sticks[i]);
        sticks[i].x -= 1;
    } 
     //User interaction
    if (keyIsPressed && keyCode === 0) {
        beaver.hop();
    } else {
        beaver.fall();
    }
  //ctx = myGameArea.context;
  beaver.draw();
};
/*var myGameArea={
画布:document.createElement(“画布”),
开始:函数(){
this.canvas.width=480;
this.canvas.height=270;
this.context=this.canvas.getContext(“2d”);
document.body.insertBefore(this.canvas,document.body.childNodes[0]);
这个.frameNo=0;
this.interval=setInterval(updateGameArea,20);
},
清除:函数(){
this.context.clearRect(0,0,this.canvas.width,this.canvas.height);
}
}*/
var Beaver=函数(x,y){
这个.x=x;
这个。y=y;
this.img=getImage(“https://www.kasandbox.org/third_party/javascript-khansrc/live-editor/build/images/creatures/Hopper-Happy.png");
这是0;
};
//海狸绘图协议函数
Beaver.prototype.draw=函数(){
ctx=myGameArea.context;
ctx.fill(255,0,0);
this.y=约束(this.y,0,高度-50);
图像(this.img,this.x,this.y,40,40);
};
//定义跳跃和下降函数
Beaver.prototype.hop=函数(){
this.img=getImage(“https://www.kasandbox.org/third_party/javascript-khansrc/live-editor/build/images/creatures/Hopper-Jumping.png");
这个.y-=5;
};
Beaver.prototype.fall=函数(){
this.img=getImage(“https://www.kasandbox.org/third_party/javascript-khansrc/live-editor/build/images/creatures/Hopper-Happy.png");
这个.y+=5;
};
//集棒测试功能
/*Beaver.prototype.checkForStickGrab=函数(斗杆){
//在下列情况下,海狸应使用粘胶:
//斗杆的中心x位置在海狸的两侧之间

如果((stick.x>=this.x&&stick.x我看到您正在通过Khan Academy沙盒使用Processing.js。为了在HTML中实现这一点,您需要包含库

您可以按照此程序上的说明逐步查看说明及其工作原理


您是否将此脚本作为单独的.js文件包含到html中?如果您将此脚本作为html文件写入,您将它包含在哪里?或者?我将它作为单独的js文件使用。但是,如果嵌入到我的html中,我想我必须将其包含在标记中,不是吗?是的,正确。请检查您是否遗漏了指向depen的任何链接你也可以发布你的html吗?我的html只是一个画布,在这里:虽然我用JavaScript创建了一些元素