-
Notifications
You must be signed in to change notification settings - Fork 163
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 #1118 from Aditi22Bansal/main
Shadow clone game
- Loading branch information
Showing
3 changed files
with
125 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,17 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Shadow Clone</title> | ||
<link rel="stylesheet" href="style.css"> | ||
</head> | ||
<body> | ||
<div id="game-container"> | ||
<div id="player"></div> | ||
</div> | ||
<button id="clone-button">Create Clone</button> | ||
<button id="reset-button">Reset</button> | ||
<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,63 @@ | ||
document.addEventListener('DOMContentLoaded', () => { | ||
const player = document.getElementById('player'); | ||
const gameContainer = document.getElementById('game-container'); | ||
const cloneButton = document.getElementById('clone-button'); | ||
const resetButton = document.getElementById('reset-button'); | ||
let clones = []; | ||
|
||
let playerPosition = { x: 10, y: 10 }; | ||
let moveDistance = 10; | ||
|
||
function updatePlayerPosition() { | ||
player.style.left = playerPosition.x + 'px'; | ||
player.style.top = playerPosition.y + 'px'; | ||
} | ||
|
||
function createClone() { | ||
const clone = document.createElement('div'); | ||
clone.classList.add('clone'); | ||
clone.style.left = playerPosition.x + 'px'; | ||
clone.style.top = playerPosition.y + 'px'; | ||
gameContainer.appendChild(clone); | ||
clones.push(clone); | ||
} | ||
|
||
function resetGame() { | ||
playerPosition = { x: 10, y: 10 }; | ||
updatePlayerPosition(); | ||
clones.forEach(clone => gameContainer.removeChild(clone)); | ||
clones = []; | ||
} | ||
|
||
function movePlayer(e) { | ||
switch (e.key) { | ||
case 'ArrowUp': | ||
if (playerPosition.y - moveDistance >= 0) { | ||
playerPosition.y -= moveDistance; | ||
} | ||
break; | ||
case 'ArrowDown': | ||
if (playerPosition.y + moveDistance <= gameContainer.clientHeight - player.clientHeight) { | ||
playerPosition.y += moveDistance; | ||
} | ||
break; | ||
case 'ArrowLeft': | ||
if (playerPosition.x - moveDistance >= 0) { | ||
playerPosition.x -= moveDistance; | ||
} | ||
break; | ||
case 'ArrowRight': | ||
if (playerPosition.x + moveDistance <= gameContainer.clientWidth - player.clientWidth) { | ||
playerPosition.x += moveDistance; | ||
} | ||
break; | ||
} | ||
updatePlayerPosition(); | ||
} | ||
|
||
document.addEventListener('keydown', movePlayer); | ||
cloneButton.addEventListener('click', createClone); | ||
resetButton.addEventListener('click', resetGame); | ||
|
||
updatePlayerPosition(); | ||
}); |
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,45 @@ | ||
body { | ||
margin: 0; | ||
padding: 0; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
background-color: #282c34; | ||
color: white; | ||
font-family: Arial, sans-serif; | ||
} | ||
|
||
#game-container { | ||
position: relative; | ||
width: 500px; | ||
height: 500px; | ||
background-color: #444; | ||
border: 2px solid #fff; | ||
margin-top: 20px; | ||
} | ||
|
||
#player, .clone { | ||
position: absolute; | ||
width: 20px; | ||
height: 20px; | ||
background-color: #61dafb; | ||
} | ||
|
||
#clone-button, #reset-button { | ||
margin: 10px; | ||
padding: 10px 20px; | ||
font-size: 16px; | ||
cursor: pointer; | ||
} | ||
|
||
#clone-button { | ||
background-color: #61dafb; | ||
border: none; | ||
color: #282c34; | ||
} | ||
|
||
#reset-button { | ||
background-color: #ff6f61; | ||
border: none; | ||
color: #fff; | ||
} |