-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.js
65 lines (57 loc) · 1.71 KB
/
Player.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
function Player() {
this.x = 100;
this.y = 250;
this.width = 50;
this.height = 50;
this.velocity = 0;
this.acceleration = 3;
this.previousPosition = this.y;
}
Player.prototype.reset = function() {
this.x = 100;
this.y = 250;
this.velocity = 0;
}
Player.prototype.update = function(delta) {
this.previousPosition = this.y;
if (keysDown[32] && !jump) {
jump = true;
this.velocity = 8;
this.y -= 3;
}
this.velocity -= this.acceleration * (delta / 100);
this.y -= this.velocity;
if (this.y >= canvas.height || this.y <= 0) {
gameOver = true;
}
}
Player.prototype.render = function(delta) {
ctx.drawImage(bg, player.x, player.previousPosition, player.width, player.height, player.x, player.previousPosition, player.width, player.height);
timeCounter += delta;
if (timeCounter >= 200) {
timeCounter = 0;
animationCrop++;
if (animationCrop * this.width >= batA.width) {
animationCrop = 0;
}
}
ctx.drawImage(batA, animationCrop * this.width, 0, this.width, this.height, this.x, this.y, this.width, this.height);
}
Player.prototype.collisionDetection = function() {
for (var i = 0; i < aTubes.length; i++) {
if (aTubes[i].x < canvas.width) {
if (this.x <= aTubes[i].x + aTubes[i].width &&
aTubes[i].x <= this.x + this.width &&
this.y <= aTubes[i].y + aTubes[i].height &&
aTubes[i].y <= this.y + this.height) {
gameOver = true;
}
}
}
}
Player.prototype.scoreKeeper = function() {
if (aTubes[0].x <= 20 && !scoreChecker) {
score++;
scoreChecker = true;
}
}