-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
157 lines (140 loc) · 4.03 KB
/
index.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
<head>
<style>
body {
/*background: black;*/
margin: 0;
}
</style>
</head>
<body>
<canvas></canvas>
</body>
<script>
const canvas = document.querySelector('canvas')
const c = canvas.getContext('2d')
canvas.width = 700
canvas.height = 400
c.fillStyle = 'red'
c.fillRect(0, 0, canvas.width, canvas.height)
class Paddle {
constructor({position}) {
this.position = position
this.velocity = {
x: 0,
y: 0,
}
this.width = 10
this.height = 80
}
draw() {
c.fillStyle = 'white'
c.fillRect(this.position.x, this.position.y, this.width, this.height)
}
update() {
this.draw()
if (
this.position.y + this.velocity.y > 0 &&
this.position.y + this.height + this.velocity.y <
canvas.height
) this.position.y += this.velocity.y
}
}
class Ball {
constructor({position}) {
this.position = position
const speed = 2
const direction = {
x: Math.random() - 0.5 >= 0 ? -speed : speed,
y: Math.random() - 0.5 >= 0 ? -speed : speed,
}
this.velocity = {
x: direction.x,
y: direction.y,
}
this.width = 10
this.height = 10
}
draw() {
c.fillStyle = 'white'
c.fillRect(this.position.x, this.position.y, this.width, this.height)
}
update() {
this.draw()
const rightSide = this.position.x + this.width + this.velocity.x
const leftSide = this.position.x + this.velocity.x
const bottomSide = this.position.y + this.height
const topSide = this.position.y
//paddle 1 collisions
if (
leftSide <= paddle1.position.x + paddle1.width &&
bottomSide >= paddle1.position.y &&
topSide <= paddle1.position.y + paddle1.height
) this.velocity.x = -this.velocity.x
//paddle 2 collisions
if (
rightSide >= paddle2.position.x &&
bottomSide >= paddle2.position.y &&
topSide <= paddle2.position.y + paddle2.height
) this.velocity.x = -this.velocity.x
//reverse y directions
if (
bottomSide + this.velocity.y >= canvas.height ||
this.position.y + this.velocity.y <= 0
) {
this.velocity.y = -this.velocity.y
}
this.position.x += this.velocity.x
this.position.y += this.velocity.y
}
}
const paddle1 = new Paddle({
position: {
x: 10,
y: 140
},
})
const paddle2 = new Paddle({
position: {
x: canvas.width - 10 * 2,
y: 140
},
})
const ball = new Ball({
position: {
x: canvas.width / 2,
y: canvas.height / 2,
},
})
paddle1.draw()
paddle2.draw()
function animate() {
requestAnimationFrame(animate)
c.fillStyle = 'black'
c.fillRect(0, 0, canvas.width, canvas.height)
paddle1.update()
paddle2.update()
ball.update()
}
animate()
addEventListener('keydown', (event) => {
const speed = 5
switch (event.key) {
//go up
case 'w':
paddle1.velocity.y = -speed
break
//go down
case 's':
paddle1.velocity.y = speed
break
//go up
case 'ArrowUp':
paddle2.velocity.y = -speed
break
//go down
case 'ArrowDown':
paddle2.velocity.y = speed
break
}
})
</script>