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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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="modal-container" id="modal">
	<div class="modal-content">
		<h2>Game Over!</h2>
		<p>Your final score: <span id="score">123</span></p>
		<button id="play_btn">Play again</button>
		<a id="link" href="#" target="_blank">Share with your friends</a>
	</div>
</div>

<h1>Car Avoidance Game</h1>
<h4>Control your car using the arrow keys</h4>

<!-- SOCIAL PANEL HTML -->
<div class="social-panel-container">
	<div class="social-panel">
		<p>Created with <i class="fa fa-heart"></i> by
			<a target="_blank" href="https://florin-pop.com">Florin Pop</a></p>
		<button class="close-btn"><i class="fas fa-times"></i></button>
		<h4>Get in touch on</h4>
		<ul>
			<li>
				<a href="https://www.patreon.com/florinpop17" target="_blank">
					<i class="fab fa-discord"></i>
				</a>
			</li>
			<li>
				<a href="https://twitter.com/florinpop1705" target="_blank">
					<i class="fab fa-twitter"></i>
				</a>
			</li>
			<li>
				<a href="https://linkedin.com/in/florinpop17" target="_blank">
					<i class="fab fa-linkedin"></i>
				</a>
			</li>
			<li>
				<a href="https://facebook.com/florinpop17" target="_blank">
					<i class="fab fa-facebook"></i>
				</a>
			</li>
			<li>
				<a href="https://instagram.com/florinpop17" target="_blank">
					<i class="fab fa-instagram"></i>
				</a>
			</li>
		</ul>
	</div>
</div>
<button class="floating-btn">
	Get in Touch
</button>

<div class="floating-text">
	Part of <a href="https://florin-pop.com/blog/2019/09/100-days-100-projects" target="_blank">#100Days100Projects</a>
</div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Muli&display=swap');

* {
	box-sizing: border-box;
}

body {
	background-color: #786fa6;
	font-family: 'Muli', sans-serif;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	height: 100vh;
	margin: 0;
}

h1 {
	color: #fff;
	font-size: 36px;
	margin: 0;
}

h4 {
	color: #fff;
	margin: 10px 0 20px;
}

canvas {
	box-shadow: 0 0 4px rgba(0, 0, 0, 0.8);
}

.modal-container {
	background-color: rgba(0, 0, 0, 0.5);
	display: none;
	align-items: center;
	justify-content: center;
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
}

.modal-content {
	border-radius: 4px;
	box-shadow: 0 0 4px rgba(255, 255, 255, 0.8);
	padding: 20px 50px;
	background-color: #fff;
	text-align: center;
}

.modal-content h2 {
	margin: 0;
}

.modal-content p {
	margin: 15px;
}

.modal-content button {
	background-color: #303952;
	border: none;
	border-radius: 4px;
	color: #fff;
	font-size: 16px;
	padding: 10px;
	text-decoration: none;
}

.modal-content a {
	color: #303952;
	display: block;
	margin-top: 10px;
}

/* SOCIAL PANEL CSS */
.social-panel-container {
	position: fixed;
	right: 0;
	bottom: 80px;
	transform: translateX(100%);
	transition: transform 0.4s ease-in-out;
}

.social-panel-container.visible {
	transform: translateX(-10px);
}

.social-panel {	
	background-color: #fff;
	border-radius: 16px;
	box-shadow: 0 16px 31px -17px rgba(0,31,97,0.6);
	border: 5px solid #001F61;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	font-family: 'Muli';
	position: relative;
	height: 169px;	
	width: 370px;
	max-width: calc(100% - 10px);
}

.social-panel button.close-btn {
	border: 0;
	color: #97A5CE;
	cursor: pointer;
	font-size: 20px;
	position: absolute;
	top: 5px;
	right: 5px;
}

.social-panel button.close-btn:focus {
	outline: none;
}

.social-panel p {
	background-color: #001F61;
	border-radius: 0 0 10px 10px;
	color: #fff;
	font-size: 14px;
	line-height: 18px;
	padding: 2px 17px 6px;
	position: absolute;
	top: 0;
	left: 50%;
	margin: 0;
	transform: translateX(-50%);
	text-align: center;
	width: 235px;
}

.social-panel p i {
	margin: 0 5px;
}

.social-panel p a {
	color: #FF7500;
	text-decoration: none;
}

.social-panel h4 {
	margin: 20px 0;
	color: #97A5CE;	
	font-family: 'Muli';	
	font-size: 14px;	
	line-height: 18px;
	text-transform: uppercase;
}

.social-panel ul {
	display: flex;
	list-style-type: none;
	padding: 0;
	margin: 0;
}

.social-panel ul li {
	margin: 0 10px;
}

.social-panel ul li a {
	border: 1px solid #DCE1F2;
	border-radius: 50%;
	color: #001F61;
	font-size: 20px;
	display: flex;
	justify-content: center;
	align-items: center;
	height: 50px;
	width: 50px;
	text-decoration: none;
}

.social-panel ul li a:hover {
	border-color: #FF6A00;
	box-shadow: 0 9px 12px -9px #FF6A00;
}

.floating-btn {
	border-radius: 26.5px;
	background-color: #001F61;
	border: 1px solid #001F61;
	box-shadow: 0 16px 22px -17px #03153B;
	color: #fff;
	cursor: pointer;
	font-size: 16px;
	line-height: 20px;
	padding: 12px 20px;
	position: fixed;
	bottom: 20px;
	right: 20px;
	z-index: 999;
}

.floating-btn:hover {
	background-color: #ffffff;
	color: #001F61;
}

.floating-btn:focus {
	outline: none;
}

