-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
141 lines (132 loc) Β· 5.1 KB
/
index.html
File metadata and controls
141 lines (132 loc) Β· 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Unicorn Race to the Rainbow</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background: linear-gradient(to right, #91181c, #fecfef);
font-family: 'Comic Sans MS', Impact, Haettenschweiler, 'Arial Narrow Bold', sans-serif;
flex-direction: column;
overflow: hidden;
}
.game-box {
position: relative;
width: 400px;
height: 500px;
background: white;
border-radius: 20px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
overflow: hidden;
}
.unicorn {
position: absolute;
width: 60px;
height: 60px;
background: url('https://cdn-icons-png.flaticon.com/512/3463/3463446.png') no-repeat center/cover;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
}
.star, .cloud {
position: absolute;
width: 40px;
height: 40px;
}
.star {
background: url('https://cdn-icons-png.flaticon.com/512/1828/1828884.png') no-repeat center/cover;
}
.cloud {
background: url('https://cdn-icons-png.flaticon.com/512/414/414927.png') no-repeat center/cover;
}
h2, p {
color: white;
}
.game-over, .win-message {
display: none;
font-size: 24px;
color: white;
background: rgba(0, 0, 0, 0.7);
padding: 20px;
border-radius: 10px;
text-align: center;
}
</style>
</head>
<body>
<h2>Unicorn Race to the Rainbow! π¦π</h2>
<div class="game-box" id="game-box">
<div class="unicorn" id="unicorn"></div>
</div>
<p id="message">Collect 10 stars β and avoid clouds βοΈ!</p>
<p class="game-over" id="game-over">Game Over π<br><button onclick="restartGame()">Restart</button></p>
<p class="win-message" id="win-message">You reached the rainbow! π<br><button onclick="restartGame()">Play Again</button></p>
<script>
const gameBox = document.getElementById("game-box");
const unicorn = document.getElementById("unicorn");
const message = document.getElementById("message");
const gameOverMessage = document.getElementById("game-over");
const winMessage = document.getElementById("win-message");
let unicornX = gameBox.clientWidth / 2 - 30;
unicorn.style.left = `${unicornX}px`;
let score = 0;
let lives = 3;
document.addEventListener("keydown", function(event) {
if (event.key === "ArrowLeft" && unicornX > 0) {
unicornX -= 20;
} else if (event.key === "ArrowRight" && unicornX < gameBox.clientWidth - 60) {
unicornX += 20;
}
unicorn.style.left = `${unicornX}px`;
});
function spawnItem(type) {
const item = document.createElement("div");
item.classList.add(type);
let itemX = Math.random() * (gameBox.clientWidth - 40);
item.style.left = `${itemX}px`;
gameBox.appendChild(item);
let itemY = 0;
let fallInterval = setInterval(() => {
itemY += 5;
item.style.top = `${itemY}px`;
if (itemY > gameBox.clientHeight) {
clearInterval(fallInterval);
item.remove();
}
if (itemY > gameBox.clientHeight - 70 && itemX > unicornX - 20 && itemX < unicornX + 60) {
clearInterval(fallInterval);
item.remove();
if (type === "star") {
score++;
message.textContent = "Score: " + score + " β";
if (score >= 10) {
winMessage.style.display = "block";
gameBox.style.display = "none";
}
} else if (type === "cloud") {
lives--;
message.textContent = "Lives: " + lives + " π";
if (lives <= 0) {
gameOverMessage.style.display = "block";
gameBox.style.display = "none";
}
}
}
}, 30);
}
function startGame() {
setInterval(() => spawnItem("star"), 1500);
setInterval(() => spawnItem("cloud"), 2500);
}
function restartGame() {
location.reload();
}
startGame();
</script>
</body>
</html>