Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Piano tiles #2637

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Piano Tiles/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Games - Piano Tiles</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div class="canvasContainer">
<div class="score" id="score">0</div>
<div class="gameEnd none" id="gameEnd">
Opps! Wrong Tile
<br />
<button id="restartBtn">Restart Game</button>
</div>
<div class="gameStart" id="gameStart">
Lets Start Tapping
<br />
<button id="startBtn">Start Playing</button>
</div>
<canvas class="game" id="gameCanvas"></canvas>
</div>

<script src="script.js"></script>
</body>
</html>
11 changes: 11 additions & 0 deletions Piano Tiles/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "Piano Tiles",
"version": "1.0.0",
"description": "An simple Piano Tiles game",
"manifest_version": 3,
"author": "Sourabh singh rawat",
"action":{
"default_popup": "index.html",
"default_title": "Piano Tiles"
}
}
203 changes: 203 additions & 0 deletions Piano Tiles/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
var config = {
width: 300,
height: 600,
rows: 6,
cols: 4,
speed: 5,
interval: 20,
};
config.height = 600 - 2;
config.defaultSpeed = config.speed;
var score = 0;
var scoreElement;
var startGameElement, endGameElement;
var scoreElement;
var gameLoop;
var tileRows = [];
var gameCanvas;
var gameContext;
var isGameStarted = false;
document.addEventListener(
"DOMContentLoaded",
function () {
console.log("Script Loaded");
gameCanvas = document.getElementById("gameCanvas");
scoreElement = document.getElementById("score");
startGameElement = document.getElementById("gameStart");
endGameElement = document.getElementById("gameEnd");
gameContext = gameCanvas.getContext("2d");
gameCanvas.style.width = config.width + "px";
gameCanvas.style.height = config.height + "px";
gameCanvas.setAttribute("width", config.width);
gameCanvas.setAttribute("height", config.height);
gameContext.lineWidth = 0.5;
initGame();
},
null
);

function addRow() {
var black_index = Math.floor(Math.random() * config.cols);
var tile_width = config.width / config.cols;
var tile_height = config.height / config.rows;
var y = config.height;
if (tileRows.length > 0) {
var lastRow = tileRows[tileRows.length - 1];
y = lastRow.y + lastRow.height;
}
var row = {
x: 0,
y: y,
width: config.width,
height: config.height / config.rows,
tileWidth: tile_width,
tileHeight: tile_height,
color: "#FFFFFF",
black: {
index: black_index,
color: "#000000",
},
increament: function () {
if (this.y <= 0) {
console.log(this.isValid);
if (!this.isValid) {
console.log("Game Over");
stopGameLoop();
this.y -= config.speed;
displayWrongTile(this, this.black.index);
return;
}
}
this.y = this.y - config.speed;
},
decreament: function () {
this.y = this.y + config.speed;
},
isValid: false,
};
tileRows.push(row);
}

function displayRow(row) {
gameContext.fillStyle = row.color;
gameContext.fillRect(0, row.y, row.width, row.height);
for (var i = 0; i < config.cols; i++) {
gameContext.strokeRect(
i * row.tileWidth,
row.y,
row.tileWidth,
row.tileHeight
);

if (row.black.index == i) {
gameContext.fillStyle = row.black.color;
gameContext.fillRect(
i * row.tileWidth,
row.y,
row.tileWidth,
row.tileHeight
);
}
}
row.increament();
}
function startGameLoop() {
gameLoop = setInterval(function () {
displayAllRow();
}, config.interval);
}
function displayAllRow() {
gameContext.clearRect(0, 0, config.width, config.height);
for (var i = 0; i < tileRows.length; i++) {
displayRow(tileRows[i]);
}
}

function stopGameLoop() {
clearInterval(gameLoop);
gameCanvas.removeEventListener("click", handleGameUserInput);
endGameElement.style.display = "block";
}

function handleGameUserInput(e) {
if (!isGameStarted) {
isGameStarted = true;
startGameLoop();
}
var tile_width = config.width / config.cols;
var tile_height = config.height / config.rows;
var x = e.clientX - gameCanvas.offsetLeft;
var y = e.clientY - gameCanvas.offsetTop;
var clicked_row = Math.ceil(y / tile_height) - 1;
var clicked_col = Math.ceil(x / tile_width) - 1;
for (var i = 0; i < tileRows.length; i++) {
var row = tileRows[i];
if (row.y < y && row.y + row.height > y) {
if (clicked_col === row.black.index) {
if (!row.isValid) {
row.isValid = true;
row.black.color = "#AAAAAA";
score++;
scoreElement.innerHTML = score;
addRow();
} else {
stopGameLoop();
displayWrongTile(row, clicked_col);
}
} else {
stopGameLoop();
displayWrongTile(row, clicked_col);
}
break;
}
}
}

