-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple life sim (1).html
146 lines (127 loc) · 3.89 KB
/
simple life sim (1).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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Mold Growth Simulator</title>
<style>
html, body, canvas {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let moldCells = [];
let redCell;
let time = 0;
// Set up canvas
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Define MoldCell class
class MoldCell {
constructor(x, y, size, growthRate) {
this.x = x;
this.y = y;
this.size = size;
this.growthRate = growthRate;
this.color = 'rgb(0, ' + Math.floor(255 - (this.size * 20)) + ', 0)';
}
grow() {
this.size += this.growthRate;
this.color = 'rgb(0, ' + Math.floor(255 - (this.size * 20)) + ', 0)';
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
ctx.fillStyle = this.color;
ctx.fill();
}
}
// Define RedCell class
class RedCell {
constructor(x, y, size, speed) {
this.x = x;
this.y = y;
this.size = size;
this.speed = speed;
}
move() {
if (this.x < canvas.width / 2) {
this.x += this.speed;
} else {
this.x -= this.speed;
}
if (this.y < canvas.height / 2) {
this.y += this.speed;
} else {
this.y -= this.speed;
}
}
eat() {
for (let i = moldCells.length - 1; i >= 0; i--) {
const moldCell = moldCells[i];
const dx = moldCell.x - this.x;
const dy = moldCell.y - this.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < this.size + moldCell.size) {
moldCells.splice(i, 1);
}
}
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI);
ctx.fillStyle = 'red';
ctx.fill();
}
}
// Create initial mold cells
for (let i = 0; i < 5000; i++) {
const x = Math.floor(Math.random() * canvas.width);
const y = Math.floor(Math.random() * canvas.height);
const size = Math.random() * 5 + 5;
const growthRate = Math.random() * 0.01 + 0.01;
moldCells.push(new MoldCell(x, y, size, growthRate));
}
// Create red cell
redCell = new RedCell(canvas.width / 2, canvas.height / 2, 50, 2);
// Animation
function animate() {
// Clear canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Move and eat with red cell
redCell.move();
redCell.eat();
// Draw mold cells
for (let i = 0; i < moldCells.length; i++) {
const moldCell = moldCells[i];
moldCell.grow();
moldCell.draw();
}
// Draw red cell
redCell.draw();
// Regrow mold cells over time
if (time % 60 === 0) {
for (let i = 0; i < 100; i++) {
const x = Math.floor(Math.random() * canvas.width);
const y = Math.floor(Math.random() * canvas.height);
const size = Math.random() * 5 + 5;
const growthRate = Math.random() * 0.01 + 0.01;
moldCells.push(new MoldCell(x, y, size, growthRate));
}
}
time++;
// Call animate function again
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>