-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pong.js
131 lines (106 loc) · 2.44 KB
/
Pong.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
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
const canvas = document.getElementById("PongCanvas");
//background size
canvas.width = 1000;
canvas.height = 600;
const context = canvas.getContext("2d");
const bs = 20;
const FPS = 120;
// ball position
var bx = canvas.width/2-bs/2;
var by = canvas.height/2-bs/2;
//ball velocity
var xv, yv;
xv = 300/FPS;
yv = Math.floor(Math.random()*276+25)/FPS;
if (Math.floor(Math.random()*2)==0) {
xv = -xv;
}
if (Math.floor(Math.random()*2)==0) {
yv = -yv;
}
var p1 = canvas.height/2-75;
var p2 = canvas.height/2-75;
var a,b;
function update() {
//background colour
context.fillStyle = "black";
context.fillRect(0, 0, canvas.width, canvas.height);
//ball colour
context.fillStyle = "white";
//ball size
context.fillRect(bx, by, bs, bs);
//player 1
context.fillStyle = "white";
context.fillRect(30, p1, 25, 150);
//player 2
context.fillStyle = "white";
context.fillRect(canvas.width-55, p2, 25, 150);
//position changer
document.onkeydown = checkKey;
function checkKey() {
//controls for player 1
if (window.event.keyCode == '87') {
a = 0 //p1 -= 300/FPS;
}
if (window.event.keyCode == '83') {
a = 1 //p1 += 300/FPS;
}
//controls for player 2
if (window.event.keyCode == '38') {
b = 0 //p2 -= 300/FPS;
}
if (window.event.keyCode == '40') {
b = 1 //p2 += 300/FPS;
}
}
if (a == 0 && p1>0) {
p1 -= 200/FPS;
}
if (a == 1 && p1<canvas.height-150) {
p1 += 200/FPS;
}
if (b == 0 && p2>0) {
p2 -= 200/FPS;
}
if (b == 1 && p2<canvas.height-150) {
p2 += 200/FPS;
}
//boxiness for player 1
if (25<bx && 55>bx && p1-20<by && p1+150>by) {
xv = -xv
yv = Math.floor(Math.random()*276+25)/FPS;
if (Math.floor(Math.random()*2)==0) {
yv = -yv;
}
}
//boxiness for player 2
if (canvas.width-75<bx && canvas.width-45>bx && p2-20<by && p2+150>by) {
xv = -xv
yv = Math.floor(Math.random()*276+25)/FPS;
if (Math.floor(Math.random()*2)==0) {
yv = -yv;
}
}
if (bx < 0 || bx > canvas.width-bs) {
xv = -xv;
bx = canvas.width/2-bs/2;
by = canvas.height/2-bs/2;
p1 = canvas.height/2-75;
p2 = canvas.height/2-75;
yv = Math.floor(Math.random()*276+25)/FPS;
if (Math.floor(Math.random()*2)==0) {
xv = -xv;
}
if (Math.floor(Math.random()*2)==0) {
yv = -yv;
}
a=2
b=2
}
if (by < 0 || by > canvas.height-bs) {
yv = -yv;
}
bx += xv;
by += yv;
}
setInterval(update, 1000/FPS);