-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealmtime-nodemcu-snakegame-sketch.ino
312 lines (297 loc) · 7.35 KB
/
realmtime-nodemcu-snakegame-sketch.ino
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WebSocketsServer.h>
const char* ssid = "MiniProject";
const char* password = "911911911";
const char index_html[] PROGMEM = R"html(
<!DOCTYPE html>
<html>
<head>
<title>NodeMCU Snake Game</title>
<style>
body {
text-align: center;
font-family: Arial, sans-serif;
background-color: #1e3c72;
color: white;
}
canvas {
border: 3px solid #333;
background-color: #e0e0e0;
margin-top: 20px;
}
h1 {
color: #f39c12;
}
#score {
color: #f39c12;
font-size: 20px;
}
</style>
</head>
<body>
<h1>NodeMCU Snake Game</h1>
<div id="score">Score: 0 | High Score: 0</div>
<canvas id="gameCanvas" width="640" height="480"></canvas>
<script>
const ws = new WebSocket('ws://192.168.4.1:81');
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreDiv = document.getElementById('score');
let highScore = 0;
ws.onmessage = (event) => {
const gameState = JSON.parse(event.data);
drawGame(gameState);
updateScore(gameState.score, gameState.highScore);
};
function drawGame(gameState) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'green';
for (let i = 0; i < gameState.snake.length; i++) {
const segment = gameState.snake[i];
ctx.fillRect(segment.x * 20, segment.y * 20, 20, 20);
}
ctx.fillStyle = 'red';
ctx.beginPath();
const foodX = gameState.food.x * 20 + 10;
const foodY = gameState.food.y * 20 + 10;
ctx.arc(foodX, foodY, 10, 0, Math.PI * 2);
ctx.fill();
}
function updateScore(score, highScore) {
scoreDiv.textContent = `Score: ${score} | High Score: ${highScore}`;
}
</script>
</body>
</html>
)html";
const char controller_html[] PROGMEM = R"html(
<!DOCTYPE html>
<html>
<head>
<title>Snake Game Controller</title>
<style>
body {
font-family: 'Arial', sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background: linear-gradient(to bottom, #1e3c72, #2a5298);
margin: 0;
}
.container {
text-align: center;
background-color: rgba(255, 255, 255, 0.9);
border-radius: 15px;
padding: 40px;
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
width: 320px;
}
h1 {
color: #333;
font-size: 28px;
margin-bottom: 20px;
}
button {
background-color: #3498db;
color: white;
border: none;
padding: 25px;
font-size: 24px;
margin: 10px;
border-radius: 50%;
cursor: pointer;
transition: all 0.3s ease;
box-shadow: 0 6px 15px rgba(52, 152, 219, 0.3);
}
button:hover {
background-color: #2980b9;
transform: translateY(-4px);
box-shadow: 0 8px 20px rgba(52, 152, 219, 0.5);
}
button:active {
transform: translateY(2px);
box-shadow: 0 3px 10px rgba(52, 152, 219, 0.4);
}
.controls {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 15px;
grid-template-rows: 1fr 1fr 1fr;
margin-top: 20px;
justify-items: center;
}
.controls button {
width: 80px;
height: 80px;
}
.up {
grid-column: 2;
grid-row: 1;
}
.down {
grid-column: 2;
grid-row: 3;
}
.left {
grid-column: 1;
grid-row: 2;
}
.right {
grid-column: 3;
grid-row: 2;
}
</style>
</head>
<body>
<div class="container">
<h1>Snake Game Controller</h1>
<div class="controls">
<button class="up" onclick="sendCommand('up')">↑</button>
<button class="left" onclick="sendCommand('left')">←</button>
<button class="right" onclick="sendCommand('right')">→</button>
<button class="down" onclick="sendCommand('down')">↓</button>
</div>
</div>
<script>
const ws = new WebSocket('ws://192.168.4.1:81');
function sendCommand(command) {
ws.send(command);
}
</script>
</body>
</html>
)html";
ESP8266WebServer server(80);
WebSocketsServer webSocket(81);
struct SnakeSegment {
int x, y;
};
SnakeSegment snake[100];
int snakeLength = 1;
int snakeX, snakeY;
int foodX, foodY;
int direction = 1;
int score = 0;
int highScore = 0;
const int ledPin = LED_BUILTIN;
void setup() {
Serial.begin(115200);
WiFi.softAP(ssid, password);
pinMode(ledPin, OUTPUT);
server.on("/", HTTP_GET, []() {
server.send(200, "text/html", index_html);
});
server.on("/controller", HTTP_GET, []() {
server.send(200, "text/html", controller_html);
});
server.begin();
webSocket.begin();
webSocket.onEvent(onWebSocketEvent);
snakeX = 10;
snakeY = 10;
generateFood();
}
void loop() {
server.handleClient();
webSocket.loop();
unsigned long currentMillis = millis();
static unsigned long previousMillis = 0;
const long interval = 16;
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
updateGame();
sendGameState();
}
blinkLED();
}
void updateGame() {
static unsigned long lastMoveTime = 0;
unsigned long currentMillis = millis();
if (currentMillis - lastMoveTime >= 400) {
lastMoveTime = currentMillis;
for (int i = snakeLength - 1; i > 0; i--) {
snake[i].x = snake[i - 1].x;
snake[i].y = snake[i - 1].y;
}
switch (direction) {
case 1:
snake[0].x++;
break;
case 2:
snake[0].y++;
break;
case 3:
snake[0].x--;
break;
case 4:
snake[0].y--;
break;
}
if (snake[0].x == foodX && snake[0].y == foodY) {
snakeLength++;
score++;
if (score > highScore) {
highScore = score;
}
generateFood();
}
for (int i = 1; i < snakeLength; i++) {
if (snake[0].x == snake[i].x && snake[0].y == snake[i].y) {
resetGame();
}
}
if (snake[0].x < 0 || snake[0].x >= 32 || snake[0].y < 0 || snake[0].y >= 24) {
resetGame();
}
}
}
void generateFood() {
foodX = random(0, 32);
foodY = random(0, 24);
}
void resetGame() {
score = 0;
snakeLength = 1;
snake[0].x = 10;
snake[0].y = 10;
generateFood();
}
void sendGameState() {
String gameState = "{\"snake\":[";
for (int i = 0; i < snakeLength; i++) {
gameState += "{\"x\":" + String(snake[i].x) + ",\"y\":" + String(snake[i].y) + "}";
if (i < snakeLength - 1) {
gameState += ",";
}
}
gameState += "],\"food\":{\"x\":" + String(foodX) + ",\"y\":" + String(foodY) + "},";
gameState += "\"score\":" + String(score) + ",";
gameState += "\"highScore\":" + String(highScore) + "}";
webSocket.broadcastTXT(gameState);
}
void blinkLED() {
static unsigned long lastBlinkTime = 0;
static bool ledState = LOW;
unsigned long currentMillis = millis();
if (currentMillis - lastBlinkTime >= 1000) {
ledState = !ledState;
digitalWrite(ledPin, ledState);
lastBlinkTime = currentMillis;
}
}
void onWebSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length) {
if (type == WStype_TEXT) {
String command = String((char*)payload);
if (command == "up") {
direction = 4;
} else if (command == "down") {
direction = 2;
} else if (command == "left") {
direction = 3;
} else if (command == "right") {
direction = 1;
}
}
}