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
|
$(document).ready(function(){
var k = $.karma ({container: "#karma-main", lang: "es-MX"});
k.init({
images: [
{id: "ball", file: "ball37px.png", localized : false },
{id: "balloon", file: "balloon37px.png", localized : false },
{id: "banana", file: "banana37px.png", localized : false },
{id: "chilli", file: "chilli.png", localized : false },
{id: "fish" , file: "fish64px.png", localized : false },
{id: "flower", file: "flower37px.png", localized : false },
{id: "happyMonkey", file: "happyMonkey.jpg", localized : false },
{id: "scorebox", file: "scorebox.png", localized : false }
]
,
sounds: [
{id: "correct", file: "correct.ogg"},
{id: "incorrect", file: "incorrect.ogg"},
{id: "trigger", file: "trigger.ogg", localized: false}
]
});
k.main(function() {
var topLtCanvas = document.getElementById("topLtCanvas")
var topLtCtx = topLtCanvas.getContext('2d');
var topRtCanvas = document.getElementById("topRtCanvas")
var topRtCtx = topRtCanvas.getContext('2d');
var bottomLtCanvas = document.getElementById("bottomLtCanvas")
var bottomLtCtx = bottomLtCanvas.getContext('2d');
var bottomMdCanvas = document.getElementById("bottomMdCanvas")
var bottomMdCtx = bottomMdCanvas.getContext('2d');
var bottomRtCanvas = document.getElementById("bottomRtCanvas")
var bottomRtCtx = bottomRtCanvas.getContext('2d');
var scoreboxCanvas = document.getElementById('scoreboxCanvas');
var scoreboxCtx = scoreboxCanvas.getContext('2d');
var timerCanvas = document.getElementById('timerCanvas');
var timerCtx = timerCanvas.getContext('2d');
var actionContexts = [ topLtCtx, topRtCtx,
bottomLtCtx, bottomMdCtx, bottomRtCtx];
var actionCanvases = [ topLtCanvas, topRtCanvas,
bottomLtCanvas, bottomMdCanvas, bottomRtCanvas];
var imgNames = ["ball", "banana", "balloon","chilli", "fish", "flower"];
//game logic
var total, level=0, time, n0, n1, correct;
var maskd=200;
var d=160;
var choices=[];
var score = 0;
var endTimerX = 80;
var startTimerY = 10;
var endTimerY = 100;
var offsetTimerY = 20;
var timerId;
var timerFn = function () {
timerCanvas.setAttribute("width", "100%");
timerCtx.fillStyle = "#fff";
timerCtx.fillRect(10, startTimerY, endTimerX, offsetTimerY);
if ( startTimerY >= endTimerY ){
//make trigger sound
//answer(false);
game();
}
else {
timerCanvas.setAttribute("width", "100%");
startTimerY = startTimerY + offsetTimerY;
timerCtx.fillStyle = "#fff";
timerCtx.fillRect(10, startTimerY, endTimerX, offsetTimerY);
}
};
/* var resetTimer = function () {
timerCanvas.setAttribute("width", "100%");
startTimerY = 10;
timerCtx.fillStyle = "#fff";
timerCtx.fillRect(1000, startTimerY, endTimerX, offsetTimerY);
timerId = setInterval (timerFn, 1200);
};
*/
function game () {
$.each(actionContexts, function () {
this.clearRect(0, 0, 200, 200);
this.fillStyle = "#fff";
});
total = k.math.rand( 3, 9 ); //the total
n0 = total - k.math.rand(1, total - 1 ); //first number
n1 = total - n0; //second number
for (var i=0; i<3; i++) {
choices[ i ] = k.math.rand( 3, 9 ); // generate the 3 options
}
//chose one option (the correct option) and then put the correct value into it
correct = k.math.rand( 0, 2 );
choices[ correct ] = total;
var imgId = imgNames[ level ] ;
var card = function (ctx, n, minx, miny, d ) {
ctx.save();
var r = k.rectangle({x:minx, y:miny, width:maskd, height:maskd,
stroke:false,fill:false}).draw(ctx);
//do the clip
ctx.clip();
var pos = [];
var x, y, flag;
for (var i=0; i<n; i++) {
do {
flag = false;
x = k.math.rand( 0, d );
y = k.math.rand( 0, d );
for ( var j=0; j<pos.length; j++) {
if ( k.geometry.distance2( pos[j], {"x": x, "y": y} ) < 160 ) {
flag = true;
break;
}
}
}while ( flag === true );
pos.push( { "x":x, "y": y } );
k.library.images[ imgId ].draw(ctx, x, y )
}
ctx.restore();
}
//put the cards
card(topLtCtx, n0 , 0, 0, d);
card(topRtCtx, n1 , 0, 0, d);
card(bottomLtCtx, choices[ 0 ] , 0, 0, d);
card(bottomMdCtx, choices[ 1 ] , 0, 0, d);
card(bottomRtCtx, choices[ 2 ] , 0, 0, d);
if (!timerId){
timerId = setInterval (timerFn, 1200);
}
}
var writeScore = function (){
scoreboxCanvas.setAttribute("width", "100%");
scoreboxCtx.font = "bold 50px sans-serif";
scoreboxCtx.fillStyle = "#fff";
scoreboxCtx.textBaseline = "middle";
scoreboxCtx.fillText("" + score, 30, 100);
};
var answer = function (correct) {
if ( correct === false) {
// stop timer
//decrement score
//draw score
//animate sad monkey
//say u got it wrong
//freezeTimer
} else {
//stop timer
//increment score
}
};
writeScore();
//put the buttons
var buttons=[];
buttons[ 0 ] = { "canvas": bottomLtCanvas, "id": 0};
buttons[ 1 ] = { "canvas": bottomMdCanvas, "id": 1};
buttons[ 2 ] = { "canvas": bottomRtCanvas, "id": 2};
$.each(buttons, function( key, item ) {
item.canvas.addEventListener('click', function( ev ) {
if ( choices[ item.id ] === total){
clearInterval(timerId);
score = score + 1;
writeScore();
k.library.sounds[ "correct" ].play();
//animateMonkey(true);
level = (level+1)% imgNames.length;
}else {
clearInterval(timerId);
score = score - 1;
writeScore();
k.library.sounds[ "incorrect" ].play();
//animateMonkey(false);
}
game();
}, true);
});
game();
//end of Karma.main
});
//end of ready
});
|