-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtron2.html
251 lines (216 loc) · 7.85 KB
/
tron2.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>TRON</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.0.0/p5.js"></script>
<script src="https://unpkg.com/[email protected]/dist/p5.ble.js" type="text/javascript"></script>
<script src=https://MolleIndustria.GitHub.io/p5.play/lib/p5.play.js></script>
</head>
<body>
<h1>TRON Sample 2</h1>
<script>
var fill_color;
var dot;
var txt;
var spr;
var clear_bg;
const sprite_size = 5;
var canvas;
var gameOver = true;
let lastTime = Date.now();
let accData =[];
var myCharacteristic;
// Micro:Bit Accelerometer service values
const serviceUuid = '6e400001-b5a3-f393-e0a9-e50e24dcca9e' ;
const characteristicUuid = '6e400002-b5a3-f393-e0a9-e50e24dcca9e';
let myBLE;
let isConnected = false;
function setup() {
canvas = createCanvas(600, 600);
textSize(20);
textAlign(CENTER, CENTER);
fill_color = color(255, 204, 0);
bg_color = color(125, 125, 125);
background(bg_color);
clear_bg = new p5.Image(1, 1);
writeColor(clear_bg, 125, 125, 125, 255);
spr = createSprite(width/2, height/2, sprite_size, sprite_size);
spr.shapeColor = fill_color;
// Create a p5ble class
myBLE = new p5ble();
createDiv();
// Create a 'Connect' button
const connectButton = createButton('Connect');
connectButton.mousePressed(connectToBle);
// Create a 'Disconnect' button
const disconnectButton = createButton('Disconnect');
disconnectButton.mousePressed(disconnectToBle);
const startButton = createButton('Start');
startButton.mousePressed(startGame);
const stopButton = createButton('Stop');
stopButton.mousePressed(stopGame);
}
function startGame() {
// STARTS THE GAME
canvas.clear();
background(bg_color);
spr = createSprite(width/2, height/2, sprite_size, sprite_size);
spr.shapeColor = fill_color;
spr.velocity.x = 1;
spr.velocity.y = 0;
gameOver = false;
}
function stopGame() {
gameOver = true;
spr.remove();
}
function draw() {
//background(125, 125, 125);
//text(spr.position,300 ,300);
//spr.shapeColor = fill_color;
drawSprites();
if (!gameOver) {
loadPixels();
if ((spr.position.x+spr.width/2 > width) || (spr.position.x - spr.width/2 < 0)){
gameOver = true;
}
if ((spr.position.y+spr.height/2 > height) || (spr.position.y - spr.height/2 < 0)){
gameOver = true;
}
var color_under;
if (spr.velocity.x == 1) {
color_under = canvas.get(spr.position.x + spr.width/2 + 1 ,spr.position.y);
}
else if (spr.velocity.x == -1) {
color_under = canvas.get(spr.position.x - spr.width/2 - 1 ,spr.position.y);
}
else if (spr.velocity.y == 1) {
color_under = canvas.get(spr.position.x, spr.position.y + spr.height/2 + 1);
}
else if (spr.velocity.y == -1) {
color_under = canvas.get(spr.position.x, spr.position.y - spr.height/2 - 1);
}
if ( comparePixels( clear_bg.pixels, color_under) ) stopGame();
//spr.position.x-spr.width/2, spr.position.y-spr.height/2, spr.width, spr.height);
}
if (!gameOver) { // manage BLE
let currTime = Date.now();
let elapsed = currTime - lastTime;
//console.log('Elapsed millis: '+ (elapsed));
if (elapsed > 333) {
/*console.log(accData, accData.length, arrAvg);
if (1 < - 0.67) {
changeDirection(spr, 0);
}
if (1 > 0.67) {
changeDirection(spr, 1);
}
*/
lastTime = currTime;
}
}
}
function keyPressed() {
// console.log('key ' + keyCode);
if (keyCode == LEFT_ARROW)
changeDirection(spr, 0);
if (keyCode == RIGHT_ARROW)
changeDirection(spr, 1);
}
function changeDirection(sprite, direction) {
if (direction === 0) {
// fill_color = color(255, 204, 0);;
if (sprite.velocity.x == 1) {
sprite.velocity.x = 0;
sprite.velocity.y = -1;
}
else if (sprite.velocity.x == -1) {
sprite.velocity.x = 0;
sprite.velocity.y = 1;
}
else if (sprite.velocity.y == 1) {
sprite.velocity.x = 1;
sprite.velocity.y = 0;
}
else if (sprite.velocity.y == -1) {
sprite.velocity.x = -1;
sprite.velocity.y = 0;
}
} else if (direction === 1) {
//fill_color = 0;
if (sprite.velocity.x == 1) {
sprite.velocity.x = 0;
sprite.velocity.y = 1;
}
else if (sprite.velocity.x == -1) {
sprite.velocity.x = 0;
sprite.velocity.y = -1;
}
else if (sprite.velocity.y == 1) {
sprite.velocity.x = -1;
sprite.velocity.y = 0;
}
else if (sprite.velocity.y == -1) {
sprite.velocity.x = 1;
sprite.velocity.y = 0;
}
}
}
function writeColor(imageIn, red, green, blue, alpha) {
for (let y=0;y<imageIn.height;y++) {
for (let x=0;x<imageIn.width;x++) {
let index = 4 * (y * imageIn.width + x);
imageIn.pixels[index] = red;
imageIn.pixels[index + 1] = green;
imageIn.pixels[index + 2] = blue;
imageIn.pixels[index + 3] = alpha;
}
}
//console.log( imageIn.pixels );
}
function comparePixels( pix1, pix2) {
var comparison = false;
for (let i=0;i<pix1.length;i++) {
if (pix1[i] != pix2[i]) comparison = true;
}
return comparison;
}
function connectToBle() {
// Connect to a device by passing the service UUID
myBLE.connect(serviceUuid, gotCharacteristics);
}
function disconnectToBle() {
// Disonnect to the device
myBLE.disconnect();
// Check if myBLE is connected
isConnected = myBLE.isConnected();
}
function onDisconnected() {
console.log('Device got disconnected.');
isConnected = false;
}
// A function that will be called once got characteristics
function gotCharacteristics(error, characteristics) {
if (error) console.log('error: ', error);
console.log('characteristics: ', characteristics);
// And a callback function to handle notifications
// myBLE.startNotifications(characteristics[0], handleNotifications);
// You can also pass in the dataType
// Options: 'unit8', 'uint16', 'uint32', 'int8', 'int16', 'int32', 'float32', 'float64', 'string'
myBLE.startNotifications(characteristics[0], handleNotifications, 'custom');
}
// A function that will be called once got characteristics
function handleNotifications(data) {
console.log('data: ', data.getUint8(0));
let val = data.getUint8()
if (val == 49) {
changeDirection(spr, 0);
}
if (val == 50) {
changeDirection(spr, 1);
}
}
</script>
</body></html>