-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmole.js
176 lines (168 loc) · 5.78 KB
/
mole.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
//atom.input.bind(atom.key.LEFT_ARROW, 'left');
game = Object.create(Game.prototype);
game.keys = ['A', 'S', 'D', 'F'];
for (var i = 0; i < game.keys.length; i++){
atom.input.bind(atom.key[game.keys[i]], game.keys[i]);
}
atom.currentMoleTime = 0;
atom.tillNewMole = 2;
game.update = function(dt) {
atom.currentMoleTime = atom.currentMoleTime + dt;
if (atom.currentMoleTime > atom.tillNewMole){
game.activeMole = Math.floor(Math.random()*4);
atom.currentMoleTime = 0;
if (game.bop.bopped === false){
game.bop.total = game.bop.total - 1;
} else {
game.bop.bopped = false;
}
};
for (var i = 0; i<game.keys.length; i++){
if (atom.input.pressed(game.keys[i])){
game.bop.with_key(game.keys[i]);
}
};
};
game.bop = {
bopped: true,
total: 0,
draw: function(){
atom.context.fillStyle = '#000';
atom.context.font = '130px monospace';
atom.context.fillText('Score: ' + this.total, 300, 200);
},
with_key: function(key){
if (!!(game.activeMole + 1) === true && key === game.holes[game.activeMole].label){
this.total = this.total + 1;
game.activeMole = -1;
this.bopped = true;
} else {
this.total = this.total - 1;
}
}
};
game.draw = function(){
this.drawBackgound();
for (var i = 0; i < game.holes.length; i++){
if (i === game.activeMole) {
game.holes[i].active = true;
} else {
game.holes[i].active = false;
}
game.holes[i].draw();
}
this.bop.draw();
};
game.drawHoles = function(holeLabel, xOffset, yOffset){
for(i = 0; i<holeLabel.length; i++){
atom.context.fillStyle = game.hole.color;
var holeLocation = [xOffset + game.hole.spacing*i, yOffset];
game.hole.draw(holeLocation, holeLabel[i]);
}
};
game.mole = {
size: 40,
noseSize: 8,
color: '#557',
noseColor: '#c55',
eyeSize: 5,
eyeOffset: 10,
eyeColor: '#000',
draw: function(xPosition, yPosition) {
this.drawHead(xPosition, yPosition);
this.drawEyes(xPosition, yPosition);
this.drawNose(xPosition, yPosition);
this.drawWhiskers(xPosition, yPosition);
},
drawHead: function(xPosition, yPosition) {
atom.context.beginPath();
atom.context.fillStyle = this.color;
atom.context.arc(xPosition, yPosition, this.size, 0, Math.PI*2 );
atom.context.fill();
},
drawNose: function(xPosition, yPosition) {
atom.context.beginPath();
atom.context.fillStyle = this.noseColor;
atom.context.arc(xPosition, yPosition, this.noseSize, 0, Math.PI*2 );
atom.context.fill();
},
drawEyes: function(xPosition, yPosition) {
atom.context.beginPath();
atom.context.fillStyle = this.eyeColor;
atom.context.arc(xPosition + this.eyeOffset, yPosition - this.eyeOffset, this.eyeSize, 0, Math.PI*2 );
atom.context.fill();
atom.context.beginPath();
atom.context.fillStyle = this.eyeColor;
atom.context.arc(xPosition - this.eyeOffset, yPosition - this.eyeOffset, this.eyeSize, 0, Math.PI*2 );
atom.context.fill();
},
drawWhiskers: function(xPosition, yPosition) {
atom.context.beginPath();
atom.context.moveTo(xPosition - 10, yPosition);
atom.context.lineTo(xPosition - 30, yPosition);
atom.context.moveTo(xPosition + 10, yPosition);
atom.context.lineTo(xPosition + 30, yPosition);
atom.context.moveTo(xPosition - 10, yPosition +5);
atom.context.lineTo(xPosition - 30, yPosition+ 10);
atom.context.moveTo(xPosition + 10, yPosition+ 5);
atom.context.lineTo(xPosition + 30, yPosition+ 10);
atom.context.stroke();
}
};
game.hole = {
size: 40,
spacing: 280,
color: '#311',
labelOffset: 140,
labelColor: '#000',
labelFont: "130px monospace",
moleOffset: 20,
draw: function() {
this.drawHole();
this.drawLabel();
if (this.active === true){
this.drawMole(this.holeLocation[0], this.holeLocation[1] - this.moleOffset);
};
},
drawHole: function() {
atom.context.fillStyle = this.color;
atom.context.beginPath();
atom.context.arc(this.holeLocation[0], this.holeLocation[1], this.size, 0, Math.PI*2 , false);
atom.context.fill();
},
drawLabel: function() {
atom.context.fillStyle = this.labelColor;
atom.context.font = this.labelFont;
atom.context.fillText(this.label, this.holeLocation[0] - this.size, this.holeLocation[1] + this.labelOffset);
},
drawMole: function(xPosition, yPosition){
game.mole.draw(xPosition, yPosition);
}
};
game.makeHoles = function(labels, xOffset, yOffset) {
game.holes = [];
for(var i = 0; i <labels.length; i++){
var newHole = Object.create(game.hole);
newHole.holeLocation = [xOffset + game.hole.spacing*i, yOffset];
newHole.label = labels[i];
game.holes.push(newHole);
};
};
game.drawBackgound = function() {
atom.context.beginPath();
atom.context.fillStyle = '#34e';
atom.context.fillRect(0, 0, atom.width, atom.height/2);
atom.context.fillStyle = '#ee3';
atom.context.arc(140, atom.height/2 - 30, 90, Math.PI*2, 0);
atom.context.fill();
atom.context.fillStyle = '#2e2';
atom.context.fillRect(0, atom.height/2, atom.width, atom.height/2);
};
window.onblur = function() {
return game.stop();
};
window.onfocus = function() {
return game.run();
};
game.makeHoles(game.keys, 145, atom.height/2 + 85);
game.run();