-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEcosimenv.html
189 lines (172 loc) · 5.83 KB
/
Ecosimenv.html
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Virtual Eco-World</title>
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #e0f2f1;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
#container {
display: flex;
width: 90%;
height: 70%;
}
canvas {
flex: 3;
border: 3px solid #333;
}
#properties {
flex: 1;
padding: 20px;
border: 3px solid #333;
margin-left: 10px;
background-color: #fff;
}
#properties p {
margin: 10px 0;
display: flex;
justify-content: space-between;
align-items: center;
}
button {
padding: 5px 10px;
}
</style>
</head>
<body>
<div id="container">
<canvas id="ecoWorld"></canvas>
<div id="properties">
<p>
Trees: <span id="treeCount">0</span>
<span>
<button onclick="addTrees()">+</button>
<button onclick="removeTrees()">-</button>
</span>
</p>
<p>
Factories: <span id="factoryCount">0</span>
<span>
<button onclick="addFactory()">+</button>
<button onclick="removeFactory()">-</button>
</span>
</p>
<p>
Fish: <span id="fishCount">10</span>
<span>
<button onclick="addFish()">+</button>
<button onclick="removeFish()">-</button>
</span>
</p>
<p>Air Quality: <span id="airQuality">100%</span></p>
<button id="speedButton" onclick="toggleSpeed()">x1 Speed</button>
</div>
</div>
<script>
const canvas = document.getElementById('ecoWorld');
const ctx = canvas.getContext('2d');
let forestSize = 200;
let oceanSize = 200;
let factoryCount = 0;
let fishCount = 10;
let birdCount = 0;
let airQuality = 100;
let speedMultiplier = 1;
let simulationInterval;
function drawWorld() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#8bc34a';
ctx.beginPath();
ctx.moveTo(0, canvas.height);
ctx.lineTo(0, canvas.height - oceanSize);
ctx.quadraticCurveTo(canvas.width / 4, canvas.height - oceanSize + 50, canvas.width / 2, canvas.height - oceanSize);
ctx.quadraticCurveTo(3 * canvas.width / 4, canvas.height - oceanSize - 50, canvas.width, canvas.height - oceanSize);
ctx.lineTo(canvas.width, canvas.height);
ctx.closePath();
ctx.fill();
ctx.fillStyle = '#2196f3';
ctx.fillRect(0, canvas.height - oceanSize, canvas.width, oceanSize);
for (let i = 0; i < fishCount; i++) {
ctx.fillStyle = 'yellow';
ctx.beginPath();
ctx.arc(100 + (i * 50), canvas.height - 50, 5, 0, 2 * Math.PI);
ctx.fill();
}
ctx.fillStyle = '#ff5722';
for (let i = 0; i < factoryCount; i++) {
ctx.fillRect(10 + (i * 60), canvas.height - oceanSize - 60, 50, 60);
}
for (let i = 0; i < birdCount; i++) {
ctx.fillStyle = 'black';
ctx.beginPath();
ctx.arc(50 + (i * 70), 50, 10, 0, Math.PI, true);
ctx.fill();
}
ctx.fillStyle = `rgba(255, 0, 0, ${1 - airQuality / 100})`;
ctx.fillRect(0, 0, canvas.width, canvas.height);
document.getElementById('treeCount').innerText = forestSize;
document.getElementById('factoryCount').innerText = factoryCount;
document.getElementById('fishCount').innerText = fishCount;
document.getElementById('airQuality').innerText = `${airQuality}%`;
}
function addTrees() {
forestSize += 10;
birdCount++;
drawWorld();
}
function removeTrees() {
forestSize = Math.max(0, forestSize - 10);
birdCount = Math.max(0, birdCount - 1);
drawWorld();
}
function addFactory() {
factoryCount++;
if (factoryCount % 3 === 0) {
oceanSize += 20;
fishCount -= 2;
airQuality -= 5;
}
drawWorld();
}
function removeFactory() {
factoryCount = Math.max(0, factoryCount - 1);
drawWorld();
}
function addFish() {
fishCount++;
drawWorld();
}
function removeFish() {
fishCount = Math.max(0, fishCount - 1);
drawWorld();
}
function startSimulation() {
if (simulationInterval) clearInterval(simulationInterval);
simulationInterval = setInterval(() => {
if (Math.random() < 0.1 * speedMultiplier) {
addFactory();
}
}, 1000);
}
function toggleSpeed() {
if (speedMultiplier === 1) {
speedMultiplier = 10;
document.getElementById('speedButton').innerText = 'x10 Speed';
} else {
speedMultiplier = 1;
document.getElementById('speedButton').innerText = 'x1 Speed';
}
startSimulation();
}
startSimulation();
</script>
</body>
</html>