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

Added Pattern Creation Game #4695

Merged
merged 5 commits into from
Jul 8, 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
25 changes: 25 additions & 0 deletions Games/Pattern_Creation_Game/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<h1>Pattern Creation Game </h1>

A web-based game that allows users to create, save, load, and share patterns using a customizable grid. Users can select colors, change brush sizes, use an eraser, and apply a fill tool to create intricate designs.
<br>
<h3> Features </h3>

1- Color Picker: Select any color using an HTML color input. <br>
2- Brush Size Tool: Change the size of the brush to color multiple cells at once. <br>
3- Eraser Tool: Erase colors from the grid cells. <br>
4- Fill Tool: Fill the entire grid with the selected color. <br>
5- Undo/Redo: Revert or reapply the last few actions. <br>
6- Save Pattern: Save the current pattern as a JSON string. <br>
7- Load Pattern: Load a pattern from a JSON string. <br>
8- Clear Grid: Reset the entire grid to white.
<br>
<h3> How to Play </h3>

1- Select a Color: Use the color picker to choose a color, or click the "Eraser" to select the eraser tool. <br>
2- Change Brush Size: Select the desired brush size from the dropdown menu. <br>
3- Color the Grid: Click on grid cells to color them with the selected brush size and color. <br>
4- Fill Grid: Click the "Fill Grid" button to fill the entire grid with the selected color. <br>
5- Clear Grid: Click the "Clear Grid" button to reset the grid to white. <br>
6- Undo/Redo: Use the "Undo" and "Redo" buttons to revert or reapply the last few actions. <br>
7- Save Pattern: Click the "Save Pattern" button to save the current pattern as a JSON string in the textarea. <br>
8- Load Pattern: Enter a JSON string in the textarea and click the "Load Pattern" button to load the pattern.
35 changes: 35 additions & 0 deletions Games/Pattern_Creation_Game/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pattern Creation Game</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="game-container">
<div id="color-palette">
<input type="color" id="color-picker" onchange="selectColor(this.value)">
<div class="color eraser" onclick="selectColor('white')">Eraser</div>
</div>
<div id="controls">
<button onclick="savePattern()">Save Pattern</button>
<button onclick="loadPattern()">Load Pattern</button>
<button onclick="undo()">Undo</button>
<button onclick="redo()">Redo</button>
<button onclick="clearGrid()">Clear Grid</button>
<button onclick="fillGrid()">Fill Grid</button>
<label for="brush-size">Brush Size: </label>
<select id="brush-size" onchange="changeBrushSize(this.value)">
<option value="1">1x1</option>
<option value="2">2x2</option>
<option value="3">3x3</option>
</select>
</div>
<div id="grid-container"></div>
<textarea id="pattern-code" placeholder="Pattern code for save/load"></textarea>
</div>

<script src="script.js"></script>
</body>
</html>
110 changes: 110 additions & 0 deletions Games/Pattern_Creation_Game/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
const gridContainer = document.getElementById('grid-container');
const patternCode = document.getElementById('pattern-code');
let selectedColor = 'black';
let brushSize = 1;
let grid = [];
let history = [];
let redoStack = [];

function createGrid() {
gridContainer.innerHTML = '';
for (let i = 0; i < 10; i++) {
grid[i] = [];
for (let j = 0; j < 10; j++) {
const cell = document.createElement('div');
cell.classList.add('grid-cell');
cell.addEventListener('click', () => colorCell(i, j));
grid[i][j] = { element: cell, color: 'white' };
gridContainer.appendChild(cell);
}
}
}

function colorCell(row, col) {
for (let i = 0; i < brushSize; i++) {
for (let j = 0; j < brushSize; j++) {
if (grid[row + i] && grid[row + i][col + j]) {
const cell = grid[row + i][col + j];
if (cell.color !== selectedColor) {
history.push({ row: row + i, col: col + j, color: cell.color });
redoStack = [];
cell.color = selectedColor;
cell.element.style.backgroundColor = selectedColor;
}
}
}
}
}

function selectColor(color) {
selectedColor = color;
}

