-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameLogic.js
More file actions
135 lines (109 loc) · 3.77 KB
/
GameLogic.js
File metadata and controls
135 lines (109 loc) · 3.77 KB
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
/*
========================================
========== An AddingGame ==========
========== Created by: ==========
========== Jack Reinieren ==========
========================================
*/
// ========== Global variables ==========
var imgs = ["0.png", "1.png", "2.png", "3.png", "4.png", "5.png", "6.png", "7.png", "8.png", "9.png", "10.png"];
var Loc = ''; // PLEASE ADD YOUR OWN FILE LOCATION HERE
var loadedImg = [];
var nextNumbers = true;
var total;
var nr1;
var nr2;
var noLoops = false; //noLoop is a P5 function, so that is why I settled for noLoops
var goed; // "goed" means "correct" in Dutch
var fout; // "fout" means "incorrect" in Dutch
// ========== Functions ==========
function gameLoop () {
this.displayHeader = function() {
var heading = document.getElementById('heading')
heading.innerHTML = '<h1>What is: ' + nr1 + " + " +nr2 + "?</h1>"
}
this.loadImages = function() {
for (var i = 0; i < imgs.length; i++) {
loadedImg[i] = loadImage(Loc + '/img/' + imgs[i]);
}
}
this.randomInt = function(min,range) {
return Math.floor((Math.random()*(range))+min)
}
this.randomNrs = function () {
nr1 = this.randomInt(0,imgs.length);
nr2 = this.randomInt(0,imgs.length);
}
this.randomize = function() {
if(nextNumbers === true) {
do {
} while (nr1 + nr2 > 10)
console.log("numbers: "+ nr1 + " " + " + " + nr2)
nextNumbers = false;
}
}
this.randomize();
this.displayImages = function () {
img1 = image(loadedImg[nr1], -150,0);
img2 = image(loadedImg[nr2], -300 +(width / 2), 0);
}
this.escPressed = function() {
document.onkeydown = function(evt) {
evt = evt || window.event;
if (evt.keyCode == 27) {
alert('Esc key pressed.');
noLoop();
}
}
}
this.goodOrNot = function() {
if (nr1 + nr2 == total) {
goed.play();
goed.onended(function(){ //called because otherwise the mushrooms will change BEFORE the complete soundfile is played.
nextNumbers=true;
});
} else {
fout.play();
}
noLoops = false;
}
}
//========== Functions not directly related to the loop for the Game ==========
function speechRec (){
var recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition || window.mozSpeechRecognition || window.msSpeechRecognition)();
var transcript;
var property = document.getElementById('SpeechRec_On');
recognition.lang = 'nl-NL';
recognition.maxAlternatives = 2;
recognition.start();
property.style.backgroundColor = '#ef0000';
console.log(property.style.backgroundColor)
recognition.onresult = function(event) {
var interimTransc = '';
for(var i = event.resultIndex; i < event.results.length; i++) {
transcript = event.results[i][0].transcript;
if(event.results[i].isFinal) {
if (transcript.includes("een")) {
transcript = transcript.replace("een", 1);
} else if (transcript.includes("vier")) {
transcript = transcript.replace("vier", 4)
console.log("replace vier: " + transcript)
}
transcript = transcript.replace(/\D/g,'');
} else {
interimTransc += transcript;
}
property.style.backgroundColor = "";
total = transcript;
console.log("total is: " + total)
noLoops = true;
}
}
}
function LoadSounds () {
var goedAntwoord = '00_Base_GoedAntwoord.ogg';
var foutAntwoord = '00_Base_FoutAntwoord.ogg';
goed = loadSound(Loc + '/snd/' + goedAntwoord);
fout = loadSound(Loc + '/snd/'+ foutAntwoord);
}
// ================================= End of all Functions ========================================