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.
<h1>Super HTML5 Brick Breaker</h1>
<p>Click to begin. Use the mouse to move the paddle. If there are no bricks left, let the ball touch the paddle to finish the game.</p>
<canvas id="gameCanvas" width="800" height="600"></canvas>
body {
display: flex;
flex-direction: column;
justify-content: center;
text-align: center;
font-family: monospace;
background-color: #DDD;
}
p {
font-size: 1.5em;
font-weight: bold;
width: 600px;
margin: auto;
margin-bottom: 20px;
}
#gameCanvas {
margin: auto;
border-radius: 5px;
}
var canvas, canvasContext;
//ball variables
var ballX = 400;
var ballSpeedX = 0;
var ballY = 530;
var ballSpeedY = 0;
//paddle variables and constants
const PADDLE_WIDTH = 100;
const PADDLE_HEIGHT = 10;
const PADDLE_DIST_FROM_EDGE = 60;
var paddleX = 350;
//mouse variables;
var mouseX;
var mouseY;
//bricks variables and constants
const BRICK_WIDTH = 80;
const BRICK_HEIGHT = 20;
const BRICK_COLS = 10;
const BRICK_GAP = 2;
const BRICK_ROWS = 17;
var brickGrid = new Array(BRICK_COLS * BRICK_ROWS);
var bricksLeft = 0;
//score variables
var maximumScore = 0;
var playerScore = 0;
var attempts = 5;
var playerAttempts = attempts;
var showEndingScreen = false;
function updateMousePosition(evt) {
var rect = canvas.getBoundingClientRect();
var root = document.documentElement;
mouseX = evt.clientX - rect.left - root.scrollLeft;
mouseY = evt.clientY - rect.top - root.scrollTop;
paddleX = mouseX - (PADDLE_WIDTH/2);
//cheat to test the ball collision
// ballX = mouseX;
// ballY = mouseY;
// ballSpeedX = 4;
// ballSpeedY = -4;
}
function handleMouseClick(evt) {
if(showEndingScreen) {
playerScore = 0;
maximumScore = 0;
playerAttempts = attempts;
brickReset();
ballReset();
showEndingScreen = false;
}
if(ballSpeedX == 0 && ballSpeedY == 0) {
ballSpeedX = 0;
ballSpeedY = -5;
}
}
window.onload = function() {
canvas = document.getElementById('gameCanvas');
canvasContext = canvas.getContext('2d');
var framesPerSecond = 30;
setInterval(updateAll, 1000/framesPerSecond);
canvas.addEventListener('mousedown', handleMouseClick);
canvas.addEventListener('mousemove', updateMousePosition);
brickReset();
}
function updateAll() {
moveAll();
drawAll();
}
function ballReset() {
if(playerAttempts <= 0) {
showEndingScreen = true;
}
ballX = canvas.width/2;
ballY = 400;
ballSpeedX = 0;
ballSpeedY = 5;
}
function ballMovement() {
ballX += ballSpeedX;
//right
if(ballX > canvas.width && ballSpeedX > 0.0) {
ballSpeedX *= -1;
}
//left
if(ballX < 0 && ballSpeedX < 0.0) {
ballSpeedX *= -1;
}
ballY += ballSpeedY;
// bottom
if(ballY > canvas.height) {
playerAttempts--;
ballReset();
}
// top
if(ballY < 0 && ballSpeedY < 0.0) {
ballSpeedY *= -1;
}
}
function isBrickAtColRow(col, row) {
if(col >= 0 && col < BRICK_COLS && row >= 0 && row < BRICK_ROWS) {
var brickIndexUnderCoord = rowColToArrayIndex(col, row);
return brickGrid[brickIndexUnderCoord];
} else {
return false;
}
}
function ballBrickCollision() {
var ballBrickCol = Math.floor(ballX / BRICK_WIDTH);
var ballBrickRow = Math.floor(ballY / BRICK_HEIGHT);
var brickIndexUnderBall = rowColToArrayIndex(ballBrickCol, ballBrickRow);
if(ballBrickCol >= 0 && ballBrickCol < BRICK_COLS && ballBrickRow >= 0 && ballBrickRow < BRICK_ROWS) {
if(isBrickAtColRow(ballBrickCol, ballBrickRow)) {
brickGrid[brickIndexUnderBall] = false;
bricksLeft--; //remove brick from the amount
console.log(bricksLeft);
playerScore += 10;
console.log(playerScore);
var previousBallX = ballX - ballSpeedX;
var previousBallY = ballY - ballSpeedY;
var previousBrickCol = Math.floor(previousBallX / BRICK_WIDTH);
var previousBrickRow = Math.floor(previousBallY / BRICK_HEIGHT);
var bothTestsFailed = true;
if(previousBrickCol != ballBrickCol) {
if(isBrickAtColRow(previousBrickCol, ballBrickRow) == false) {
ballSpeedX *= -1;
bothTestsFailed = false;
}
}
if(previousBrickRow != ballBrickRow) {
if(isBrickAtColRow(previousBrickCol, ballBrickRow) == false) {
ballSpeedY *= -1;
bothTestsFailed = false;
}
}
if(bothTestsFailed) { //armpit case prevents the ball from going through when both corners are covered
ballSpeedX *= -1;
ballSpeedY *= -1;
}
}
}
}
function ballPaddleCollision() {
var paddleTopEdgeY = canvas.height - PADDLE_DIST_FROM_EDGE;
var paddleBottomEdgeY = paddleTopEdgeY + PADDLE_HEIGHT;
var paddleLeftEdgeX = paddleX;
var paddleRightEdgeX = paddleLeftEdgeX + PADDLE_WIDTH;
if(ballY+10 > paddleTopEdgeY && //below the top of the paddle
ballY < paddleBottomEdgeY && //above the bottom of the paddle
ballX+10 > paddleLeftEdgeX && //right of the left side of the paddle
ballX-10 < paddleRightEdgeX) { //left of the right side of the paddle
ballSpeedY *= -1;
var centerOfPaddleX = paddleX + PADDLE_WIDTH/2;
var ballDistFromPaddleCenterX = ballX - centerOfPaddleX;
ballSpeedX = ballDistFromPaddleCenterX * 0.35;
if(bricksLeft == 0) {
// brickReset();
showEndingScreen = true;
}
}
}
function moveAll() {
if(showEndingScreen) {
return;
}
ballMovement();
ballBrickCollision();
ballPaddleCollision();
}
function brickReset() {
bricksLeft = 0;
var i;
for(i = 0; i < 3 * BRICK_COLS; i++) {
brickGrid[i] = false;
}
for(; i < BRICK_COLS * BRICK_ROWS; i++) {
if(Math.random() < 0.5) {
brickGrid[i] = true;
bricksLeft++;//counts how many bricks there are on the scene and stores the value
maximumScore += 10;
}else {
brickGrid[i] = false;
}//end of else (random check)
}//end of for
console.log(maximumScore);
}//end of brickReset
function rowColToArrayIndex(col, row) {
return col + row * BRICK_COLS;
}
function drawBricks() {
for(var eachRow = 0; eachRow < BRICK_ROWS; eachRow++) {
for(var eachCol = 0; eachCol < BRICK_COLS; eachCol++) {
var arrayIndex = rowColToArrayIndex(eachCol, eachRow);
if(brickGrid[arrayIndex]) {
rect((BRICK_WIDTH*eachCol), BRICK_HEIGHT*eachRow, BRICK_WIDTH-BRICK_GAP, BRICK_HEIGHT-BRICK_GAP, 'blue');
}//end of brick drawing if true
}
}//end of brick for
}//end of drawBricks
function drawAll() {
//background
rect(0, 0, canvas.width, canvas.height, 'black');
if(showEndingScreen) {
if(playerScore == maximumScore) {
text("YOU WIN!", canvas.width/2, 100, 'white', 'bold 3em Arial', 'center');
text("SCORE: " + playerScore, canvas.width/2, 250, 'white', 'bold 2em Arial', 'center');
text("ATTEMPTS: " + playerAttempts, canvas.width/2, 400, 'white', 'bold 2em Arial', 'center');
text("Click to continue", canvas.width/2, 550, 'white', 'bold 1.5em Arial', 'center');
} else {
text("YOU LOSE!", canvas.width/2, 100, 'white', 'bold 3em Arial', 'center');
text("SCORE: " + playerScore, canvas.width/2, 250, 'white', 'bold 2em Arial', 'center');
text("ATTEMPTS: " + playerAttempts, canvas.width/2, 400, 'white', 'bold 2em Arial', 'center');
text("Click to continue", canvas.width/2, 550, 'white', 'bold 1.5em Arial', 'center');
}
return;
}
//ball
circle(ballX, ballY, 10, 'white');
//paddle
rect(paddleX, canvas.height-PADDLE_DIST_FROM_EDGE, PADDLE_WIDTH, PADDLE_HEIGHT, 'white');
//bricks
drawBricks();
var mouseBrickCol = Math.floor(mouseX / BRICK_WIDTH);
var mouseBrickRow = Math.floor(mouseY / BRICK_HEIGHT);
var brickIndexUnderMouse = rowColToArrayIndex(mouseBrickCol, mouseBrickRow);
text(mouseBrickCol + "," + mouseBrickRow + ":" + brickIndexUnderMouse, mouseX, mouseY, 'yellow', '12px Arial');
text("Score: " + playerScore, 10, 30, 'white', 'bold 1.4em monospace', 'left');
text("Attempts: " + playerAttempts, 673, 30, 'white', 'bold 1.4em monospace', 'left');
}
function rect(topLeftX, topLeftY, boxWidth, boxHeight, fillColor) {
canvasContext.fillStyle = fillColor;
canvasContext.fillRect(topLeftX, topLeftY, boxWidth, boxHeight);
}
function circle(centerX, centerY, radius, fillColor) {
canvasContext.fillStyle = fillColor;
canvasContext.beginPath();
canvasContext.arc(centerX, centerY, radius, 0, Math.PI*2, true);
canvasContext.fill();
}
function text(showWords, textX, textY, fillColor, fontSizeStyle, textAlignment) {
canvasContext.fillStyle = fillColor;
canvasContext.font = fontSizeStyle;
canvasContext.textAlign = textAlignment;
canvasContext.fillText(showWords, textX, textY);
}
Also see: Tab Triggers