forked from udacity/frontend-nanodegree-arcade-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
163 lines (135 loc) · 4.23 KB
/
app.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
const cell = {
width: 101,
height: 83
};
const numberOfRows = 6;
const numberOfColumns = 5;
const playingField = {
width: cell.width * numberOfColumns, // 505
height: cell.height * numberOfRows // 498
};
const character = {
width: 75,
height: 60
};
const offsetToTheCenter = 23;
const initialPlayerPosition = {
x: 2 * cell.width, // 202
y: 5 * cell.height - offsetToTheCenter // 392
};
const initialEnemyPosition = {
x: -2 * cell.width, // -202
y: [1, 2, 3].map(rowNumber => rowNumber * cell.height - offsetToTheCenter) // 60, 143, 226
};
const minSpeed = 1;
const maxSpeed = 1000;
const enemySpeed = Math.floor(Math.random() * (maxSpeed - minSpeed)) + minSpeed;
const desynchronizedSpeed = [0, 100, 50].map(speedDelta => enemySpeed + speedDelta);
let count = 0;
// ===========================================================
const score = document.createElement('h3');
const speed = document.createElement('h4');
const resetButton = document.createElement('button');
score.textContent = 'your score is: 0';
speed.textContent = `game speed is: ${enemySpeed}`;
resetButton.textContent = 'reset speed';
resetButton.addEventListener('click', location.reload.bind(location))
document.body.append(score, resetButton, speed);
// ===========================================================
const Enemy = function (x, y, speed, player) {
this.x = x;
this.y = y;
this.speed = speed;
this.player = player;
this.sprite = 'images/enemy-bug.png'
};
Enemy.prototype.update = function (dt) {
if (this.x <= playingField.width) {
this.x = this.speed * dt + this.x;
} else {
this.x = -2 * cell.width;
}
this.detectCollision();
};
Enemy.prototype.detectCollision = function () {
if (this.player.x <= this.x + character.width &&
this.player.x + character.width >= this.x &&
this.player.y <= this.y + character.height &&
this.player.y + character.height >= this.y) {
this.player.reset()
}
};
Enemy.prototype.render = function () {
ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
};
// ===========================================================
const Player = function (x, y) {
this.x = x;
this.y = y;
this.moveSideways = cell.width;
this.moveAhead = cell.height;
this.sprite = 'images/char-boy.png';
};
Player.prototype.update = function () {
this.scored();
};
Player.prototype.scored = function () {
if (this.y < cell.height / 2) {
count++;
score.textContent = `your score is: ${count}`;
this.x = initialPlayerPosition.x;
this.y = initialPlayerPosition.y;
}
};
Player.prototype.reset = function () {
score.textContent = 'wasted';
this.x = initialPlayerPosition.x;
this.y = initialPlayerPosition.y;
};
Player.prototype.render = function () {
ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
};
Player.prototype.handleInput = function (key) {
switch (key) {
case 'up':
this.y -= this.moveAhead;
if (this.y < 0) {
this.y = 0;
}
break;
case 'down':
this.y += this.moveAhead;
if (this.y > initialPlayerPosition.y) {
this.y = initialPlayerPosition.y;
}
break;
case 'left':
this.x -= this.moveSideways;
if (this.x < 0) {
this.x = 0;
}
break;
case 'right':
this.x += this.moveSideways;
if (this.x > playingField.width - cell.width) {
this.x = playingField.width - cell.width;
}
break;
}
};
// ===========================================================
const player = new Player(initialPlayerPosition.x, initialPlayerPosition.y);
const allEnemies = new Array(3);
for (let i = 0; i < allEnemies.length; i++) {
allEnemies[i] = new Enemy(initialEnemyPosition.x, initialEnemyPosition.y[i], desynchronizedSpeed[i], player);
}
// ===========================================================
document.addEventListener('keyup', function (e) {
let allowedKeys = {
37: 'left',
38: 'up',
39: 'right',
40: 'down'
};
player.handleInput(allowedKeys[e.keyCode]);
});