Skip to content

Instantly share code, notes, and snippets.

@ysfzrn
Created October 2, 2017 07:26
Show Gist options
  • Save ysfzrn/e10180c7598ea41fb82e06e210047938 to your computer and use it in GitHub Desktop.
Save ysfzrn/e10180c7598ea41fb82e06e210047938 to your computer and use it in GitHub Desktop.
//src/stores/gameStore.js
const segmentRate = 10; // yılanın her hareketinde katedeceği mesafe
const boardWidth = SharedStyle.board.width;
const BoardHeight = SharedStyle.board.height - 10;
let globalID;
...
@action("Snake is moving")
handleMoveSnake =() => {
let temp = _.cloneDeep(this.snake.slice());
this.lastSegment = temp[0];
for (let i = 0; i < temp.length; i++) {
if (i !== 0) {
this.lastSegment = temp[i - 1];
}
if (this.currentDirection === "right") {
if (i === 0) {
if (this.snake[i].x + segmentRate >= boardWidth ) {
this.snake[i].x = 0;
}else{
this.snake[i].x = this.snake[i].x + segmentRate;
}
} else {
this.snake[i].x = this.lastSegment.x;
this.snake[i].y = this.lastSegment.y;
}
}
}
globalID = setTimeout(()=>{
requestAnimationFrame(this.handleMoveSnake);
}, 1000 / this.intervalRate);
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment