如何在javascript游戏中添加重播功能
我很难为我的代码添加play-reach函数,我不知道我做错了什么。有什么帮助吗????似乎在玩家死后我不会更新我的游戏区域 此外,在显示器中使用图像而不是文本后,我的分数不再显示。如何在代码中显示图像时添加分数如何在javascript游戏中添加重播功能,javascript,html,2d-games,Javascript,Html,2d Games,我很难为我的代码添加play-reach函数,我不知道我做错了什么。有什么帮助吗????似乎在玩家死后我不会更新我的游戏区域 此外,在显示器中使用图像而不是文本后,我的分数不再显示。如何在代码中显示图像时添加分数 <!DOCTYPE html> <html> <head> <style> canvas { border:1px solid #d3d3d3; background-image: url("http://i.stac
<!DOCTYPE html>
<html>
<head>
<style>
canvas {
border:1px solid #d3d3d3;
background-image: url("http://i.stack.imgur.com/WHu9Z.png");
}
</style>
</head>
<body>
<script>
var myGamePiece;
var myObstacles = [];
var myScore;
function startGame() {
myGamePiece = new component(40, 40, "http://orig15.deviantart.net/a436/f/2012/101/1/c/mario_sprite_by_flamingdragon5000-d4vt57a.png", 10, 120, "image");
myGamePiece.gravity = 0.05;
myScore = new component("30px", "Consolas", "black", 280, 40, "text");
myGameArea.start();
}
var myGameArea = {
canvas : document.createElement("canvas"),
start : function() {
this.canvas.width = 480;
this.canvas.height = 350;
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);
}
}
function component(width, height, color, x, y, type) {
this.type = type;
if (type == "image") {
this.image = new Image();
this.image.src = color;
}
this.score = 0;
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.gravity = 0;
this.gravitySpeed = 0;
this.update = function() {
ctx = myGameArea.context;
if (this.type == "image") {
ctx.drawImage(this.image,
this.x,
this.y,
this.width, this.height);
} else {
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
this.newPos = function() {
this.gravitySpeed += this.gravity;
this.x += this.speedX;
this.y += this.speedY + this.gravitySpeed;
this.hitBottom();
}
this.hitBottom = function() {
var rockbottom = myGameArea.canvas.height - this.height -23;
if (this.y > rockbottom) {
this.y = rockbottom;
this.gravitySpeed = 0;
}
}
this.crashWith = function(otherobj) {
var myleft = this.x;
var myright = this.x + (this.width);
var mytop = this.y;
var mybottom = this.y + (this.height);
var otherleft = otherobj.x;
var otherright = otherobj.x + (otherobj.width);
var othertop = otherobj.y;
var otherbottom = otherobj.y + (otherobj.height);
var crash = true;
if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
crash = false;
}
return crash;
}
}
function updateGameArea() {
var x, height, gap, minHeight, maxHeight, minGap, maxGap;
for (i = 0; i < myObstacles.length; i += 1) {
if (myGamePiece.crashWith(myObstacles[i])) {
return;
}
}
myGameArea.clear();
myGameArea.frameNo += 1;
if (myGameArea.frameNo == 1 || everyinterval(150)) {
x = myGameArea.canvas.width;
minHeight = 20;
maxHeight = 200;
height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
minGap = 90;
maxGap = 200;
gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
myObstacles.push(new component(50, height, "green", x, 0));
myObstacles.push(new component(50, x - height - gap, "green", x, height + gap));
}
for (i = 0; i < myObstacles.length; i += 1) {
myObstacles[i].x += -1;
myObstacles[i].update();
}
myScore.text="SCORE: " + myGameArea.frameNo;
myScore.update();
myGamePiece.newPos();
myGamePiece.update();
}
function everyinterval(n) {
if ((myGameArea.frameNo / n) % 1 == 0) {return true;}
return false;
}
function accelerate(n) {
myGamePiece.gravity = n;
}
function clickPlayagain() {
myGameArea.stop();
// myGameArea2.stop();
startGame();
}
startGame();
</script>
<br>
<button onmousedown="accelerate(-0.15)" onmouseup="accelerate(0.08)">ACCELERATE</button>
<button id="playagain" onclick="clickPlayagain()">Play again</button>
<p>Use the ACCELERATE button to stay in the air</p>
</body>
</html>
帆布{
边框:1px实心#D3;
背景图像:url(“http://i.stack.imgur.com/WHu9Z.png");
}
我的配子;
var myobstackes=[];
var myScore;
函数startName(){
myGamePiece=新组件(40,40,“http://orig15.deviantart.net/a436/f/2012/101/1/c/mario_sprite_by_flamingdragon5000-d4vt57a.png,10120,“图像”);
myGamePiece.gravity=0.05;
myScore=新组件(“30px”、“控制台”、“黑色”、280、40、“文本”);
myGameArea.start();
}
var myGameArea={
画布:document.createElement(“画布”),
开始:函数(){
this.canvas.width=480;
this.canvas.height=350;
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);
}
}
功能组件(宽度、高度、颜色、x、y、类型){
this.type=type;
如果(类型==“图像”){
this.image=新图像();
this.image.src=颜色;
}
这个分数=0;
这个。宽度=宽度;
高度=高度;
这是0.speedX=0;
该值为0;
这个.x=x;
这个。y=y;
这个重力=0;
这个重力速度=0;
this.update=函数(){
ctx=myGameArea.context;
if(this.type==“image”){
ctx.drawImage(此.image,
这个,x,,
这个,嗯,,
这个。宽度,这个。高度);
}否则{
ctx.fillStyle=颜色;
ctx.fillRect(this.x,this.y,this.width,this.height);
}
}
this.newPos=函数(){
this.gravitySpeed+=this.gravity;
this.x+=this.speedX;
这个.y+=这个.speedY+这个.gravitySpeed;
这个;
}
this.hitbooth=函数(){
var rockbottom=myGameArea.canvas.height-this.height-23;
如果(this.y>rockbottom){
y=最低点;
这个重力速度=0;
}
}
this.crashWith=函数(otherobj){
var myleft=this.x;
var myright=this.x+(this.width);
var mytop=this.y;
var mybottom=this.y+(this.height);
var otherleft=otherobj.x;
var otherright=otherobj.x+(otherobj.width);
var othertop=otherobj.y;
var otherbottom=otherobj.y+(otherobj.height);
var-crash=true;
如果((mybottomotherbottom)| |(myrightotherright)){
崩溃=错误;
}
返回碰撞;
}
}
函数updateGameArea(){
变量x,高度,间隙,最小高度,最大高度,最小间隙,最大间隙;
对于(i=0;i
加快
再玩一次
使用加速按钮保持在空中
希望它能帮助:
<!DOCTYPE html>
<html>
<head>
<style>
canvas {
border:1px solid #d3d3d3;
background-image: url("http://i.stack.imgur.com/WHu9Z.png");
}
</style>
</head>
<body>
<script>
var myGamePiece;
var myObstacles = [];
var myScore;
var myGameArea;
function startGame() {
myGamePiece = new component(40, 40, "http://orig15.deviantart.net/a436/f/2012/101/1/c/mario_sprite_by_flamingdragon5000-d4vt57a.png", 10, 120, "image");
myGamePiece.gravity = 0.05;
myScore = new component("30px", "Consolas", "black", 280, 40, "text");
if(myGameArea){
document.removeChild(myGameArea.canvas);
}
myGameArea = {
canvas : document.createElement("canvas"),
start : function() {
this.canvas.width = 480;
this.canvas.height = 350;
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);
}
}
myGameArea.start();
}
function reset(){
myGamePiece = undefined;
myObstacles = [];
myScore = "";
}
function component(width, height, color, x, y, type) {
this.type = type;
if (type == "image") {
this.image = new Image();
this.image.src = color;
}
this.score = 0;
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.gravity = 0;
this.gravitySpeed = 0;
this.update = function() {
ctx = myGameArea.context;
if (this.type == "image") {
ctx.drawImage(this.image,
this.x,
this.y,
this.width, this.height);
}
if (this.type == "text") {
ctx.font = this.width + " " + this.height;
ctx.fillStyle = color;
ctx.fillText(this.text, this.x, this.y);
}
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
this.newPos = function() {
this.gravitySpeed += this.gravity;
this.x += this.speedX;
this.y += this.speedY + this.gravitySpeed;
this.hitBottom();
}
this.hitBottom = function() {
var rockbottom = myGameArea.canvas.height - this.height -23;
if (this.y > rockbottom) {
this.y = rockbottom;
this.gravitySpeed = 0;
}
}
this.crashWith = function(otherobj) {
var myleft = this.x;
var myright = this.x + (this.width);
var mytop = this.y;
var mybottom = this.y + (this.height);
var otherleft = otherobj.x;
var otherright = otherobj.x + (otherobj.width);
var othertop = otherobj.y;
var otherbottom = otherobj.y + (otherobj.height);
var crash = true;
if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
crash = false;
}
return crash;
}
}
function updateGameArea() {
var x, height, gap, minHeight, maxHeight, minGap, maxGap;
for (i = 0; i < myObstacles.length; i += 1) {
if (myGamePiece.crashWith(myObstacles[i])) {
return;
}
}
myGameArea.clear();
myGameArea.frameNo += 1;
if (myGameArea.frameNo == 1 || everyinterval(150)) {
x = myGameArea.canvas.width;
minHeight = 20;
maxHeight = 200;
height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
minGap = 90;
maxGap = 200;
gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
myObstacles.push(new component(50, height, "green", x, 0));
myObstacles.push(new component(50, x - height - gap, "green", x, height + gap));
}
for (i = 0; i < myObstacles.length; i += 1) {
myObstacles[i].x += -1;
myObstacles[i].update();
}
myScore.text="SCORE: " + myGameArea.frameNo;
myScore.update();
myGamePiece.newPos();
myGamePiece.update();
}
function everyinterval(n) {
if ((myGameArea.frameNo / n) % 1 == 0) {return true;}
return false;
}
function accelerate(n) {
myGamePiece.gravity = n;
}
function clickPlayagain() {
reset();
startGame();
}
startGame();
</script>
<br>
<button onmousedown="accelerate(-0.15)" onmouseup="accelerate(0.08)">ACCELERATE</button>
<button id="playagain" onclick="clickPlayagain()">Play again</button>
<p>Use the ACCELERATE button to stay in the air</p>
</body>
</html>
帆布{
边框:1px实心#D3;
背景图像:url(“http://i.stack.imgur.com/WHu9Z.png");
}
我的配子;
var myobstackes=[];
var myScore;
myGameArea;
函数startName(){
myGamePiece=新组件(40,40,“http://orig15.deviantart.net/a436/f/2012/101/1/c/mario_sprite_by_flamingdragon5000-d4vt57a.png,10120,“图像”);
myGamePiece.gravity=0.05;
myScore=新组件(“30px”、“控制台”、“黑色”、280、40、“文本”);
if(我的游戏区){
document.removeChild(myGameArea.canvas);
}
myGameArea={
画布:document.createElement(“画布”),
开始:函数(){
this.canvas.width=480;
this.canvas.height=350;
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);
}
}
myGameArea.start();
}
函数重置(){
myGamePiece=未定义;
肌障碍=[];
myScore=“”;
}
功能组件(宽度、高度、颜色、x、y、类型){
this.type=type;
如果(类型==“图像”){
this.image=新图像();
this.image.src=颜色;
}
这个分数=0;
这个。宽度=宽度;
高度=高度;
这是0.speedX=0;
此参数为0