-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
107 lines (98 loc) · 3.68 KB
/
main.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
import Ball from "./ball.js";
import Paddle from "./paddle.js";
const ball = new Ball()
const paddlePushoff = 40
const paddleWidth = 20
const playLength = 10
const leftPaddle = new Paddle(paddleWidth)
leftPaddle.setAxis(paddlePushoff)
const rightPaddle = new Paddle(paddleWidth)
rightPaddle.setAxis(window.innerWidth - paddlePushoff)
function calcActiveOffset() {
activeOffsetX = Math.cos(ball.direction) * ball.velocity
activeOffsetY = Math.sin(ball.direction) * ball.velocity
}
document.addEventListener("keydown", (e) => {
const paddleVelocity = 20
switch (e.key) {
case "w": leftPaddle.velocity = paddleVelocity; break;
case "s": leftPaddle.velocity = -paddleVelocity; break;
case "ArrowUp": rightPaddle.velocity = paddleVelocity; break;
case "ArrowDown": rightPaddle.velocity = -paddleVelocity; break;
}
})
document.addEventListener("keyup", (e) => {
switch (e.key) {
case "w": leftPaddle.velocity = 0; break;
case "s": leftPaddle.velocity = 0; break;
case "ArrowUp": rightPaddle.velocity = 0; break;
case "ArrowDown": rightPaddle.velocity = 0; break;
}
})
var rightPlane = window.innerWidth - (paddlePushoff + (paddleWidth / 2))
var leftPlane = paddlePushoff + (paddleWidth / 2)
window.addEventListener("resize", () => {
rightPaddle.setAxis(window.innerWidth - paddlePushoff)
rightPlane = window.innerWidth - (paddlePushoff + (paddleWidth / 2))
})
var rightTurn = true;
var leftScore = 0;
var rightScore = 0;
while (leftScore < playLength && rightScore < playLength) {
var activeOffsetX
var activeOffsetY
ball.velocity = 10
if (rightTurn) {
ball.direction = (Math.random() - 0.5) * (Math.PI / 3)
} else {
ball.direction = (Math.random() - 0.5) * (Math.PI / 3) + Math.PI
}
ball.setPosCenter()
leftPaddle.center()
rightPaddle.center()
var inPlay = true
calcActiveOffset()
while(inPlay) {
let candidateX = ball.posX + activeOffsetX
let candidateY = ball.posY - activeOffsetY
if (ball.posY + ball.radius > window.innerHeight) {
candidateY = window.innerHeight - ball.radius
ball.direction = - ball.direction
calcActiveOffset()
} else if (ball.posY - ball.radius < 0) {
candidateY = ball.radius
ball.direction = - ball.direction
calcActiveOffset()
}
if (candidateX + ball.radius >= rightPlane && ball.posX + ball.radius < rightPlane) {
let paddleCheck = rightPaddle.checkY(ball.posY)
if (paddleCheck != null) {
ball.direction = Math.PI + paddleCheck
}
ball.velocity = ball.velocity + 1
calcActiveOffset()
} else if (candidateX - ball.radius <= leftPlane && ball.posX - ball.radius > leftPlane) {
let paddleCheck = leftPaddle.checkY(ball.posY)
if (paddleCheck != null) {
ball.direction = - paddleCheck
}
ball.velocity = ball.velocity + 1
calcActiveOffset()
}
ball.setPos(candidateX, candidateY)
leftPaddle.setY(leftPaddle.posY - leftPaddle.velocity)
rightPaddle.setY(rightPaddle.posY - rightPaddle.velocity)
if (ball.posX - ball.radius < 0) {
inPlay = false
rightScore++
rightTurn = false
} else if (ball.posX + ball.radius > window.innerWidth) {
inPlay = false
leftScore++
rightTurn = true
}
await new Promise(f => setTimeout(f, 16))
}
document.querySelector("#leftScore").innerHTML = leftScore
document.querySelector("#rightScore").innerHTML = rightScore
}