function displayWrongTile(row, col_number) {
gameContext.fillStyle = "#FF0000";
row.decreament();
gameContext.fillRect(
col_number * row.tileWidth,
row.y,
row.tileWidth,
row.tileHeight
);
}

function initGame() {
gameContext.clearRect(0, 0, config.width, config.height);
for (var i = 0; i < config.rows; i++) {
addRow();
}
for (var j = 0; j < 50; j++) {
for (var i = 0; i < tileRows.length; i++) {
tileRows[i].increament();
}
}
for (var i = 0; i < tileRows.length; i++) {
displayRow(tileRows[i]);
}
}

document.getElementById("startBtn").addEventListener("click", startGame)
function startGame() {
endGameElement.style.display = "none";
startGameElement.style.display = "none";
gameCanvas.addEventListener("click", handleGameUserInput);
}

document.getElementById("restartBtn").addEventListener("click", restartGame);

function restartGame() {
tileRows = [];
score = 0;
isGameStarted = false;
config.speed = config.defaultSpeed;
scoreElement.innerHTML = score;
endGameElement.style.display = "none";
initGame();
startGame();
// setTimeout(startGame,1000);
}


45 changes: 45 additions & 0 deletions Piano Tiles/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
body {
margin: 0px;
height: 550px;
width: 450px;
}
.canvasContainer {
margin: 0 auto;
text-align: center;
position: relative;
font-size: 0px;
height: 100%;
width: 100%;
}
.canvasContainer canvas {
border: 0.5px solid #ddd;
}
.canvasContainer .score {
position: absolute;
font-weight: bold;
font-size: 1.5em;
left: 0px;
top: 0px;
width: 100%;
}
.gameEnd,
.gameStart {
position: absolute;
top: 50%;
margin-top: -25px;
left: 0px;
width: 100%;
font-size: 1.5rem;
text-shadow: -1px -1px #fff;
}
.canvasContainer button {
background-color: #fff;
border: 1px solid #aaa;
padding: 10px 15px;
margin-top: 10px;
border-radius: 20px;
cursor: pointer;
}
.none {
display: none;
}
1 change: 1 addition & 0 deletions Rubik's Cube/3bfd9608.js

Large diffs are not rendered by default.

89 changes: 89 additions & 0 deletions Rubik's Cube/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<html>
<head>
<title>Rubik cube</title>
<meta name="description" content="rubiks cube" />
<meta property="keywords" content="rubiks cube online" />
<meta property="og:title" content="RubikCube" />
<meta property="og:description" content="html css js" />
<meta property="og:keywords" content="rubiks" />
<meta property="author" content="Sourabh" />
<meta
name="viewport"
content="width=device-width,user-scalable=no,initial-scale=1,maximum-scale=1,minimum-scale=1"
/>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
height: 500px;
width: 500px;
}
h1 {
position: fixed;
right: 5px;
bottom: 10px;
font-size: 18px;
font-weight: 400;
writing-mode: vertical-rl;
}
#container {
height: 100%;
width: 100%;
}
#status {
font-size: 18px;
position: absolute;
left: 0;
top: 0;
width: 100vw;
display: flex;
flex-wrap: wrap;
align-items: center;
}
.status-item {
margin-left: 20px;
display: flex;
align-items: center;
}
#order-select {
outline: 0;
margin-left: 5px;
}
#disorder {
width: 50px;
display: none;
}
#restore {
width: 50px;
}
</style>
<link
rel="preload"
href="./3bfd9608.js"
as="script"
crossorigin="anonymous"
/>
</head>
<body>
<div id="container">
<div id="status">
<div class="status-item">
<label for="finish">State :</label> <span id="finish"></span>
</div>
<div class="status-item">
<label for="order-select">Order :</label>
<select id="order-select">
<option value="2">2</option>
<option value="3" selected="">3</option>
<option value="4">4</option>
</select>
</div>
<div class="status-item"><button id="restore">Restore</button></div>
</div>
</div>
<script type="module" src="./3bfd9608.js"></script>
</body>
</html>
11 changes: 11 additions & 0 deletions Rubik's Cube/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "Rubiks Cube",
"version": "1.0.0",
"description": "An simple rubiks cube game",
"manifest_version": 3,
"author": "Sourabh singh rawat",
"action":{
"default_popup": "index.html",
"default_title": "Rubiks Cube"
}
}
Loading
Loading