Dylan schreef:Codeblok (voorbeeld.php):
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"use strict";
var GameClient = class GameClient {
constructor() {
console.log("GameClient initialized");
this.canvas = document.getElementById("game");
this.ctx = this.canvas.getContext("2d");
// canvas resizing
window.addEventListener("resize", this._resizeCanvas.bind(this), false);
this._resizeCanvas();
// key ups and downs
this.keysDown = [];
window.addEventListener("keydown", this._keyDown.bind(this), false);
window.addEventListener("keyup", this._keyUp.bind(this), false);
// players
this.players = [];
this.players.push(new LocalPlayer("red"));
this.players.push(new OpponentPlayer("blue"));
// ball
this.Ball = new Ball();
// drawing
this.frameInterval = 1000 / 30; // 30 frames per second
this.currentTime = Date.now();
this.startTime = this.currentTime;
window.requestAnimationFrame(this._draw.bind(this));
}
_keyDown(event) {
if(!this.keysDown.contains(event.which)) this.keysDown.push(event.which);
}
_keyUp(event) {
if(this.keysDown.contains(event.which)) {
let index = this.keysDown.indexOf(event.which);
this.keysDown.splice(index, 1);
}
}
// resizes the canvas
_resizeCanvas() {
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
}
// draw on the canvas
_draw() {
this.currentTime = Date.now();
let elapsed = this.currentTime - this.startTime;
if(elapsed > this.frameInterval) {
this.startTime = this.currentTime - (elapsed % this.frameInterval);
// clear canvas
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
// draw background
this.ctx.fillStyle = "black";
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
// draw players
let scoreboardY = 30;
for(let i = 0, player = this.players[0];
i < this.players.length;
i++, player = this.players
) {
this.ctx.fillStyle = player.color;
this.ctx.fillRect(player.x, player.y, player.width, player.height);
//scoreboard
// draw scoreboard
this.ctx.font = "40px Calibri";
this.ctx.fillStyle = player.color;
this.ctx.fillText("Score: " + player.score, 10, scoreboardY);
scoreboardY += 45;
player.update();
}
// draw the ball
this.ctx.beginPath();
this.ctx.fillStyle = this.Ball.color;
this.ctx.arc(this.Ball.x, this.Ball.y, this.Ball.size, 0, Math.PI * 2);
this.ctx.fill();
this.Ball.update();
}
window.requestAnimationFrame(this._draw.bind(this));
}
}
var Ball = class Ball {
constructor() {
this.canvas = document.getElementById("game");
this.size = 10;
this.x = this.canvas.width / 2 - this.size / 2;
this.y = this.canvas.height / 2 - this.size / 2;
this.velx = (Math.floor(Math.random() * 2) == 0 ? 5 : - 5);
this.vely = (Math.floor(Math.random() * 2) == 0 ? 5 : - 5);
this.color = "white";
}
update() {
if(this.x < 70) {
} else if(this.x > this.canvas.width - 30) {
} else {
if(this.y <= 0) {
this.y = 0;
this.vely *= -1;
}
if(this.y >= this.canvas.height - this.size) {
this.y = this.canvas.height - this.size;
this.vely *= -1;
}
if(this.x <= Game.players[0].x + Game.players[0].width + Game.players[1].width / 2) {
if(this.y >= Game.players[0].y && this.y <= Game.players[0].y + Game.players[0].height) {
this.velx *= -1;
Game.players[0].score += 10;
}
}
if(this.x >= Game.players[1].x - Game.players[1].width / 2) {
if(this.y >= Game.players[1].y && this.y <= Game.players[1].y + Game.players[1].height) {
this.velx *= -1;
Game.players[1].score += 10;
}
}
this.x += this.velx;
this.y += this.vely;
}
}
}
var Player = class Player {
constructor() {
this.width = 25;
this.height = 100;
this.speed = 10;
this.score = 0;
this.canvas = document.getElementById("game");
}
}
var LocalPlayer = class LocalPlayer extends Player {
constructor(color) {
super();
this.color = color;
this.x = 100;
this.y = this.canvas.height / 2 - this.height / 2;
}
update() {
if(Game.keysDown.contains(87)) {
if(this.y > 0) {
this.y -= this.speed;
}
} else if(Game.keysDown.contains(83)) {
if(this.y <= this.canvas.height - this.height) {
this.y += this.speed;
}
}
}
}
var OpponentPlayer = class LocalPlayer extends Player {
constructor(color) {
super();
this.color = color;
this.x = this.canvas.width - 100;
this.y = this.canvas.height / 2 - this.height / 2;
}
update() {
if(Game.keysDown.contains(38)) {
if(this.y > 0) {
this.y -= this.speed;
}
} else if(Game.keysDown.contains(40)) {
if(this.y <= this.canvas.height - this.height) {
this.y += this.speed;
}
}
}
}
Array.prototype.contains = function(i) {
return (this.indexOf(i) >= 0);
};
==
Afbeelding
ES6 
[I]toevoeging op 3 januari 2016, 20:27:26:http://dylaan.nl/PingPong/Alleen nog even een winnaar tekst, komt later wel
toevoeging op 3 januari 2016, 20:47:40:en die Opponent class kan wel weg
Netjes
