mirror of
https://github.com/clb92/simple-js-snake.git
synced 2025-12-06 01:32:03 +01:00
Score and lots of fixes. Audio works better, more drops on the board at the same time, and more.
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
html, body {
|
html, body {
|
||||||
height: 100%;
|
height: 100%;
|
||||||
background-color: #333;
|
background-color: black;
|
||||||
}
|
}
|
||||||
|
|
||||||
.error {
|
.error {
|
||||||
@@ -34,6 +34,20 @@ html, body {
|
|||||||
-moz-transition: all .1s ease;
|
-moz-transition: all .1s ease;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#score {
|
||||||
|
position: absolute;
|
||||||
|
float: left;
|
||||||
|
top: 6%;
|
||||||
|
left: 4%;
|
||||||
|
font-size: 20pt;
|
||||||
|
color: #333;
|
||||||
|
text-align: center;
|
||||||
|
z-index: 101;
|
||||||
|
transition: all .5s ease;
|
||||||
|
-webkit-transition: all .5s ease;
|
||||||
|
-moz-transition: all .5s ease;
|
||||||
|
}
|
||||||
|
|
||||||
#popup-text {
|
#popup-text {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
float: left;
|
float: left;
|
||||||
|
|||||||
@@ -14,7 +14,11 @@
|
|||||||
<canvas id="gameCanvas">
|
<canvas id="gameCanvas">
|
||||||
<p class="error">Your browser does not support HTML5 Canvas!</p>
|
<p class="error">Your browser does not support HTML5 Canvas!</p>
|
||||||
</canvas>
|
</canvas>
|
||||||
<p id="popup-text">Ready?<br><small><small>Press Space, ESC or P to begin. Those keys also pause/unpause.</small></small></p>
|
<p id="score"></p>
|
||||||
|
<p id="popup-text">Ready?
|
||||||
|
<br><small><small>Press Space, ESC or P to begin. Those keys also pause/unpause.</small></small>
|
||||||
|
<br><br><small><small><small>Notice: If you resize the window, you'll have to reload the page!</small></small></small>
|
||||||
|
</p>
|
||||||
<div id="overlay"></div>
|
<div id="overlay"></div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@@ -26,7 +26,14 @@ var Game = function() {
|
|||||||
death: new Audio("audio/death3.wav"),
|
death: new Audio("audio/death3.wav"),
|
||||||
speedUp: new Audio("audio/speedUp.wav"),
|
speedUp: new Audio("audio/speedUp.wav"),
|
||||||
speedDown: new Audio("audio/speedDown.wav"),
|
speedDown: new Audio("audio/speedDown.wav"),
|
||||||
pause: new Audio("audio/pause.wav")
|
pause: new Audio("audio/pause.wav"),
|
||||||
|
start: new Audio("audio/test.wav")
|
||||||
|
}
|
||||||
|
for (var sound in this.sounds) {
|
||||||
|
if (this.sounds.hasOwnProperty(sound)) {
|
||||||
|
this.sounds[sound].load();
|
||||||
|
console.log('Loaded: ' + this.sounds[sound].src);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.canvas = new Canvas(this, "gameCanvas");
|
this.canvas = new Canvas(this, "gameCanvas");
|
||||||
this.initGrid();
|
this.initGrid();
|
||||||
@@ -40,6 +47,7 @@ Game.prototype.initGrid = function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Game.prototype.start = function() {
|
Game.prototype.start = function() {
|
||||||
|
//this.sounds.start.play();
|
||||||
this.paused = false;
|
this.paused = false;
|
||||||
this.started = true;
|
this.started = true;
|
||||||
/* var parent = this;
|
/* var parent = this;
|
||||||
@@ -80,6 +88,7 @@ Game.prototype.start = function() {
|
|||||||
|
|
||||||
Game.prototype.update = function() {
|
Game.prototype.update = function() {
|
||||||
this.snake.move();
|
this.snake.move();
|
||||||
|
this.writeScore();
|
||||||
this.resources.checkDropExpiration();
|
this.resources.checkDropExpiration();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,10 +98,12 @@ Game.prototype.togglePause = function() {
|
|||||||
if (this.paused) {
|
if (this.paused) {
|
||||||
//clearInterval(this.gameLoop);
|
//clearInterval(this.gameLoop);
|
||||||
this.canvas.canvas.style.filter = "blur(40px)";
|
this.canvas.canvas.style.filter = "blur(40px)";
|
||||||
|
this.canvas.canvas.style.WebkitFilter = "blur(40px)";
|
||||||
document.getElementById("popup-text").textContent = "Paused!";
|
document.getElementById("popup-text").textContent = "Paused!";
|
||||||
document.getElementById("popup-text").removeAttribute("class");
|
document.getElementById("popup-text").removeAttribute("class");
|
||||||
} else {
|
} else {
|
||||||
this.canvas.canvas.style.filter = "blur(0px)";
|
this.canvas.canvas.style.filter = "blur(0px)";
|
||||||
|
this.canvas.canvas.style.WebkitFilter = "blur(0px)";
|
||||||
document.getElementById("popup-text").setAttribute("class", "hidden");
|
document.getElementById("popup-text").setAttribute("class", "hidden");
|
||||||
if (!this.started) {
|
if (!this.started) {
|
||||||
this.start();
|
this.start();
|
||||||
@@ -109,7 +120,6 @@ Game.prototype.applyEffect = function(effect) {
|
|||||||
this.snake.embiggen(1);
|
this.snake.embiggen(1);
|
||||||
break;
|
break;
|
||||||
case ('death'):
|
case ('death'):
|
||||||
this.sounds.death.play();
|
|
||||||
this.end();
|
this.end();
|
||||||
break;
|
break;
|
||||||
case ('faster'):
|
case ('faster'):
|
||||||
@@ -130,11 +140,21 @@ Game.prototype.applyEffect = function(effect) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Game.prototype.writeScore = function() {
|
||||||
|
document.getElementById("score").textContent = this.score + " points";
|
||||||
|
}
|
||||||
|
|
||||||
Game.prototype.end = function() {
|
Game.prototype.end = function() {
|
||||||
|
this.sounds.death.play();
|
||||||
this.gameover = true;
|
this.gameover = true;
|
||||||
clearInterval(this.gameLoop);
|
clearInterval(this.gameLoop);
|
||||||
this.canvas.canvas.style.transition = "all 1s ease-out";
|
this.canvas.canvas.style.transition = "all 1s ease-out";
|
||||||
|
this.canvas.canvas.style.MozTransition = "all 1s ease-out";
|
||||||
|
this.canvas.canvas.style.WebkitTransition = "all 1s ease-out";
|
||||||
this.canvas.canvas.style.filter = "blur(20px)";
|
this.canvas.canvas.style.filter = "blur(20px)";
|
||||||
document.getElementById("popup-text").textContent = "Game over!";
|
this.canvas.canvas.style.WebkitFilter = "blur(20px)";
|
||||||
|
document.getElementById("score").style.filter = "blur(20px)";
|
||||||
|
document.getElementById("score").style.WebkitFilter = "blur(20px)";
|
||||||
|
document.getElementById("popup-text").innerHTML = "Game over!<br><br><small>Score: " + this.score + "</small>";
|
||||||
document.getElementById("popup-text").removeAttribute("class");
|
document.getElementById("popup-text").removeAttribute("class");
|
||||||
}
|
}
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
var Resources = function(game) {
|
var Resources = function(game) {
|
||||||
this.game = game;
|
this.game = game;
|
||||||
this.droprateModifier = 5;
|
this.droprateModifier = 5;
|
||||||
this.maxDrops = 3;
|
this.maxDrops = 5;
|
||||||
this.types = [
|
this.types = [
|
||||||
'food',
|
'food',
|
||||||
'food',
|
'food',
|
||||||
@@ -69,7 +69,7 @@ Resources.prototype.checkDropExpiration = function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Resources.prototype.dropRandom = function() {
|
Resources.prototype.dropRandom = function() {
|
||||||
if (!this.game.paused) {
|
if (!this.game.paused && !this.game.gameover) {
|
||||||
if (this.locations.length < this.maxDrops) {
|
if (this.locations.length < this.maxDrops) {
|
||||||
var randX = this.getRandomInt(0, this.game.grid.width - 1);
|
var randX = this.getRandomInt(0, this.game.grid.width - 1);
|
||||||
var randY = this.getRandomInt(0, this.game.grid.height - 1);
|
var randY = this.getRandomInt(0, this.game.grid.height - 1);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
var init = function () {
|
var init = function () {
|
||||||
var game = new Game();
|
var game = new Game();
|
||||||
game.canvas.canvas.style.filter = "blur(40px)";
|
game.canvas.canvas.style.filter = "blur(40px)";
|
||||||
|
game.canvas.canvas.style.WebkitFilter = "blur(40px)";
|
||||||
game.canvas.drawAll();
|
game.canvas.drawAll();
|
||||||
|
|
||||||
setTimeout(function(){
|
setTimeout(function(){
|
||||||
|
|||||||
Reference in New Issue
Block a user