Skip to content

Instantly share code, notes, and snippets.

@ebrahimebrahim
Created January 10, 2023 02:46
Show Gist options
  • Save ebrahimebrahim/4cd2497e335fab58af4a48a688bed7ac to your computer and use it in GitHub Desktop.
Save ebrahimebrahim/4cd2497e335fab58af4a48a688bed7ac to your computer and use it in GitHub Desktop.
snake_game_edits
<!DOCTYPE html>
<html>
<head>
<title>Snake Game</title>
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<style>
html, body {
height: 100%;
margin: 0;
}
body {
background: black;
display: flex;
align-items: center;
justify-content: center;
}
canvas {
border: 2px solid white; background-color:gold;
}
#p1
{
color:red;
position:absolute;
left:100px;
top:10px;
}
#p2
{
color:red;
position:absolute;
left:100px;
top:30px;
}
#score
{
color:yellow;
position:absolute;
left:155px;
top:10px;
}
#totalScoreLabel
{
color:red;
position:absolute;
left:100px;
top:50px;
}
#totalScoreValue
{
color:yellow;
}
#high
{
color:yellow;
position:absolute;
left:195px;
top:30px;
}
#btn_stop
{
color:brown;
position:absolute;
left:100px;
top:80px;
}
#end_msg
{
color:brown;
position:absolute;
left:100px;
top:100px;
}
</style>
</head>
<body>
<p id="p1">SCORE:</p>
<p id="p2">HIGHSCORE:</p>
<p id="score"></p>
<p id="high"></p>
<p id="totalScoreLabel">TOTAL SCORE:&nbsp;<span id="totalScoreValue"></span></p>
<canvas width="400" height="400" id="game"></canvas>
<script>
var canvas = document.getElementById('game');
var context = canvas.getContext('2d');
var grid = 16;
var count = 0;
var score=0;
var totalScore = 0;
//reading last score value
if(localStorage.score){
document.getElementById('score').innerHTML=localStorage.score;
}
var max=0;
var snake = {
x: 160,
y: 160,
// snake velocity. moves one grid length every frame in either the x or y direction
dx: grid,
dy: 0,
// keep track of all grids the snake body occupies
cells: [],
// length of the snake. grows when eating an apple
maxCells: 4
};
var apple = {
x: 320,
y: 320
};
// get random whole numbers in a specific range
// @see https://stackoverflow.com/a/1527820/2124254
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
// game loop
function loop() {
requestAnimationFrame(loop);
// slow game loop to 10 fps instead of 60 (60/10 = 6)
if (++count < 6) {
return;
}
count = 0;
context.clearRect(0,0,canvas.width,canvas.height);
// move snake by it's velocity
snake.x += snake.dx;
snake.y += snake.dy;
// wrap snake position horizontally on edge of screen
if (snake.x < 0) {
snake.x = canvas.width - grid;
}
else if (snake.x >= canvas.width) {
snake.x = 0;
}
// wrap snake position vertically on edge of screen
if (snake.y < 0) {
snake.y = canvas.height - grid;
}
else if (snake.y >= canvas.height) {
snake.y = 0;
}
// keep track of where snake has been. front of the array is always the head
snake.cells.unshift({x: snake.x, y: snake.y});
// remove cells as we move away from them
if (snake.cells.length > snake.maxCells) {
snake.cells.pop();
}
// draw apple
context.fillStyle = 'red';
context.fillRect(apple.x, apple.y, grid-1, grid-1);
// draw snake one cell at a time
context.fillStyle = 'green';
snake.cells.forEach(function(cell, index) {
// drawing 1 px smaller than the grid creates a grid effect in the snake body so you can see how long it is
context.fillRect(cell.x, cell.y, grid-1, grid-1);
// snake ate apple
if (cell.x === apple.x && cell.y === apple.y) {
snake.maxCells++;
score+=1;
totalScore+=1;
//saving score for next playing.
localStorage.setItem('score',score);
//max=score;
document.getElementById('score').innerHTML=score;
document.getElementById('totalScoreValue').innerHTML=totalScore;
// canvas is 400x400 which is 25x25 grids
apple.x = getRandomInt(0, 25) * grid;
apple.y = getRandomInt(0, 25) * grid;
}
// check collision with all cells after this one (modified bubble sort)
for (var i = index + 1; i < snake.cells.length; i++)
{
// snake occupies same space as a body part. reset game
if (cell.x === snake.cells[i].x && cell.y === snake.cells[i].y)
{
if(score>max)
{
max=score;
}
snake.x = 160;
snake.y = 160;
snake.cells = [];
snake.maxCells = 4;
snake.dx = grid;
snake.dy = 0;
score=0;
apple.x = getRandomInt(0, 25) * grid;
apple.y = getRandomInt(0, 25) * grid;
document.getElementById('high').innerHTML=max;
}
}
}
);
}
// listen to keyboard events to move the snake
document.addEventListener('keydown', function(e) {
// prevent snake from backtracking on itself by checking that it's
// not already moving on the same axis (pressing left while moving
// left won't do anything, and pressing right while moving left
// shouldn't let you collide with your own body)
// left arrow key
if (e.which === 65 && snake.dx === 0) {
snake.dx = -grid;
snake.dy = 0;
}
// up arrow key
else if (e.which === 87 && snake.dy === 0) {
snake.dy = -grid;
snake.dx = 0;
}
// right arrow key
else if (e.which === 68 && snake.dx === 0) {
snake.dx = grid;
snake.dy = 0;
}
// down arrow key
else if (e.which === 83 && snake.dy === 0) {
snake.dy = grid;
snake.dx = 0;
}
});
// start the game
requestAnimationFrame(loop);
function myFunction() {
document.getElementById('end_msg').innerHTML="";
alert('Press confirm to continue');
}
//stop playing
//$(document).ready(function(){
//$('#btn_stop').click(function(){
// document.getElementById('end_msg').innerHTML="Game stoped by player" ;
// setTimeout(myFunction, 1000);
//});
//});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment