-
-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathapp.js
More file actions
156 lines (137 loc) · 3.22 KB
/
Copy pathapp.js
File metadata and controls
156 lines (137 loc) · 3.22 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
const water_edge = 52;
// Enemies our player must avoid
class Enemy {
constructor({ x, y, speedX, initialWidth, player }) {
this.initialWidth = initialWidth + 100;
this.x = x;
this.y = y;
this.speedX = speedX;
this.height = 40;
this.width = 101;
this.player = player;
this.sprite = "images/enemy-bug.png";
}
checkCollision() {
if (
this.x + this.width > this.player.x &&
this.player.x + this.player.width > this.x &&
this.player.y > this.y - this.height &&
this.player.y - this.player.height < this.y
) {
this.player.lose();
}
}
update(dt) {
this.checkCollision();
if (this.x >= this.initialWidth) {
this.x = -101;
}
this.x += dt * this.speedX;
}
render() {
ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
}
}
// Now write your own player class
// This class requires an update(), render() and
// a handleInput() method.
class Player {
constructor({ initialWidth, initialHeight, speedX, speedY }) {
this.initialWidth = initialWidth;
this.initialHeight = initialHeight;
this.x = this.initialWidth / 2;
this.y = this.initialHeight;
this.height = 30;
this.width = 30;
this.speedX = speedX;
this.speedY = speedY;
this.sprite = "images/char-boy.png";
}
update() {
if (this.y < 0) {
this.resetPosition();
}
}
render() {
ctx.drawImage(Resources.get(this.sprite), this.x, this.y);
}
handleInput(keyCode) {
if (this.y >= 0 && keyCode === "up") {
this.y -= this.speedY;
}
if (this.y < this.initialHeight && keyCode === "down") {
this.y += this.speedY;
}
if (this.x > 0 && keyCode === "left") {
this.x -= this.speedX;
}
if (this.x < this.initialWidth && keyCode === "right") {
this.x += this.speedX;
}
if (this.y < water_edge) {
this.wins();
}
}
resetPosition() {
this.x = this.initialWidth / 2;
this.y = this.initialHeight;
}
lose() {
this.resetPosition();
alert("Game over. Try again!");
}
wins() {
setTimeout(() => {
this.resetPosition();
}, 100);
alert("You win!!! Congratulation");
}
}
const playerConfiguration = {
initialWidth: 400,
initialHeight: 375,
speedY: 80,
speedX: 100,
};
const player = new Player(playerConfiguration);
const enemyFirstConfiguration = {
x: 100,
y: 60,
speedX: 200,
initialWidth: 400,
player,
};
const enemySecondConfiguration = {
x: 100,
y: 145,
speedX: 180,
initialWidth: 400,
player,
};
const enemyThirdConfiguration = {
x: 100,
y: 230,
speedX: 150,
initialWidth: 400,
player,
};
// Now instantiate your objects.
// Place all enemy objects in an array called allEnemies
// Place the player object in a variable called player
const allEnemies = [
new Enemy(enemyFirstConfiguration),
new Enemy(enemySecondConfiguration),
new Enemy(enemyThirdConfiguration),
];
// This listens for key presses and sends the keys to your
// Player.handleInput() method. You don't need to modify this.
function handleClick(e) {
const allowedKeys = {
37: "left",
38: "up",
39: "right",
40: "down",
};
player.handleInput(allowedKeys[e.keyCode]);
}
document.addEventListener("keyup", handleClick);