2017-07-28 116 views
1

我正在寫簡單的「蛇」遊戲,我面臨着這個問題: 每次馴服我的蛇打紅圈(蘋果),蘋果應該移動到一個新的位置畫布。現在出現了新的蘋果,但舊的蘋果不會消失(它應該),並且當畫布上有兩個以上的蘋果時,它們會創建一個填充圖形......它看起來像這樣:ibb.co/nrYdLQ(也不應該發生)。JavaScript - 將對象移動到新位置

負責移動一個蘋果的代碼是這樣的:

if (!this.objectCollide(myApple)) { 
    this.segments.pop(); 
    } else { 
    myApple = new block(Math.floor(Math.random() * gameField.width),Math.floor(Math.random() * gameField.height)) 
    }; 

,我不知道爲什麼它的工作就像我上面描述,而不是隻移動一個蘋果到新的位置,並刪除舊的。 請幫忙。

的jsfiddle:https://jsfiddle.net/e1ga0fpm/

完整的JavaScript代碼:

var gameField = document.getElementById('gameField'); 
var ctx = gameField.getContext("2d"); 
var blockSize = 10; 
columnCt = gameField.width/blockSize; 
rowsCt = gameField.height/blockSize; 

var block = function(x, y) { 
    this.x = x; 
    this.y = y; 
} 

block.prototype.drawBlock = function() { 
    ctx.fillStyle = "blue"; 
    ctx.fillRect(this.x * blockSize, this.y * blockSize, blockSize, 
    blockSize); 
}; 

block.prototype.drawApple = function() { 
    ctx.fillStyle = "red"; 
    ctx.textBaseline = "bottom"; 
    ctx.arc(this.x, this.y, 6, 2 * Math.PI, false); 
    ctx.fill(); 
} 

var Snake = function() { 
    this.segments = [new block(20, 20), new block(19, 20), new block(18, 20), new block(17, 20), 
    new block(16, 20), new block(15, 20), new block(14, 20), new block(13, 20), new block(12, 20), 
    new block(11, 20), new block(10, 20) 
    ]; 
    this.direction = "right"; 
} 

Snake.prototype.drawSnake = function() { 
    for (i = 0; i < this.segments.length; i++) { 
    this.segments[i].drawBlock(); 
    } 
} 

Snake.prototype.setDirection = function(dir) { 
    if (this.direction == "left" && dir == "right" || this.direction == "right" && dir == "left" || this.direction == "up" && dir == "down" || 
    this.direction == "down" && dir == "up") { 
    return 
    } else { 
    this.direction = dir; 
    }; 
}; 

Snake.prototype.objectCollide = function(obj) { 
    if (this.segments[0].x == Math.round(obj.x/blockSize) && this.segments[0].y == Math.round(obj.y/blockSize)) { 
    return true 
    } else { 
    return false 
    } 
}; 

Snake.prototype.move = function() { 
    var head = this.segments[0]; 
    var newHead; 

    switch (this.direction) { 
    case "right": 
     newHead = new block(head.x + 1, head.y); 
     break; 
    case "left": 
     newHead = new block(head.x - 1, head.y) 
     break; 
    case "down": 
     newHead = new block(head.x, head.y + 1) 
     break; 
    case "up": 
     newHead = new block(head.x, head.y - 1) 
     break; 
    } 

    this.segments.unshift(newHead); 

    if (!this.objectCollide(myApple)) { 
    this.segments.pop(); 
    } else { 
    myApple = new block(Math.floor(Math.random() * gameField.width),Math.floor(Math.random() * gameField.height)) 
    }; 
    var collision = newHead.x >= columnCt || newHead.x <= -1 || 
    newHead.y >= rowsCt || newHead.y <= -1; 

    for (i = 1; i < this.segments.length; i++) { 
    if (this.segments[i].x == newHead.x && this.segments[i].y == newHead.y) { 
     collision = true; 
     break; 
    }; 
    }; 

    if (collision) { 
    clearInterval(myFun); 
    }; 

}; 

var mySnake = new Snake() 
mySnake.drawSnake(); 
var myApple = new block(Math.floor(Math.random() * gameField.width), 
    Math.floor(Math.random() * gameField.height)); 
var myFun = setInterval(function() { 
    ctx.clearRect(0, 0, gameField.width, gameField.height); 
    mySnake.move(); 
    mySnake.drawSnake(); 
    myApple.drawApple(); 
}, 100) 

var directions = { 
    37: "left", 
    38: "up", 
    39: "right", 
    40: "down" 
}; 

