-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
297 lines (243 loc) · 5.37 KB
/
sketch.js
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
let mobilenet;
let video;
let label='';
let prob='';
let classifier;
let button1;
let button2;
let button3;
let clicks1=0;
let clicks2=0;
let cost;
function modelready(){
console.log("Model Ready");
}
function videoready(){
console.log("Video Ready");
}
function whileTraining(loss){
if(loss==null){
console.log("Training Complete");
classifier.classify(gotresults);
}else{
console.log(loss);
cost=loss;
}
}
function gotresults(error,result){
if(error){
console.error(error);
}else{
label=result;
classifier.classify(gotresults);
}
}
function setup() {
can=createCanvas(440,300);
video=createCapture(VIDEO);
createElement('h4','Artificial Intelligence Game');
createElement('h5','Train your own gestures !');
video.hide();
background(0);
mobilenet=ml5.featureExtractor('MobileNet',modelready);
classifier=mobilenet.classification(video,videoready);
button1=createButton('Run');
button1.class('waves-effect waves-light btn-large light-blue');
button1.mousePressed(function(){
classifier.addImage('Run');
clicks1=clicks1+1;
button1.html('Run (' + clicks1 + ')');
});
button2=createButton('Jump');
button2.class('waves-effect waves-light btn-large light-blue');
button2.mousePressed(function(){
classifier.addImage('Jump');
clicks2=clicks2+1;
button2.html('Jump ('+clicks2+')');
});
button3=createButton('Train Model');
button3.class('waves-effect waves-light btn-large red');
button3.mousePressed(function(){
classifier.train(whileTraining);
});
er=createElement("h5","Loss : ");
}
// GAME
/////////////////////
//DECLARE VARIABLES//
/////////////////////
//Canvas Variables
var c;
var ctx;
//Player Variables
var img_player;
var player_x;
var player_y;
var player_width;
var player_height;
var gravity;
//Fork Variables
var img_fork;
var fork_x;
var fork_y;
var fork_width;
var fork_height;
var fork_speed;
var can_spawn;
var fork_count;
//Game State Variables
var has_started = false;
var is_playing;
var score = 0;
////////////
//Run Game//
////////////
if (has_started == false) {
Start();
}
var nextInterval = setInterval(Draw, 10);
var nextUpdate = setInterval(Update, 1);
/////////////////
//THE GAME LOOP//
/////////////////
//Run at the start of our game
function Start() {
///////////////////////
//Set Variable Values//
///////////////////////
//Canvas Values
c = document.getElementById("screen");
ctx = c.getContext("2d");
c.width = 600;
c.height = 320;
offset_x = 32;
offset_y = c.height - 128;
//Player Values
img_player = new Image();
img_player.src = "https://image.ibb.co/jUqkD5/dodo.png";
player_x = offset_x;
player_y = offset_y;
player_width = 64;
player_height = 64;
gravity = 2;
//Fork Values
img_fork = new Image();
img_fork.src = "https://image.ibb.co/d2iu6Q/fork.png";
can_spawn = true;
fork_count = 0;
//Game State Values
has_started = true;
is_playing = false;
//score = 0;
}
//Runs every frame
function Update() {
Input();
if (is_playing) {
if (can_spawn == true) {
SpawnFork();
}
if (fork_count > 0) {
CollisionHandling();
}
}
}
//Draws the graphics
function Draw() {
//Background
ctx.fillStyle = "white";
ctx.fillRect(0, 0, c.width, c.height);
ctx.fillStyle = "#03a9f4";
ctx.fillRect(0, c.height - 64, c.width, c.height);
//Player
ctx.drawImage(img_player, player_x, player_y);
//Score
ctx.font = "24px VT323";
ctx.fillText("Score: " + score,10, 30);
if (is_playing) {
if (fork_count > 0) {
//Move Fork
fork_x -= fork_speed;
//Draw Fork
ctx.drawImage(img_fork, fork_x, fork_y);
}
if (player_y < offset_y) {
//Gravity
player_y += gravity;
}
if (fork_x < -64) {
//Respawn Fork
can_spawn = true;
}
//Add Score
score += 1;
}
}
function draw(){
translate(width,0);
scale(-1.0,1.0);
image(video, 0, 0, 440,300);
fill(255);
document.getElementById("demo").innerHTML =
"Predicted : " + label;
if (label == ''){
}else if(label == 'Jump'){
if (player_y == offset_y) {
//Jump
player_y -= 150;
}
}else{
if (is_playing == false)
{
is_playing = true;
score = 0;
}
}
er.html('Loss : '+cost);
}
//Handles the input keyboard
function Input() {
window.onkeydown = function(e) {
var keyCode = e.keycode ? e.keycode : e.which;
switch (keyCode) {
case 32:
if (is_playing == false)
{
//Start the Game
is_playing = true;
score = 0;
}
else
{
if (player_y == offset_y) {
//Jump
player_y -= 150;
}
}
break;
default:
break;
}
}
}
//Spawns forks
function SpawnFork() {
fork_x = c.width;
fork_y = offset_y;
fork_width = 64;
fork_height = 64;
fork_speed = 3;
fork_count += 1;
if (fork_count > 0) {
can_spawn = false;
}
}
//Handles the collisions
function CollisionHandling() {
if (player_x < fork_x && player_x + player_width > fork_x) {
if (player_y + player_height > fork_y) {
//Restart the game
Start();
}
}
}