Skip to content

Commit b2930c2

Browse files
committed
improved design
1 parent 0a7a504 commit b2930c2

4 files changed

Lines changed: 55 additions & 16 deletions

File tree

apple.png

100 KB
Loading

index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
<p id="highscore">Highscore:</p>
1515
</div>
1616
<div id="playground">
17-
<div id="snake"></div>
17+
<div id="snake">
18+
<div class="eye"></div>
19+
<div class="eye"></div>
20+
</div>
1821
</div>
1922
</div>
2023
</body>

script.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const dimensionOfSquare = playground.clientWidth / 17;
1313

1414
let timeSinceLastMove;
1515

16+
let apple_interval;
1617
let timeouts = [];
1718
let apple;
1819
let snakeTail;
@@ -90,12 +91,12 @@ function isAppleFound() {
9091
inBound(
9192
snake.getBoundingClientRect().left,
9293
apple.getBoundingClientRect().left,
93-
dimensionOfSquare / 5
94+
dimensionOfSquare / 3
9495
) &&
9596
inBound(
9697
snake.getBoundingClientRect().top,
9798
apple.getBoundingClientRect().top,
98-
dimensionOfSquare / 5
99+
dimensionOfSquare / 3
99100
)
100101
);
101102
}
@@ -106,7 +107,7 @@ function clearTimeouts() {
106107
}
107108

108109
function renderTail() {
109-
for (let i = 1; i < prevPositions.length; i++) {
110+
for (let i = 1; i < prevPositions.length - 1; i++) {
110111
renderSingleTail(i);
111112
}
112113
}
@@ -123,11 +124,15 @@ function renderSingleTail(index) {
123124
if (snakeTails[index]) snakeTails[index].remove();
124125

125126
snakeTails[index] = document.createElement("div");
127+
console.log((index % 2) * 0.1 + 0.452);
128+
129+
snakeTails[index].style.backgroundColor =
130+
"hsla(260, 75%, 50%, " + ((index % 2) * 0.05 + 0.452) + ")";
126131

127132
snakeTails[index].style.top = snake.getBoundingClientRect().top + "px";
128133
snakeTails[index].style.left = snake.getBoundingClientRect().left + "px";
129-
snakeTails[index].style.width = dimensionOfSquare + "px";
130-
snakeTails[index].style.height = dimensionOfSquare + "px";
134+
snakeTails[index].style.width = dimensionOfSquare - 0.4 + "px";
135+
snakeTails[index].style.height = dimensionOfSquare - 0.4 + "px";
131136
snakeTails[index].classList.add("tempSnake");
132137

133138
container.appendChild(snakeTails[index]);
@@ -179,7 +184,7 @@ function snakeLeft() {
179184
onceLeft();
180185
snakeLeft();
181186
if (
182-
snake.getBoundingClientRect().left <=
187+
snake.getBoundingClientRect().left + dimensionOfSquare <=
183188
playground.getBoundingClientRect().left
184189
) {
185190
fail();
@@ -273,15 +278,29 @@ function getRandomInt(max) {
273278

274279
function spawnApple() {
275280
if (apple) apple.remove();
281+
282+
if (apple_interval) clearInterval(apple_interval);
276283
const leftBound = playground.getBoundingClientRect().left;
277284
const topBound = playground.getBoundingClientRect().top;
278285

279-
apple = document.createElement("div");
280-
apple.style.height = dimensionOfSquare + "px";
281-
apple.style.width = dimensionOfSquare + "px";
286+
//
287+
apple = document.createElement("img");
288+
// apple = document.createElement("div");
289+
290+
apple.src = "./apple.png";
291+
apple.style.height = dimensionOfSquare + 5 + "px";
292+
apple.style.width = dimensionOfSquare + 5 + "px";
282293
apple.classList.add("apple");
283294
container.append(apple);
284295

296+
apple_interval = setInterval(() => {
297+
apple.style.transform = "scale(1.5)"; // Enlarge for 500ms
298+
299+
setTimeout(() => {
300+
apple.style.transform = "scale(1)"; // Reset after 500ms
301+
}, 500);
302+
}, 2000); // Repeat every 2 seconds
303+
285304
let index = 0;
286305
let left;
287306
let top;

style.css

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
* {
22
margin: 0;
33
}
4+
body {
5+
background-color: #4a752c;
6+
}
47

58
#container {
69
height: 100vh;
@@ -12,7 +15,7 @@
1215
p {
1316
padding: 10px;
1417
font-weight: 900;
15-
color: black;
18+
color: white;
1619
font-size: 3em;
1720
font-family: monospace;
1821
}
@@ -32,18 +35,32 @@ p {
3235
flex-wrap: wrap;
3336
width: 75vh;
3437
height: 75vh;
35-
border: 1px solid black;
3638
}
3739
#snake {
40+
border-radius: 5px;
41+
display: flex;
42+
flex-direction: column;
43+
justify-content: space-around;
44+
align-items: end;
3845
position: absolute;
39-
background-color: hsla(260, 75%, 50%, 0.637);
46+
background-color: rgb(0, 17, 255);
47+
}
48+
.eye {
49+
margin: 5px;
50+
background-color: white;
51+
width: 30%;
52+
border-radius: 50px;
53+
height: 30%;
4054
}
4155
.apple {
42-
background-color: red;
56+
/* background-color: red; */
4357
position: absolute;
44-
border-radius: 50px;
58+
transition: 1s;
59+
60+
/* border-radius: 50px; */
4561
}
4662
.tempSnake {
4763
position: absolute;
48-
background-color: hsla(260, 75%, 50%, 0.637);
64+
border-radius: 15px;
65+
background-color: hsla(260, 75%, 50%, 0.452);
4966
}

0 commit comments

Comments
 (0)