Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/examples/adding_up_to_10/js/lesson.js.~1~
blob: 6f8bc8838de9dd18a5b73179a016e77eabc4554d (plain)
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
$(document).ready(function(){

var k = $.karma ({container: "#karma-main"/*, lang: "es-MX"*/});
k.size(1200, 800);
k.init({
	images: [
		{id: "ball",   file: "ball.png",   localized : false },
		{id: "ballon", file: "ballon.png", localized : false },
		{id: "banana", file: "banana.png", localized : false },
		{id: "chilli", file: "chilli.png", localized : false },
		{id: "fish"  , file: "fish.png",   localized : false },
	    {id: "flower", file: "flower.png", localized : false },
	    {id: "plussign", file: "plussign.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" }
	]
});
k.main(function() {
	alert(gk.paths.sounds.localized);
	var imgNames = ["ball", "ballon", "banana", "chilli", "fish", "flower" ];
	//game logic
	var total, level=0, time, n0, n1, correct;
	var maskd=252;
	var d=200;
	var choices=[];
	
	function game () {
	gk.ctx.clearRect(0,0,1200,800);
	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 ] ;

	    // add plus sign, the scorebox, and the happy monkey
	    k.library.images["plussign"].draw(460,200);
	    k.library.images["happyMonkey"].draw(1000,600);
	
	var card = function ( n, minx, miny, d ) {
		gk.ctx.save();
		var r = k.rectangle({x:minx, y:miny, width:maskd, height:maskd,
			stroke:false,fill:false}).draw();
			
		//do the clip
		gk.ctx.clip();
		var pos = [];
		var x, y, flag;
		for (var i=0; i<n; i++) {
			do {
				flag = false;
				x = minx + k.math.rand( 0, d );
				y = miny + k.math.rand( 0, d );
				for ( var j=0; j<pos.length; j++) {
					if ( k.geometry.distance2( pos[j], {"x": x, "y": y} ) 
					< 4000 ) {
						flag = true;
						break;
					}
				}
				
			}while ( flag === true );
			pos.push( { "x":x, "y": y } );
			k.library.images[ imgId ].draw( x, y )
		}
		
		gk.ctx.restore();
	}
	//put the cards
	
	card( n0 , 165, 100, d);
	card( n1 , 550, 100, d);
	card( choices[ 0 ] ,  65, 480, d);
	card( choices[ 1 ] , 360, 480, d);
	card( choices[ 2 ] , 650, 480, d);
	}
	
	game();
	//put the buttons
	var buttons=[];
	buttons[ 0 ] = k.button({id: 0, x:65, y:480, width:maskd, height: maskd});
	buttons[ 1 ] = k.button({id: 1, x:360, y:480, width:maskd, height: maskd});
	buttons[ 2 ] = k.button({id: 2, x:650, y:480, width:maskd, height: maskd});
	buttons[0].onClick = buttons[1].onClick = buttons[2].onClick = function() {
		if ( choices[ this.id ] === total){
			
			k.library.sounds[ "correct" ].play();
			level = (level+1)% imgNames.length;
			game();
		}else {
			k.library.sounds[ "incorrect" ].play();
			game();
		}
	}
	
	
});

});