document.onkeydown = function(event) { 
    var newDirection = directions[event.keyCode] 
    if (newDirection != undefined) { 
    mySnake.setDirection(newDirection); 
    }; 

回答

1

你在畫蘋果的時候忘了beginpath。另外,當吃蘋果時,你必須添加新的塊給蛇。檢查下面的編輯代碼。

這裏更新fiddle

block.prototype.drawApple = function() { 
    ctx.fillStyle = "red"; 
    ctx.textBaseline = "bottom"; 
    ctx.beginPath(); 
    ctx.arc(this.x, this.y, 6, 2 * Math.PI, false); 
    ctx.fill(); 
} 

var gameField = document.getElementById('gameField'); 
 
var ctx = gameField.getContext("2d"); 
 
var blockSize = 10; 
 
columnCt = gameField.width/blockSize; 
 
rowsCt = gameField.height/blockSize; 
 

 
var block = function(x, y) { 
 
    this.x = x; 
 
    this.y = y; 
 
} 
 

 
block.prototype.drawBlock = function() { 
 
    ctx.fillStyle = "blue"; 
 
    ctx.fillRect(this.x * blockSize, this.y * blockSize, blockSize, 
 
    blockSize); 
 
}; 
 

 
block.prototype.drawApple = function() { 
 
    ctx.fillStyle = "red"; 
 
    ctx.textBaseline = "bottom"; 
 
    ctx.beginPath(); 
 
    ctx.arc(this.x, this.y, 6, 2 * Math.PI, false); 
 
    ctx.fill(); 
 

 
} 
 

 
var Snake = function() { 
 
    this.segments = [new block(20, 20), new block(19, 20), new block(18, 20), new block(17, 20), 
 
    new block(16, 20), new block(15, 20) 
 
    ]; 
 
    this.direction = "right"; 
 
} 
 

 
Snake.prototype.drawSnake = function() { 
 
    for (i = 0; i < this.segments.length; i++) { 
 
    this.segments[i].drawBlock(); 
 
    } 
 
} 
 

 
Snake.prototype.setDirection = function(dir) { 
 
    if (this.direction == "left" && dir == "right" || this.direction == "right" && dir == "left" || this.direction == "up" && dir == "down" || 
 
    this.direction == "down" && dir == "up") { 
 
    return 
 
    } else { 
 
    this.direction = dir; 
 
    }; 
 
}; 
 

 
Snake.prototype.objectCollide = function(obj) { 
 
    if (this.segments[0].x == Math.round(obj.x/blockSize) && this.segments[0].y == Math.round(obj.y/blockSize)) { 
 
    return true 
 
    } else { 
 
    return false 
 
    } 
 
}; 
 

 
Snake.prototype.move = function() { 
 
    var head = this.segments[0]; 
 
    var newHead; 
 

 
    switch (this.direction) { 
 
    case "right": 
 
     newHead = new block(head.x + 1, head.y); 
 
     break; 
 
    case "left": 
 
     newHead = new block(head.x - 1, head.y) 
 
     break; 
 
    case "down": 
 
     newHead = new block(head.x, head.y + 1) 
 
     break; 
 
    case "up": 
 
     newHead = new block(head.x, head.y - 1) 
 
     break; 
 
    } 
 

 
    this.segments.unshift(newHead); 
 

 
    if (!this.objectCollide(myApple)) { 
 
    this.segments.pop(); 
 
    } else { 
 
    myApple = new block(Math.floor(Math.random() * gameField.width), Math.floor(Math.random() * gameField.height)); 
 
    this.segments.push(new block(this.segments[0][0], 20)) 
 
    }; 
 
    var collision = newHead.x >= columnCt || newHead.x <= -1 || 
 
    newHead.y >= rowsCt || newHead.y <= -1; 
 

 
    for (i = 1; i < this.segments.length; i++) { 
 
    if (this.segments[i].x == newHead.x && this.segments[i].y == newHead.y) { 
 
     collision = true; 
 
     break; 
 
    }; 
 
    }; 
 

 
    if (collision) { 
 
    clearInterval(myFun); 
 
    }; 
 

 
}; 
 

 
var mySnake = new Snake() 
 
mySnake.drawSnake(); 
 
var myApple = new block(Math.floor(Math.random() * gameField.width), 
 
    Math.floor(Math.random() * gameField.height)); 
 
var myFun = setInterval(function() { 
 
    ctx.clearRect(0, 0, gameField.width, gameField.height); 
 
    mySnake.move(); 
 
    mySnake.drawSnake(); 
 
    myApple.drawApple(); 
 
}, 100) 
 

 
var directions = { 
 
    37: "left", 
 
    38: "up", 
 
    39: "right", 
 
    40: "down" 
 
}; 
 

 
document.onkeydown = function(event) { 
 
    var newDirection = directions[event.keyCode] 
 
    if (newDirection != undefined) { 
 
    mySnake.setDirection(newDirection); 
 
    }; 
 
};
canvas { 
 
    position: absolute; 
 
    top: 0; 
 
    bottom: 0; 
 
    left: 0; 
 
    right: 0; 
 
    margin: auto; 
 
    border: 5px solid grey; 
 
}
<canvas id="gameField" height="500" width="500"> 
 
</canvas>

+0

謝謝!這就像一個魅力!你介意怎麼解釋一下ctx.beginPath();使它工作?因爲它會讓我感到困惑。 – Pawel

+0

否則它會從先前的繪製點畫出弧線。 –

+0

@Pawel檢查更新的代碼與蘋果吃,蛇的大小也增加。 –

2

林相當unshure爲什麼蘋果不會 「吃掉」 不過,我可能知道爲什麼它看起來太奇怪了:

如果您畫到畫布看起來像一支鋼筆。所以,每當你畫一個新的蘋果,筆就移動到那個位置,並畫出一條線。在幾個蘋果之後,如果你調用.fill(),這個(但不可見的)行會被填充。所以你需要在畫之前移動筆:

block.prototype.drawApple = function() { 
    ctx.fillStyle = "red"; 
    ctx.textBaseline = "bottom"; 
    ctx.moveTo(this.x,this.y); 
    ctx.arc(this.x, this.y, 6, 2 * Math.PI, false); 
    ctx.fill(); 
} 
+0

謝謝! ,我真的不知道爲什麼我沒有想到......所以1個問題解決了,但爲什麼蘋果沒有「吃」? – Pawel

相關問題