Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Vuyani Shabangu committed Jan 24, 2020
1 parent c2fe136 commit dded6bd
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 22 deletions.
6 changes: 6 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ h2 {
justify-content: center;
}

#score{
font-size: 100px;
right: 0;
top: 40px;
position: absolute;
}

21 changes: 2 additions & 19 deletions index.html

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Snake} from './snake.js';
let snake = new Snake();
let direction = null;
const board = document.getElementById('board');
const scoreDisplay = document.getElementById('score');

let interval = setInterval(function(){ render(); console.log(snake.getInterval())}, snake.getInterval());

Expand All @@ -21,9 +22,11 @@ const render = () => {
});
snake.displayTarget()

scoreDisplay.innerHTML = snake.getScore();

changeToTarget(snake.displayTarget().x, snake.displayTarget().y)
clearInterval(interval);
interval = setInterval(function(){ render(); console.log(snake.getInterval())}, snake.getInterval());
interval = setInterval(function(){ render(); }, snake.getInterval());
}


Expand Down
27 changes: 26 additions & 1 deletion js/snake.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class Snake {
this.pivots = new Array();
this.target = new Target(0,0,"");
this.interval = 100;
this.score = 0;
}

plotPath(){
Expand Down Expand Up @@ -85,11 +86,21 @@ export class Snake {
return valid
}

getScore(){
return this.score;
}

increaseScore(){
this.score += 1;
}

iterate(direction){

if(this.isTargetHit(this.path[0], this.target)){
this.target.plotTarget();
this.growSnake();
this.reduceInterval()
this.increaseScore();
}

if(direction)
Expand All @@ -99,7 +110,16 @@ export class Snake {
}
this.path.map(block => {this.move(block)});


this.isEndGame()
}

isEndGame(){
let endGame = false;
let head = this.path[0];
this.path.map(bodyPart => {
if(bodyPart.type !== "head" && bodyPart.x === head.x && bodyPart.y === head.y)
alert("GAME OVER!")
});
}

move(block){
Expand Down Expand Up @@ -136,6 +156,11 @@ export class Snake {
return block;
}

moveSnake(){
this.growSnake();

}


moveToCurrentDirection(block){
if(block.direction === "left" )
Expand Down
2 changes: 1 addition & 1 deletion js/target.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class Target extends Block {
this.type = "target";
}

plotTarget(){
plotTarget(snake){
this.x = this.plotTargetCoordinate();
this.y = this.plotTargetCoordinate()
}
Expand Down

0 comments on commit dded6bd

Please sign in to comment.