.floating-text {
	background-color: #001F61;
	border-radius: 10px 10px 0 0;
	color: #fff;
	font-family: 'Muli';
	padding: 7px 15px;
	position: fixed;
	bottom: 0;
	left: 50%;
	transform: translateX(-50%);
	text-align: center;
	z-index: 998;
}

.floating-text a {
	color: #FF7500;
	text-decoration: none;
}

@media screen and (max-width: 480px) {

	.social-panel-container.visible {
		transform: translateX(0px);
	}
	
	.floating-btn {
		right: 10px;
	}
}
              
            
!

JS

              
                const modalEl = document.getElementById('modal');
const playBtn = document.getElementById('play_btn');
const scoreEl = document.getElementById('score');
const linkEl = document.getElementById('link');

const speed = 5;
const carSize = 60;
const enemyCarsNr = Math.floor(window.innerWidth / 80);
let score = 0;

let playerCar;
let enemyCars;

let carImg;
let enemyCarImg;

playBtn.addEventListener('click', () => {
	startGame();
});

function startGame() {
	score = 0;
	modalEl.style.display = 'none';
	
	enemyCars = [];	
	playerCar = new Car(20, random(height - carSize), carSize, carImg);
	
	for(let i=0; i<enemyCarsNr; i++) {
		const car = new Car(width, random(height - carSize), carSize, enemyCarImg);
		car.vel.x = random(-speed * 2, -speed);
		enemyCars.push(car);
	}
	
	loop();
}

function preload() {
	carImg = loadImage('https://image.flaticon.com/icons/svg/296/296216.svg');
	enemyCarImg = loadImage('https://image.flaticon.com/icons/svg/744/744465.svg');
}

function setup() {
	createCanvas(window.innerWidth * 3 / 4, window.innerHeight * 3 / 4);

	startGame();
}

function draw() {
	score++;
	
	background('#303952');
	drawRoad();
	
	playerCar.checkBoundaries();
	playerCar.update();
	playerCar.draw();
	
	if(playerCar.isColliding(enemyCars)) {
		noLoop();
		gameOver();
	}
	
	enemyCars.forEach(car => {
		car.checkLeftBorder();
		car.update();
		car.draw();
	})
	
	// drawScore
	fill(255);
	textSize(20);
	text(`Score: ${score}`, width - 120, 30);
}

function drawRoad() {
	const h = 10;
	const w = width / 7;
	const spaceX = 15;
	const spaceY = height / 6;
	const offset = 40;
	
	for(let i=0; i<7; i++) {
		for(let j=0; j<6; j++) {
			fill('rgba(255, 255, 255, 0.7)');
			noStroke();
			rect(i * w + spaceX * i - offset, j * h + spaceY * j + offset, w, h);
		}
	}
}

function gameOver() {
	modalEl.style.display = 'flex';
	scoreEl.innerHTML = score;
	linkEl.href = `https://www.twitter.com/share?text=I scored ${score} points in this Car Avoidance Game created by @florinpop1705. You should check it out. It's fun! 😃&hashtags=100Days100Projects&url=https://codepen.io/FlorinPop17/full/WNNgqqO`;
}

function keyReleased() {
	switch(key) {
		case 'ArrowUp': {
			playerCar.vel.y = 0;
			break;
		}
		case 'ArrowDown': {
			playerCar.vel.y = 0;
			break;
		}
		case 'ArrowLeft': {
			playerCar.vel.x = 0;
			break;
		}
		case 'ArrowRight': {
			playerCar.vel.x = 0;
			break;
		}
	}
}

function keyPressed() {
	switch(key) {
		case 'ArrowUp': {
			playerCar.vel.y = -speed;
			break;
		}
		case 'ArrowDown': {
			playerCar.vel.y = speed;
			break;
		}
		case 'ArrowLeft': {
			playerCar.vel.x = -speed;
			break;
		}
		case 'ArrowRight': {
			playerCar.vel.x = speed;
			break;
		}
	}
}

class Car {
	constructor(x, y, size, img) {
		this.pos = createVector(x, y);
		this.vel = createVector(0, 0);
		this.size = createVector(size, size / 2);
		this.img = img;
	}
	
	update() {
		this.pos.add(this.vel);
	}
	
	draw() {
		// draw the car
		this.img.resize(this.size.x, 0);
		image(this.img, this.pos.x, this.pos.y - this.size.x / 5);
	}
	
	checkBoundaries() {
		if(this.pos.x < 0) {
			this.pos.x = 0;
		}
		
		if(this.pos.x > width - this.size.x ) {
			this.pos.x = width - this.size.x;
		}
		
		if(this.pos.y < 0) {
			this.pos.y = 0;
		}
		
		if(this.pos.y > height - this.size.y ) {
			this.pos.y = height - this.size.y;
		}
	}
	
	resetPosition() {
		this.pos.x = width;
		this.pos.y = random(height - carSize);
	}
	
	isColliding(cars) {
		for(let i=0; i<cars.length; i++) {
			const car = cars[i];
			
			// check collision
			if(this.pos.x < car.pos.x + car.size.x &&
			   this.pos.x + this.size.x > car.pos.x &&
			   this.pos.y < car.pos.y + car.size.y &&
			   this.pos.y + this.size.y > car.pos.y) {
				return true;
			}
		}
		
		return false;
	}
	
	checkLeftBorder() {
		if(this.pos.x + this.size.x < 0) {
			this.resetPosition();
		}
	}
}



// SOCIAL PANEL JS
const floating_btn = document.querySelector('.floating-btn');
const close_btn = document.querySelector('.close-btn');
const social_panel_container = document.querySelector('.social-panel-container');

floating_btn.addEventListener('click', () => {
	social_panel_container.classList.toggle('visible')
});

close_btn.addEventListener('click', () => {
	social_panel_container.classList.remove('visible')
});
              
            
!
999px

Console