-
Notifications
You must be signed in to change notification settings - Fork 839
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4695 from ananyag309/main
Added Pattern Creation Game
- Loading branch information
Showing
6 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.