-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
165 lines (136 loc) · 3.93 KB
/
main.js
File metadata and controls
165 lines (136 loc) · 3.93 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
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
// Game Options
var game = {
speed : 30,
width: 56,
height: 46,
score: 0,
tick: 0,
moves : 0,
gap : .5,
hits: 3,
level : 1
}
// Chatacters used in display
var characters = {
wall : '▓',
hole : ' ',
man : '😁',
deadMan : '😖',
cherry : '$'
}
var currentKey = false; // Left or right key
var manPosition = game.width / 2;
var gapWidth = game.width * game.gap; // gap to fall down
var gapOffset = gapWidth / 2;
var gapOffsetVaiance = 2; // gap modifier
var wallArray = makeInitalWallArray();
var textArea = document.querySelector('#game');
function makeInitalWallArray() {
var top = Array
.apply(0, Array(game.height - 10))
.map(function() { return [0,game.width] });
var bottom = Array
.apply(0, Array(10))
.map(function() { return [gapOffset,gapWidth] });
return top.concat(bottom)
}
/**
* Main render function
* Calls self
* @return null
*/
function render() {
var i = 0;
var outputArray = [];
var newOffset = Math.random() > .5 ? gapOffset + gapOffsetVaiance : gapOffset - gapOffsetVaiance;
var endGame = false;
/**
* Makes a line of text from array generation and joins
* @param {num} gapOffset gap position
* @param {num} gapWidth gap size
* @return {String} current wall state
*/
function drawWall(gapOffset, gapWidth) {
var go = gapOffset;
var gw = gapWidth;
var line = "";
line += Array(go).join(characters.wall);
line += Array(gw).join(characters.hole);
line += Array(game.width - go - gw).join(characters.wall);
return line;
}
// difficulty
levelCheck();
// scores
game.tick++;
// move guy
if(currentKey && manPosition + currentKey > 0 && manPosition + currentKey < game.width) {
manPosition += currentKey;
}
// render last state
for (i; i < wallArray.length; i++) {
outputArray[i] = drawWall(wallArray[i][0], wallArray[i][1]);
};
// inject guy
if(outputArray[game.height / 2].charAt(manPosition) !== characters.hole) {
game.hits--;
outputArray[game.height / 2] = outputArray[game.height / 2].replaceAt(manPosition, characters.deadMan);
if(game.hits === 0) {
endGame = true;
outputArray.push('xxx GAME OVER xxx');
}
} else {
outputArray[game.height / 2] = outputArray[game.height / 2].replaceAt(manPosition, characters.man);
}
outputArray.push('\nLIVES: '+ game.hits + " " + 'LEVEL : ' + game.level + " " + 'SCORE : ' + game.tick);
// draw text
textArea.textContent = outputArray.join('\n');
// new walls
wallArray.shift();
if(newOffset < 1 || newOffset + gapWidth >= game.width - 1) {
wallArray.push(wallArray[wallArray.length - 1]);
} else {
wallArray.push([newOffset, gapWidth]);
}
gapOffset = wallArray[wallArray.length - 1][0];
if(!endGame) {
// repeat
window.setTimeout(render, game.speed);
}
}
/**
* Update game settings mid render cycle
*/
function levelCheck() {
if(game.tick > 0 && game.tick % 100 == 0 && gapWidth > 8) {
gapWidth -= 2;
game.level++;
game.score += 20;
}
if(game.tick > 0 && game.tick % 50 == 0 && game.speed > 10) {
game.speed = game.speed * .95;
game.level++;
game.score += 10;
}
}
// Needed in render, could be better :/
String.prototype.replaceAt = function(index, character) {
return this.substr(0, index) + character + this.substr(index+character.length);
}
// Record keypresses
window.addEventListener('keydown', function(e) {
var kk = e.keyCode;
// left
if(kk == 37 || kk == 65) {
currentKey = -1;
}
// right
if(kk == 39 || kk == 68) {
currentKey = +1;
}
});
window.addEventListener('keyup', function(e) {
currentKey = false;
});
// start
render();