Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="overlay"></div>
<div class="scoreboard">
	<span class="current-score">
		<i class="fas fa-square"></i>
		<span>0</span>
	</span>
	<span class="highscore">
		<i class="fas fa-trophy"></i>
		<span>0</span>
	</span>
</div>
<div class="grid">
	<div class="instruction">
		<div>
			<div class="arrow-keys empty"></div>
			<div class="arrow-keys"><i class="fas fa-caret-up"></i></div>
			<div class="arrow-keys empty"></div>
		</div>
		<div>
			<div class="arrow-keys"><i class="fas fa-caret-left"></i></div>
			<div class="arrow-keys"><i class="fas fa-caret-down"></i></div>
			<div class="arrow-keys"><i class="fas fa-caret-right"></i></div>
		</div>
	</div>
	<div class="hand"> <i class="fas fa-hand-point-up"></i> </div>
</div>
<div class="stats">
	<div class="sky">
		<span class="stats-score">
			<i class="fas fa-square"></i>
			<span></span>
		</span>
		<span class="stats-highscore">
			<i class="fas fa-trophy"></i>
			<span></span>
		</span>
		<span class="new-highscore"></span>
	</div>
	<div class="checks">
		<button>Play Again?</button>
	</div>
</div>
              
            
!

CSS

              
                *{
	padding: 0;
	margin: 0;
	box-sizing: border-box;
}

body{
	height: 100vh;
	width: 100vw;

	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	background-image: linear-gradient(to top, #f3e7e9 0%, #e3eeff 99%, #e3eeff 100%);
}

.instruction{
	position: absolute;
	top: 50px;
	right: 50px;
	height: 120px;
	width: 120px;
	padding: 5px;
	border-radius: 10px;
	background: rgb(0,0,0,0.6);
	font-size: 20px;

	display: flex;
	flex-direction: column;
	align-items: flex-start;
}

.instruction div{
	display: flex;
}

.arrow-keys{
	width: 21px;
	height: 21px;
	margin: 2px;
	background: rgb(255,255,255,0.9);
	color: rgb(0,0,0,0.6);
	border-radius: 2px;
	display: flex;
	align-items: center;
	justify-content: center;
}

.empty{
	background: none;
}

.hand{
	position: absolute;
	top: 90px;
	right: 55px;
	border-radius: 10px;
	background: transparent;
	color: rgb(255,255,255,0.9);
	font-size: 60px;

	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
}

.scoreboard{
	background: #457004;
	display: flex;
	justify-content: space-between;
	font-size: 1.6rem;
	padding: 10px;
	font-weight: bold;
}

.current-score{
	color: red;
}

.highscore{
	color: #f2b51b; 
}

.grid{
	position: relative;
	display: flex;
	flex-direction: column;
	padding: 30px;
	background-color: #63a106;
}

.row{
	display: flex;
}

.cell{
	height: 30px;
	width: 30px;
	background: rgb(0,0,0,0.2);
}

.first-color{
	background: #8ecc39;
}

.second-color{
	background: #a2d645;
}

.food{
	background: maroon;
/* 	box-shadow: 0 0 20px 10px yellow; */
	animation: .5s ease-in-out 0s infinite alternate pulsate;
}

@keyframes pulsate {
  from { transform: scale(1); }
  to   { transform: scale(1.2); }
}

.snake{
	background: purple;
}

.snake-head{
/* 	box-shadow: 0 0 20px 10px #b0f6ff; */
	background: rebeccapurple;
	z-index: 1;
}

.stats{
	display: none;
	position: absolute;
	height: 40vmin;
	width: 40vmin;
	z-index: 2;
	/*border: 4px solid #457004;*/
	font-weight: bold;
	/*border-radius: 20px;*/
}

.sky{
	position: relative;
	background: #457004;
	height: 60%;
	font-size: 2rem;

	display: flex;
	align-items: center;
	justify-content: space-around;

	border-top-left-radius: 10px;
	border-top-right-radius: 10px;
}

.checks{
	height: 40%;
	background: #a2d645;

	display: flex;
	align-items: center;
	justify-content: space-around;
	border-bottom-left-radius: 10px;
	border-bottom-right-radius: 10px;
}

.stats-score{
	color: red;
}

.stats-highscore{
	color: #f2b51b;
}

.new-highscore{
	position: absolute;
	bottom: 10px;
	font-size: 1.3rem;
	color: greenyellow;

	animation: 1s linear 0s infinite alternate motion;
}

@keyframes motion {
  from { transform: translateX(-10%); }
  to   { transform: translateX(10%); }
}

.checks button{
	border: none;
	outline: none;
	background: #8ecc39;
	border: 2px solid #457004;
	color: #457004;
	/*text-transform: uppercase;*/

	font-weight: bold;
	font-size: 0.9rem;
	height: 40%; 
	width: 40%;
	border-radius: 10px;
}

.checks button:hover{
	background: rgb(173, 255, 47);
	cursor: pointer;
}


/*
body > *:not(.stats) {
  filter: blur(3px);
}
*/

.dark{
	z-index: 1;
	position: absolute;
	top: 0;
	left: 0;
	height: 100%;
	width: 100%;
	background: rgb(0,0,0,0.8);
}
              
            
!

JS

              
                class Snake{
	constructor(x, y){
		this.xdir = 0;
		this.ydir = 0;
		this.body = [new Square(x, y), new Square(x, y-1), new Square(x, y-2), new Square(x, y-3)];
		cells[this.body[0].pos].classList.add("snake-head");
		for(let i=0; i<this.body.length; i++)
			cells[this.body[i].pos].classList.add("snake");
	}

	move(){
		if (this.xdir === 0 && this.ydir === 0) return;

		cells[this.body[0].pos].classList.remove("snake-head");
		cells[this.body[this.body.length - 1].pos].classList.remove("snake");

		this.body.unshift(new Square(this.body[0].x + this.xdir, this.body[0].y + this.ydir))

		if (this.body[0].x >= nrows) this.body[0].x = 0;
		else if (this.body[0].x < 0) this.body[0].x = nrows-1;
		else if (this.body[0].y >= ncols) this.body[0].y = 0;
		else if (this.body[0].y < 0) this.body[0].y = ncols-1;
		this.body[0].pos = this.body[0].x * ncols + this.body[0].y;

		cells[this.body[0].pos].classList.add("snake");
		cells[this.body[0].pos].classList.add("snake-head");

		this.eatFood();
		this.checkCollision();
	}

	changeDir(e){
		let flag = false;

		if (e === "ArrowLeft" && this.ydir === 0) {
			this.ydir = -1;
			this.xdir = 0;
			flag = true;
		}
		else if (e === "ArrowRight" && this.ydir === 0){ 
			this.ydir = 1;
			this.xdir = 0;
			flag = true;
		}
		else if (e === "ArrowUp" && this.xdir === 0) {
			this.xdir = -1;
			this.ydir = 0;
			flag = true;
		}
		else if (e === "ArrowDown" && this.xdir === 0) {
			this.xdir = 1;
			this.ydir = 0;
			flag = true;
		}

		if (flag){
			this.move();
			food.show();	
		}
	}

	eatFood(){
		if (this.body[0].pos === food.pos) {
			food.updateLocation();
			currentScore.textContent = parseInt(currentScore.textContent) + 1;
		}
		else 
			this.body.pop();
	}

	checkCollision(){
		for (let i=1; i<this.body.length; i++){
			if (this.body[0].pos === this.body[i].pos){
				this.resetGame();
				break;
			}
		}
	}

	resetGame(){
		//Checking if current score is a highscore.
		// for (let i=0; i<blurItems.length; i++){
		// 	blurItems[i].style.filter = "blur(3px)";
		// }
		overlayDiv.classList.add("dark");
		play = false;
		stats.style.display = "block";
		let presentScore = parseInt(currentScore.textContent);
		statsScore.textContent = presentScore;
		
		if (presentScore > localStorage.getItem("highscore") || localStorage.getItem("highscore") === null){
			highscore.textContent = presentScore;
			localStorage.setItem("highscore", presentScore);
			newHighScore.textContent = "NEW HIGHSCORE!";
		}else{
			newHighScore.textContent = "";
		}

		statsHighScore.textContent = localStorage.getItem("highscore");


		cells[this.body[0].pos].classList.remove("snake-head");
		for (let i=0; i<this.body.length; i++){
			cells[this.body[i].pos].classList.remove("snake");
		}

		let x = Math.floor(nrows/2);
		let y = Math.floor(ncols/2);

		this.body = [new Square(x, y), new Square(x, y-1), new Square(x, y-2), new Square(x, y-3)];
		cells[this.body[0].pos].classList.add("snake-head");
		for(let i=0; i<this.body.length; i++)
			cells[this.body[i].pos].classList.add("snake");

		this.xdir = 0;
		this.ydir = 0;
		currentScore.textContent = 0;
		food.updateLocation();
	}
}

class Square{
	constructor(x, y){
		this.x = x;
		this.y = y;
		this.pos = this.x*ncols + this.y;
	}
}

class Food{
	constructor(){
		this.x = Math.floor(nrows/3);
		this.y = Math.floor(ncols/3);
		this.pos = this.x * ncols + this.y;
	}

	updateLocation(){
		cells[this.pos].classList.remove("food");
		while (true) {
			let flag = true;
			this.x = Math.floor(Math.random()*nrows);
			this.y = Math.floor(Math.random()*ncols);
			this.pos = this.x * ncols + this.y;
			for (let i=0; i<snake.body.length; i++){
				if (snake.body[i].pos === this.pos) 
					flag = false;
			}
			if (flag) 
				break;
		}
	}

	show(){
		cells[this.pos].classList.add("food");
	}
}

let play = true;
let grid = document.querySelector(".grid");

let nrows, ncols;
if (window.innerHeight < window.innerWidth){
	nrows = Math.floor((window.innerHeight - 200)/30);
	ncols = Math.floor((window.innerHeight - 100)/30);
}else{
	nrows = Math.floor((window.innerHeight - 200)/30);
	ncols = Math.floor((window.innerWidth - 100)/30);
}

for (let i=0; i<nrows; i++){
	let row = document.createElement("div");
	row.classList.add("row");
	for (let j=0; j<ncols; j++){
		let cell = document.createElement("div");
		cell.classList.add("cell");
		row.appendChild(cell);
	}
	grid.appendChild(row);
}
scoreboard = document.querySelector(".scoreboard");
scoreboard.style.width = `${ncols*30 + 60}px`;

currentScore = document.querySelector(".current-score span");

highscore = document.querySelector(".highscore span");
if (localStorage.getItem("highscore")){
	highscore.textContent = localStorage.getItem("highscore");
}

cells = document.querySelectorAll(".cell");

for (let i=0; i<nrows; i++){
	for (let j=0; j<ncols; j++){
		if ((i+j) % 2)
			cells[i*ncols+j].classList.add("first-color");
		else
			cells[i*ncols+j].classList.add("second-color");
	}
}

let snake = new Snake(Math.floor(nrows/2), Math.floor(ncols/2));
let food = new Food();

let game = setInterval(function(){
	snake.move();
	food.show();
},200);


let instruction = document.querySelector(".instruction");
let hand = document.querySelector(".hand");

window.addEventListener("keydown", function(event){
	if (!play) return;

	instruction.style.display = "none";
	hand.style.display = "none";
	snake.changeDir(event.key);
})

// window.addEventListener("resize", function(){
// 		history.go(0);
// 	}
// );


//END GAME Things
const stats = document.querySelector(".stats");
const statsScore = document.querySelector(".stats-score span");
const statsHighScore = document.querySelector(".stats-highscore span");
const newHighScore = document.querySelector(".new-highscore");
const playBtn = document.querySelector(".checks button");

// blurItems = document.querySelectorAll("body > *:not(.stats)");
const overlayDiv = document.querySelector(".overlay");

playBtn.addEventListener("click", function(){
	play = true;
	stats.style.display = "none";
	overlayDiv.classList.remove("dark");
});
              
            
!
999px

Console