/
paddles.js
69 lines (48 loc) · 1.26 KB
/
paddles.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
function Paddle() {
Entity.call(this)
this.width = 20
this.height = 100
this.y = game.height / 2 - this.height / 2
this.score = 0
}
Paddle.prototype = Object.create(Entity.prototype)
Paddle.prototype.constructor = Paddle
Paddle.prototype.update = function() {
Entity.prototype.update.apply(this, arguments)
// Keep the paddle within the screen
this.y = Math.min(Math.max(this.y, 0),
game.height - this.height)
}
function Player() {
Paddle.call(this)
this.x = 20
this.speed = 15
}
Player.prototype = Object.create(Paddle.prototype)
Player.prototype.constructor = Player
Player.prototype.update = function() {
if (game.keyPressed.up) {
this.yVelocity = -this.speed
} else if (game.keyPressed.down) {
this.yVelocity = this.speed
} else {
this.yVelocity = 0
}
Paddle.prototype.update.apply(this, arguments)
}
function Bot() {
Paddle.call(this)
this.x = game.width - this.width - 20
this.speed = 5
}
Bot.prototype = Object.create(Paddle.prototype)
Bot.prototype.constructor = Bot
Bot.prototype.update = function() {
// Follow the ball
if (this.y < game.ball.y) {
this.yVelocity = this.speed
} else {
this.yVelocity = -this.speed
}
Paddle.prototype.update.apply(this, arguments)
}