function savePattern() {
const pattern = grid.map(row => row.map(cell => cell.color));
patternCode.value = JSON.stringify(pattern);
}

function loadPattern() {
try {
const pattern = JSON.parse(patternCode.value);
pattern.forEach((row, i) => {
row.forEach((color, j) => {
grid[i][j].color = color;
grid[i][j].element.style.backgroundColor = color;
});
});
history = [];
redoStack = [];
} catch (error) {
alert('Invalid pattern code');
}
}

function undo() {
const lastAction = history.pop();
if (lastAction) {
redoStack.push({ ...lastAction, color: grid[lastAction.row][lastAction.col].color });
grid[lastAction.row][lastAction.col].color = lastAction.color;
grid[lastAction.row][lastAction.col].element.style.backgroundColor = lastAction.color;
}
}

function redo() {
const lastUndo = redoStack.pop();
if (lastUndo) {
history.push({ ...lastUndo, color: grid[lastUndo.row][lastUndo.col].color });
grid[lastUndo.row][lastUndo.col].color = lastUndo.color;
grid[lastUndo.row][lastUndo.col].element.style.backgroundColor = lastUndo.color;
}
}

function clearGrid() {
for (let row of grid) {
for (let cell of row) {
cell.color = 'white';
cell.element.style.backgroundColor = 'white';
}
}
history = [];
redoStack = [];
}

function fillGrid() {
for (let row of grid) {
for (let cell of row) {
if (cell.color !== selectedColor) {
history.push({ row: grid.indexOf(row), col: row.indexOf(cell), color: cell.color });
cell.color = selectedColor;
cell.element.style.backgroundColor = selectedColor;
}
}
}
redoStack = [];
}

function changeBrushSize(size) {
brushSize = parseInt(size);
}

createGrid();
78 changes: 78 additions & 0 deletions Games/Pattern_Creation_Game/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}

#game-container {
display: flex;
flex-direction: column;
align-items: center;
}

#color-palette {
display: flex;
margin-bottom: 20px;
}

#color-picker {
width: 40px;
height: 40px;
margin-right: 10px;
}

.color {
width: 30px;
height: 30px;
margin: 0 5px;
border: 1px solid #000;
cursor: pointer;
}

.eraser {
display: flex;
align-items: center;
justify-content: center;
width: auto;
padding: 0 10px;
background-color: white;
border: 1px solid #000;
font-size: 14px;
}

#controls {
margin-bottom: 20px;
}

button {
margin: 0 5px;
padding: 10px;
font-size: 14px;
cursor: pointer;
}

#grid-container {
display: grid;
grid-template-columns: repeat(10, 30px);
grid-template-rows: repeat(10, 30px);
gap: 2px;
}

.grid-cell {
width: 30px;
height: 30px;
background-color: white;
border: 1px solid #ddd;
cursor: pointer;
}

#pattern-code {
margin-top: 20px;
width: 300px;
height: 100px;
resize: none;
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ This repository also provides one such platforms where contributers come over an
|[Samurai_Fighting_Game](https://github.com/kunjgit/GameZone/tree/main/Games/Samurai_Fighting_Game)|
|[Tic-tac-toe](https://github.com/kunjgit/GameZone/tree/main/Games/Tic-tac-toe)|
|[Quest_For_Riches](https://github.com/kunjgit/GameZone/tree/main/Games/Quest_For_Riches)|
|[Pattern Creation Game](https://github.com/kunjgit/GameZone/tree/main/Games/Pattern_Creation_Game)|
| [IKnowYou-Mind-Reading-Game](https://github.com/kunjgit/GameZone/tree/main/Games/IKnowYou-Mind-Reading-Game) |
| [Tic_Tac_Toe_Neon](https://github.com/kunjgit/GameZone/tree/main/Games/Tic_Tac_Toe_Neon) |
| [Catch_Craze](https://github.com/kunjgit/GameZone/tree/main/Games/Catch_Craze) |
Expand Down
Binary file added assets/images/Pattern_Creation_Game.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading