HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Day #59 - Snake Pong | AsmrProg</title>
</head>
<body>
<div class="score-container">
<div>Score : <span id="current-score">0</span></div>
<div>High Score : <span id="high-score">0</span></div>
</div>
<canvas id="gameCanvas" width="420" height="420"></canvas>
<div id="game-over">
Game Over!
<button id="restart-btn">Restart Game</button>
</div>
<script src="script.js"></script>
</body>
</html>
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap');
body{
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #333;
color: #fff;
overflow: hidden;
font-family: 'Poppins',sans-serif;
}
canvas{
border: 2px solid #fff;
}
.score-container{
display: flex;
justify-content: space-between;
width: 420px;
font-size: 18px;
}
#restart-btn{
margin-top: 15px;
padding: 10px 15px;
background-color: #f4f4f4;
border: none;
cursor: pointer;
transition: all 0.3s ease;
}
#game-over{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.8);
padding: 20px;
color: #fff;
display: none;
flex-direction: column;
align-items: center;
justify-content: center;
}
const canvas = document.getElementById('gameCanvas');
const context = canvas.getContext('2d');
const gameOverScreen = document.getElementById('game-over');
const restartBtn = document.getElementById('restart-btn');
const GRID_SIZE = 20;
const SNAKE_SIZE = GRID_SIZE;
const FOOD_SIZE = GRID_SIZE;
let snake, food, dx, dy, blinkCounter;
let gamePaused = false;
let score = 0;
let highScore = localStorage.getItem('highScore') || 0;
let currentScoreElem = document.getElementById('current-score');
let highScoreElem = document.getElementById('high-score');
// Initialize game state
function initializeGame() {
// Set initial snake segments
snake = [
{ x: Math.floor(canvas.width / 2 / GRID_SIZE) * GRID_SIZE, y: Math.floor(canvas.height / 2 / GRID_SIZE) * GRID_SIZE },
{ x: Math.floor(canvas.width / 2 / GRID_SIZE) * GRID_SIZE, y: (Math.floor(canvas.height / 2 / GRID_SIZE) + 1) * GRID_SIZE },
];
// Set the initial food position and direction
food = {
...generateFoodPosition(),
dx: (Math.random() < 0.5 ? 1 : -1) * GRID_SIZE,
dy: (Math.random() < 0.5 ? 1 : -1) * GRID_SIZE
};
// Set initial snake direction
dx = 0;
dy = -GRID_SIZE;
blinkCounter = 0;
score = 0;
currentScoreElem.textContent = score;
highScoreElem.textContent = highScore;
}
initializeGame();
// Handle keyboard inputs for snake movement
document.addEventListener('keydown', function (event) {
switch (event.key) {
case 'ArrowUp':
if (dy === 0) {
dx = 0;
dy = -GRID_SIZE;
}
break;
case 'ArrowDown':
if (dy === 0) {
dx = 0;
dy = GRID_SIZE;
}
break;
case 'ArrowLeft':
if (dx === 0) {
dx = -GRID_SIZE;
dy = 0;
}
break;
case 'ArrowRight':
if (dx === 0) {
dx = GRID_SIZE;
dy = 0;
}
break;
}
});
// Generate a food position that doesn't collide with the snake
function generateFoodPosition() {
while (true) {
let newFoodPosition = {
x: Math.floor(Math.random() * canvas.width / GRID_SIZE) * GRID_SIZE,
y: Math.floor(Math.random() * canvas.height / GRID_SIZE) * GRID_SIZE
};
let collisionWithSnake = false;
for (let segment of snake) {
if (segment.x === newFoodPosition.x && segment.y === newFoodPosition.y) {
collisionWithSnake = true;
break;
}
}
// Return the position if there is no collision
if (!collisionWithSnake) {
return newFoodPosition;
}
}
}
// Check for collisions with wall or self
function checkCollision() {
if (snake[0].x < 0 || snake[0].x >= canvas.width || snake[0].y < 0 || snake[0].y >= canvas.height) {
return true;
}
for (let i = 1; i < snake.length; i++) {
if (snake[i].x === snake[0].x && snake[i].y === snake[0].y) {
return true;
}
}
return false;
}
// Main game update function
function update() {
if (gamePaused) return;
// Calculate new snake head position
const head = { x: snake[0].x + dx, y: snake[0].y + dy };
snake.unshift(head);
// Check for collisions
if (checkCollision()) {
if (score > highScore) {
highScore = score;
localStorage.setItem('highScore', highScore);
highScoreElem.textContent = highScore;
}
gameOver();
return;
}
// Check for snake eating food
if (head.x === food.x && head.y === food.y) {
score++;
currentScoreElem.textContent = score;
food = {
...generateFoodPosition(),
dx: (Math.random() < 0.5 ? 1 : -1) * GRID_SIZE,
dy: (Math.random() < 0.5 ? 1 : -1) * GRID_SIZE
};
// Check for win condition (snake fills entire screen)
if (snake.length === (canvas.width / GRID_SIZE) * (canvas.height / GRID_SIZE)) {
gameWin();
return;
}
} else {
snake.pop(); // Remove tail segment
}
// Update food position
if (blinkCounter % 4 === 0) {
food.x += food.dx;
food.y += food.dy;
// Handle food collisions with wall
if (food.x < 0) {
food.dx = -food.dx;
food.x = 0;
}
if (food.x >= canvas.width) {
food.dx = -food.dx;
food.x = canvas.width - GRID_SIZE;
}
if (food.y < 0) {
food.dy = -food.dy;
food.y = 0;
}
if (food.y >= canvas.height) {
food.dy = -food.dy;
food.y = canvas.height - GRID_SIZE;
}
}
blinkCounter++;
draw(); // Draw the game objects
}
// Draw the background grid
function drawGrid() {
context.strokeStyle = "#AAA";
for (let i = 0; i < canvas.width; i += GRID_SIZE) {
context.beginPath();
context.moveTo(i, 0);
context.lineTo(i, canvas.height);
context.stroke();
}
for (let j = 0; j < canvas.height; j += GRID_SIZE) {
context.beginPath();
context.moveTo(0, j);
context.lineTo(canvas.width, j);
context.stroke();
}
}
// Draw game objects (snake and food)
function draw() {
context.clearRect(0, 0, canvas.width, canvas.height);
drawGrid();
for (const segment of snake) {
context.fillStyle = 'green';
context.fillRect(segment.x, segment.y, SNAKE_SIZE, SNAKE_SIZE);
}
context.fillStyle = 'red';
context.fillRect(food.x, food.y, FOOD_SIZE, FOOD_SIZE);
}
// Handle game over state
function gameOver() {
gamePaused = true;
gameOverScreen.style.display = 'flex';
}
// Handle game win state
function gameWin() {
gamePaused = true;
alert("Congratulations! You won!");
initializeGame();
}
// Restart game when restart button clicked
restartBtn.addEventListener('click', function () {
gameOverScreen.style.display = 'none';
gamePaused = false;
initializeGame();
update();
});
// Call update function every 100ms
setInterval(update, 100);
// Pause the game when window loses focus
window.addEventListener('blur', function () {
gamePaused = true;
});
// Resume the game when window gains focus
window.addEventListener('focus', function () {
gamePaused = false;
update();
});
Also see: Tab Triggers