-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
177 lines (146 loc) · 5.43 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<html>
<canvas id="gameCanvas" width="800px" height="600px"></canvas>
<script>
var canvas;
var canvasContext;
var ballX = 250;
var ballY = 250;
var ballSpeedX = 500; // px/sec
var ballSpeedY = 200; // px/sec
var paddle1Y = 250;
var paddle2Y = 250;
const PADDLE_HEIGHT = 80;
const PADDLE_WIDTH = 14;
var player1Score = 0;
var player2Score = 0;
const WINNING_SCORE = 3;
var winnerName = null;
var isSummationDisplaying = false;
window.onload = function() {
console.log('We are starting');
canvas = document.getElementById('gameCanvas');
canvasContext = canvas.getContext('2d');
var framesPerSecond = 30;
var oneSec = 1000;
var refreshingInterval = oneSec/framesPerSecond;
//Starts the game;
setInterval(function(){
moveEverything(refreshingInterval);
drawEverything();
}, refreshingInterval);
canvas.addEventListener('mousedown', function () {
isSummationDisplaying = isSummationDisplaying ? false : null;
});
canvas.addEventListener('mousemove', function(evt){
var mousePos = calculateMousePos(evt);
paddle1Y = mousePos.y - PADDLE_HEIGHT/2;
})
}
function moveEverything(refreshingInterval) {
if(isSummationDisplaying){
return
}
var winner;
//moving ball
//checking if ball get bounced from left paddle
if ((ballX <= 20) && (!checkIfBallBounced(paddle1Y+PADDLE_HEIGHT/2, ballY))) {
winner = 2;
resetBall(winner);
return;
}
//checking if ball get bounced from right paddle
if ((ballX >= canvas.width - 15) && (!checkIfBallBounced(paddle2Y+PADDLE_HEIGHT/2, ballY))) {
winner = 1;
resetBall(winner);
return;
}
//bouncing off player 1 paddle
if (ballX <= 20) {
ballSpeedX = -ballSpeedX;
var deltaY = ballY - (paddle1Y + PADDLE_HEIGHT/2);
ballSpeedY = deltaY * 10;
}
//bouncing off player 2 paddle
if (ballX >= canvas.width - 15) {
ballSpeedX = -ballSpeedX;
var deltaY = ballY - paddle2Y;
ballSpeedY = deltaY * 10;
}
//bouncing off the wall
(ballY >= canvas.height - 15) || (ballY <= 20) ? ballSpeedY = -ballSpeedY : null;
ballX = ballX + ballSpeedX/refreshingInterval;
ballY = ballY + ballSpeedY/refreshingInterval;
computerMovement();
}
function drawEverything() {
if(isSummationDisplaying){
colorRect(0, 0, canvas.width, canvas.height, 'red');
canvasContext.fillStyle = 'white';
canvasContext.fillText(winnerName + ' won!', canvas.width / 2 - 30, 200);
canvasContext.fillText('Click mouse button to start again.', canvas.width / 2 - 70, 300);
return;
}
console.log('hehe');
// drawing black background for the game
colorRect(0, 0, canvas.width, canvas.height, 'black');
// drawing left player paddle
colorRect(0, paddle1Y, PADDLE_WIDTH, PADDLE_HEIGHT, 'red');
// drawing right computer paddle
colorRect(canvas.width - PADDLE_WIDTH, paddle2Y, PADDLE_WIDTH, PADDLE_HEIGHT, 'blue');
//drawing net
drawNet();
// drawing ball
colorCircle(ballX, ballY, 10, 'white');
canvasContext.fillText('Player1: ' + player1Score + ' ' + 'Player2: ' + player2Score, canvas.width / 2 - 65, 25);
}
function drawNet() {
for(var i=20; i+20 < canvas.height; i += 40){
colorRect(canvas.width/2 - 2, i, 4, 20, 'white');
}
}
function colorCircle(centerX, centerY, radius, color) {
canvasContext.fillStyle = color;
canvasContext.beginPath();
canvasContext.arc(centerX, centerY, radius, 0, Math.PI*2, true);
canvasContext.fill();
}
function colorRect(leftX, topY, width, height, color) {
canvasContext.fillStyle = color;
canvasContext.fillRect(leftX, topY, width, height);
}
function calculateMousePos(evt) {
var rect = canvas.getBoundingClientRect();
var root = document.documentElement;
var mouseX = evt.clientX - rect.left - root.scrollLeft;
var mouseY = evt.clientY - rect.top - root.scrollTop;
return {
x: mouseX,
y: mouseY
}
}
function checkIfBallBounced (paddleY, ballY){
return ((((ballY - paddleY) <= PADDLE_HEIGHT/2)) && ((ballY - paddleY) >= -PADDLE_HEIGHT/2)) ? true : false;
}
function resetBall(winner){
ballX = 400;
ballY = 350;
ballSpeedX = 500; // px/sec
ballSpeedY = -200; // px/sec
//scoring
winner === 1 ? player1Score++ : player2Score++;
//In case there is a winner:
if (player1Score >= WINNING_SCORE || player2Score >= WINNING_SCORE){
winnerName = player1Score > player2Score ? 'Player 1' : 'Player 2';
isSummationDisplaying = true
player1Score = 0;
player2Score = 0;
}
}
function computerMovement() {
var paddle2YCenter = paddle2Y + PADDLE_HEIGHT/2;
//podążanie rakietki za piłką
paddle2YCenter < ballY - 35 ? paddle2Y += 7 : null;
paddle2YCenter > ballY + 35 ? paddle2Y -= 7 : null;
}
</script>
</html>