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
|
const canvas = document.getElementById('sand');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth - 12;
canvas.height = window.innerHeight - 12;
const gridSize = 10;
const gridWidth = Math.floor(canvas.width / gridSize);
const gridHeight = Math.floor(canvas.height / gridSize);
const grid = Array(gridHeight).fill().map(() => Array(gridWidth).fill(0));
let mouseDown = false;
let playerAdded = false;
let keys = new Set();
canvas.addEventListener('mousedown', (e) => {
mouseDown = true;
addSand(e);
});
canvas.addEventListener('mouseup', () => {
mouseDown = false;
});
canvas.addEventListener('mousemove', (e) => {
if (mouseDown) {
addSand(e);
}
});
canvas.addEventListener('touchstart', (e) => {
mouseDown = true;
e.preventDefault();
addSand(e.touches[0]);
});
canvas.addEventListener('touchend', () => {
mouseDown = false;
});
canvas.addEventListener('touchmove', (e) => {
if (mouseDown) {
e.preventDefault();
addSand(e.touches[0]);
}
});
window.addEventListener('keydown', (e) => {
keys.add(e.key);
});
window.addEventListener('keyup', (e) => {
keys.delete(e.key);
});
window.addEventListener('keydown', (e) => {
if (e.key === ' ' && !playerAdded) {
addPlayer();
playerAdded = true;
}
});
window.addEventListener('keydown', (e) => {
// reset the canvas
if (e.key === 'r') {
playerAdded = false;
for (let y = 0; y < gridHeight; y++) {
for (let x = 0; x < gridWidth; x++) {
grid[y][x] = 0;
}
}
return;
}
// move the character around
for (let y = 0; y < gridHeight; y++) {
for (let x = 0; x < gridWidth; x++) {
if (grid[y][x] === 3) {
if ((e.key === 'ArrowLeft' || e.key === 'a') && x > 0 && grid[y][x - 1] === 0) {
grid[y][x] = 0;
grid[y][x - 1] = 3;
return;
} else if ((e.key === 'ArrowRight' || e.key === 'd') && x < gridWidth - 1 && grid[y][x + 1] === 0) {
grid[y][x] = 0;
grid[y][x + 1] = 3;
return;
} else if (isJumpKey(e.key) && y < gridHeight - 1 && grid[y + 1][x] !== 0) {
jumpPlayer(y, x);
return;
}
}
}
}
});
const jumpHeight = 10;
const isJumpKey = key => {
return key === ' ' || key === 'ArrowUp' || key === 'w';
}
const jumpPlayer = (y, x) => {
grid[y][x] = 0;
grid[Math.max(y - jumpHeight, 0)][x] = 3;
}
const updateGrid = () => {
for (let y = gridHeight - 2; y >= 0; y--) {
for (let x = 0; x < gridWidth; x++) {
if (grid[y][x] === 1) {
if (grid[y + 1][x] === 0) {
grid[y][x] = 0;
grid[y + 1][x] = 1;
} else if (x > 0 && grid[y + 1][x - 1] === 0) {
grid[y][x] = 0;
grid[y + 1][x - 1] = 1;
} else if (x < gridWidth - 1 && grid[y + 1][x + 1] === 0) {
grid[y][x] = 0;
grid[y + 1][x + 1] = 1;
}
} else if (grid[y][x] === 3) {
if (y < gridHeight - 1 && grid[y + 1][x] === 0) {
grid[y][x] = 0;
grid[y + 1][x] = 3;
}
}
}
}
};
const drawGrid = () => {
// clear the canvas
ctx.fillStyle = 'beige';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// draw the sand and player
ctx.fillStyle = 'black';
for (let y = 0; y < gridHeight; y++) {
for (let x = 0; x < gridWidth; x++) {
if (grid[y][x] === 1) {
ctx.fillRect(x * gridSize, y * gridSize, gridSize, gridSize);
} else if (grid[y][x] === 3) {
ctx.fillStyle = 'red';
ctx.fillRect(x * gridSize, y * gridSize, gridSize, gridSize);
ctx.fillStyle = 'black'; // Reset fill color to black after drawing the player
}
}
}
};
const framerate = 60;
let lastFrameTime = 0;
const gameLoop = (currentTime) => {
const deltaTime = (currentTime - lastFrameTime) / 1000;
if (deltaTime > 1 / framerate) {
lastFrameTime = currentTime;
updateGrid();
drawGrid();
}
requestAnimationFrame(gameLoop);
};
requestAnimationFrame(gameLoop);
function addSand(e) {
const rect = canvas.getBoundingClientRect();
const x = Math.floor((e.clientX - rect.left) / gridSize);
const y = Math.floor((e.clientY - rect.top) / gridSize);
if (x >= 0 && x < gridWidth && y >= 0 && y < gridHeight) {
grid[y][x] = 1;
}
}
function addPlayer() {
for (let y = 0; y < gridHeight; y++) {
for (let x = 0; x < gridWidth; x++) {
if (grid[y][x] === 1) {
grid[Math.max(0, y - 10)][x] = 3;
return;
}
}
}
}
|