-
Notifications
You must be signed in to change notification settings - Fork 0
/
biker.js
89 lines (68 loc) · 2.09 KB
/
biker.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
function Biker(canvas) {
this.canvas = canvas;
this.canvas_context = canvas.getContext('2d');
this.reset();
this.width = 30;
this.height = 40;
this.movement = 10;
this.boundary_margin = 32;
this.image = new Image();
this.image.src = "static/img/biker_large.png";
this.sprite = sprite(this.canvas_context, 160, 64, this.image, 4, true);
}
Biker.prototype.reset = function(canvas) {
this.x_position = this.canvas.width / 2;
this.y_position = this. canvas.height / 6;
this.atFinishLine = false;
this.is_alive = true;
}
Biker.prototype.is_hit = function() {
sound.play('crash');
this.is_alive = false;
}
Biker.prototype.update = function(rightPressed, leftPressed, downPressed, upPressed, endOfLevel) {
// compatible movements
if (upPressed && this.upBoundaryCheck()) { // break
this.y_position -= this.movement*0.5;
}
if (downPressed && this.downBoundaryCheck()) { // accelerate
this.y_position += this.movement;
}
if (rightPressed && this.rightBoundaryCheck()) { // can also move right
this.x_position += this.movement;
}
if (leftPressed && this.leftBoundaryCheck()) { // or left
this.x_position -= this.movement;
}
// if both left and right, biker doesn't move in x-direction
if (endOfLevel && (this.downBoundaryCheck() == false)) {
this.atFinishLine = true;
}
this.sprite.update();
}
Biker.prototype.draw = function() {
if (this.is_alive) {
this.sprite.render(this.x_position, this.y_position);
}
}
// returns true if illegal position
Biker.prototype.leftBoundaryCheck = function() {
if (this.x_position + this.width/2 > (0 + boundary_margin)){
return true;
} return false;
}
Biker.prototype.rightBoundaryCheck = function() {
if (this.x_position + this.width/2 < this.canvas.width - boundary_margin){
return true;
} return false;
}
Biker.prototype.upBoundaryCheck = function() {
if (this.y_position + this.height/2 > 0 + boundary_margin){
return true;
} return false;
}
Biker.prototype.downBoundaryCheck = function() {
if (this.y_position + this.height/2 < this.canvas.height - boundary_margin){
return true;
} return false;
}