Skip to content

Instantly share code, notes, and snippets.

@Siddhant-K-code
Last active December 25, 2019 05:54
Show Gist options
  • Save Siddhant-K-code/6c5c1b453e30ccde587fbb438f224703 to your computer and use it in GitHub Desktop.
Save Siddhant-K-code/6c5c1b453e30ccde587fbb438f224703 to your computer and use it in GitHub Desktop.
Color Matching Game
Color Matching Game
--------------------
A [Pen](https://codepen.io/siddhant-k-code/pen/povwRWm) by [Siddhant Khare](https://codepen.io/siddhant-k-code) on [CodePen](https://codepen.io).
[License](https://codepen.io/siddhant-k-code/pen/povwRWm/license).
<h2>Color Matching Game</h2>
<p>Click on the screen to switch the colors</p>
<small>(<a href="https://twitter.com/Siddhant_2407">Tell me</a> how far you got)</small>
<!-- 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://www.quora.com/profile/Siddhant-Khare-16">Siddhant</a></p>
<button class="close-btn"><i class="fas fa-times"></i></button>
<h4>Get in touch on</h4>
<ul>
<li>
<a href="Siddhant_2407#2347
EMAIL
" target="_blank">
<i class="fab fa-discord"></i>
</a>
</li>
<li>
<a href="https://twitter.com/Siddhant_2407" target="_blank">
<i class="fab fa-twitter"></i>
</a>
</li>
<li>
<a href="https://www.facebook.com/siddhant.khare.90226" target="_blank">
<i class="fab fa-facebook"></i>
</a>
</li>
<li>
<a href="https://www.instagram.com/ig_siddhant_khare/" target="_blank">
<i class="fab fa-instagram"></i>
</a>
</li>
</ul>
</div>
</div>
<button class="floating-btn">
Get in Touch
</button>
const colors = {
red: '#E53E3E',
blue: '#4299E1'
}
let ballSpeed = 2;
let score = 0;
// used to place the middle balls
let topPosition;
let bottomPosition;
const middleOffset = 20;
let ballTop;
let ballBottom;
let enemyBalls = [];
function setup() {
createCanvas(350, 600);
// set positions
topPosition = height / 2 - middleOffset;
bottomPosition = height / 2 + middleOffset;
// create middle balls
ballTop = new Ball({ x: width / 2, y: topPosition, col: colors.red });
ballBottom = new Ball({ x: width / 2, y: bottomPosition, col: colors.blue });
createRandomFallingBall();
}
function draw() {
background(51);
ballTop.draw();
ballBottom.draw();
enemyBalls.forEach(ball => {
ball.checkHit(ballTop);
ball.checkHit(ballBottom);
ball.update();
ball.draw();
});
fill(255);
textSize(16);
text(`Score: ${score}`, width - 75, 25);
}
function createRandomFallingBall() {
const ballCommonOptions = {
x: width / 2,
col: Math.random() < 0.5 ? colors.red : colors.blue
}
const ballTopOptions = {
y: 0,
dy: ballSpeed,
...ballCommonOptions
}
const ballBottomOptions = {
y: height,
dy: -ballSpeed,
...ballCommonOptions
}
const ball = new Ball(Math.random() < 0.5 ? ballTopOptions : ballBottomOptions);
enemyBalls.push(ball);
}
function removeBall(ball) {
enemyBalls = enemyBalls.filter(b => b !== b);
}
function mousePressed() {
let temp = ballTop.pos.y;
ballTop.pos.y = ballBottom.pos.y;
ballBottom.pos.y = temp;
}
class Ball {
constructor(options) {
this.pos = createVector(options.x, options.y);
this.vel = createVector(0, options.dy || 0);
this.size = 15;
this.col = options.col || colors.red;
}
update() {
this.pos.add(this.vel);
}
checkHit(ball) {
const d = dist(ball.pos.x, ball.pos.y, this.pos.x, this.pos.y);
if(d < this.size * 2) {
if(this.col === ball.col) {
score++;
ballSpeed += 0.2;
} else {
score = 0;
ballSpeed = 2;
}
removeBall(this);
createRandomFallingBall();
}
}
draw() {
fill(this.col);
noStroke();
circle(this.pos.x, this.pos.y, this.size * 2)
}
}
// 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')
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.min.js"></script>
@import url('https://fonts.googleapis.com/css?family=Muli&display=swap');
* {
box-sizing: border-box;
}
body {
background-color: #5758BB;
color: #fff;
font-family: 'Muli', sans-serif;
display: flex;
align-items: center;
flex-direction: column;
min-height: 100vh;
margin: 0;
}
h2 {
margin: 10px 0 0;
}
p {
margin: 5px 0;
}
a {
color: #fff;
}
canvas {
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
cursor: pointer;
margin-top: 20px;
}
/* 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;
user-select: none;
}
.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;
user-select: none;
}
.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;
}
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.2/css/all.min.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment