Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 6 additions & 21 deletions client/js/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ export default class Board extends EventEmitter{
newSnake(x, y, name) {
if(this.snakes.length < 10){
let snake = new Snake(this.context, x, y, this.getAvailableColor(), name);

if (this.scoreboard) {
this.scoreboard.addPlayer(snake);
}

snake.draw();

this.snakes.push(snake);
Expand Down Expand Up @@ -78,24 +78,9 @@ export default class Board extends EventEmitter{

createScoreboard() {
this.scoreboard = new Scoreboard();

this.scoreboard.playersContainer.appendTo('#scoreboard');
}

render() {
this.intervalId = setInterval(() => {
this.snakes.forEach(snake => {
if(!snake.dead) {
snake.move(this);
}
});
this.scoreboard.updateScores(this.snakes, this.clientLocalSnake);
this.checkSnakeSelfCollision();
this.checkCollisionWithApples();
// END TEMP
}, constant.DELAY);
}

stopRendering(){
clearInterval(this.intervalId);
}
Expand Down Expand Up @@ -123,15 +108,15 @@ export default class Board extends EventEmitter{
}

checkCollisionWithApples() {
this.snakes.forEach((snake, i) => {
this.snakes.forEach((snake) => {

let firstBodyPart = snake.bodyParts[0];

this.apples.forEach((apple, index) => {
if (firstBodyPart.x < apple.x + apple.radius * 2 &&
firstBodyPart.x + firstBodyPart.width > apple.x &&
firstBodyPart.y < apple.y + apple.radius * 2 &&
firstBodyPart.height + firstBodyPart.y > apple.y) {
firstBodyPart.x + firstBodyPart.width > apple.x &&
firstBodyPart.y < apple.y + apple.radius * 2 &&
firstBodyPart.height + firstBodyPart.y > apple.y) {

this.apples.splice(index, 1);
snake.addScore();
Expand Down
4 changes: 2 additions & 2 deletions client/js/displayDisconnectMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
import $ from 'jquery';

export default function displayDisconnectMessage() {
$("#message").html("<h2>Warning !</h2><p>You have been deconnected, please check your internet connection and try again</p>").addClass("active");
$('body').addClass("blur");
$("#message").html("<h2>Warning !</h2><p>You have been deconnected, please check your internet connection and try again</p>").addClass("active");
$('body').addClass("blur");
}
4 changes: 3 additions & 1 deletion client/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ document.addEventListener('DOMContentLoaded', function () {

//server.sendAppleEaten(x, y);

board.render();
//board.render();
board.on('appleEaten', function(position){
server.sendAppleEaten(position);
});



server.on('disconnect', function(){
board.stopRendering();
displayMessage("Warning !", "You have been disconnected from the server ! Check your internet connection !");
Expand Down
39 changes: 6 additions & 33 deletions client/js/snake.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ export default class Snake {
lastBodyPart.y = 0;
}

lastBodyPart.draw();

this.moveBodyPartsInArray();
}

Expand All @@ -68,8 +66,6 @@ export default class Snake {
lastBodyPart.y = this.context.canvas.clientHeight - (this.height + constant.BODY_PART_MARGIN);
}

lastBodyPart.draw();

this.moveBodyPartsInArray();
}

Expand All @@ -87,8 +83,6 @@ export default class Snake {
lastBodyPart.x = this.context.canvas.clientWidth - (this.width + constant.BODY_PART_MARGIN);
}

lastBodyPart.draw();

this.moveBodyPartsInArray();
}

Expand All @@ -106,8 +100,6 @@ export default class Snake {
lastBodyPart.x = 0;
}

lastBodyPart.draw();

this.moveBodyPartsInArray();
}

Expand All @@ -119,41 +111,22 @@ export default class Snake {
this.bodyParts.splice(0, 0, this.bodyParts.splice(this.bodyParts.length - 1, 1)[0]);
}

move(board) {
this.checkCollisionWithApples(board.apples);

move() {
/*this.checkCollisionWithApples(board.apples);*/
if (this.direction === 'right') {
this.moveRight();
return this.moveRight();
}
else if (this.direction === 'down') {
this.moveDown();
return this.moveDown();
}
else if (this.direction === 'up') {
this.moveUp();
return this.moveUp();
}
else if (this.direction === 'left') {
this.moveLeft();
return this.moveLeft();
}
}

checkCollisionWithApples(apples) {
let firstBodyPart = this.bodyParts[0];

apples.forEach((apple, index) => {
if (firstBodyPart.x < apple.x + apple.radius * 2 &&
firstBodyPart.x + firstBodyPart.width > apple.x &&
firstBodyPart.y < apple.y + apple.radius * 2 &&
firstBodyPart.height + firstBodyPart.y > apple.y) {

apples.splice(index, 1);
this.addScore();

this.addBodyPart();

}
});
}

remove() {
this.bodyParts.forEach((bodyPart) => {
bodyPart.remove();
Expand Down
23 changes: 16 additions & 7 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,30 @@ io.on('connection', function(socket) {
console.log("snake : " + snake.name);
io.emit('new_snake', snake);
});

socket.on('changeDirection', (data) => {
io.emit('setDirection', data);
});

if(!inProgressGame){
setInterval(function() {
b.checkSnakeSelfCollision();
b.checkCollisionWithApples();
b.snakes.forEach(snake => {
if(!snake.dead) {
snake.move();
}
});
if(b.apples.length < constant.DEFAULT_APPLES_NUMBER){
let apple = b.generateApple();
io.emit('new_apple', apple);
}
}, constant.DELAY);

setInterval(function() {
inProgressGame = true;
io.emit('start', 'Démarrage de la partie');
console.log('Démarrage de la partie');

while(b.apples.length < constant.DEFAULT_APPLES_NUMBER){
let apple = b.generateApple();
io.emit('new_apple', apple);
}

setTimeout(function() {
inProgressGame = false;
Expand All @@ -85,4 +94,4 @@ io.on('connection', function(socket) {

http.listen(3000, () => {
console.log('listening on *:3000');
});
});