-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaddles.html
161 lines (151 loc) · 5.39 KB
/
paddles.html
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
<!DOCTYPE html>
<html>
<head>
<title>Paddles - reference</title>
</head>
<body>
<canvas id="board" width="800" height="500" />
<script>
class Paddle {
constructor(x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.speed = 5;
}
render(ctx) {
ctx.fillStyle = "white";
ctx.fillRect(this.x, this.y, this.width, this.height);
}
hasHitBall(ball) {
return ball.x > this.x && ball.x < this.x + this.width && ball.y > this.y && ball.y < this.y + this.height;
}
moveUp(minY) {
if (this.y > minY + this.speed) {
this.y -= this.speed;
}
}
moveDown(maxY) {
if (this.y < maxY - this.speed - this.height) {
this.y += this.speed;
}
}
}
class Keys {
constructor() {
this.up = false;
this.down = false;
}
processKeyDown(keyCode) {
this.up = keyCode == 38;
this.down = keyCode == 40;
}
processKeyUp(keyCode) {
if (this.up && keyCode == 38) {
this.up = false;
}
else if (this.down && keyCode == 40) {
this.down = false;
}
}
}
class HumanPlayer {
constructor(paddle) {
this.paddle = paddle;
}
update(keys, minY, maxY) {
if (keys.down) {
this.paddle.moveDown(maxY);
} else if (keys.up) {
this.paddle.moveUp(minY);
}
}
hasHitBall(ball) {
return this.paddle.hasHitBall(ball);
}
render(ctx) {
this.paddle.render(ctx);
}
}
class Speed {
constructor(x, y) {
this.x = x;
this.y = y;
}
reverseX() {
this.x *= -1;
}
reverseY() {
this.y *= -1;
}
}
class Ball {
constructor (x, y) {
this.x = x;
this.y = y;
this.radius = 3;
this.speed = new Speed(5, 0);
}
render(ctx) {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI, false);
ctx.fillStyle = "white";
ctx.fill();
}
move() {
this.x += this.speed.x;
this.y += this.speed.y;
}
update(paddle1, paddle2) {
if (paddle1.hasHitBall(this) || paddle2.hasHitBall(this)) {
this.reverseX();
}
this.move();
}
reverseX() {
this.speed.reverseX();
}
}
class Game {
constructor(ctx) {
this.ctx = ctx;
this.width = 800;
this.height = 500;
this.paddleWidth = 20;
this.paddleHeight = 100;
this.player = new HumanPlayer(new Paddle(5, 200, this.paddleWidth, this.paddleHeight));
this.ai = new Paddle(this.width - this.paddleWidth - 5, 200, this.paddleWidth, this.paddleHeight);
this.ball = new Ball(this.width / 2, this.height / 2, this.paddleWidth, this.paddleHeight);
this.keys = new Keys();
}
tick() {
this.update();
this.draw();
window.setTimeout("game.tick()", 1000/60);
}
update() {
this.ball.update(this.player, this.ai);
this.player.update(this.keys, 0, this.height);
// ai.update();
}
draw() {
this.ctx.fillStyle = "black";
this.ctx.fillRect(0, 0, this.width, this.height);
this.player.render(ctx);
this.ai.render(ctx);
this.ball.render(ctx);
}
start() {
window.addEventListener("keydown", function(e) { game.keys.processKeyDown(e.keyCode) });
window.addEventListener("keyup", function(e) { game.keys.processKeyUp(e.keyCode) });
this.tick();
}
}
var board = document.getElementById("board");
var ctx = board.getContext("2d");
var game = new Game(ctx);
game.start();
</script>
</body>
</html>