-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
256 lines (214 loc) · 6.11 KB
/
index.html
File metadata and controls
256 lines (214 loc) · 6.11 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<!DOCTYPE html>
<html>
<head>
<title>Pong</title>
<style>
#screen {
width: 800px;
height: 600px;
background-color: black;
border: 1px solid black;
position: relative;
}
.paddle {
width: 20px;
height: 100px;
background-color: red;
position: absolute;
}
.ball {
width: 20px;
height: 20px;
background-color: #00ff00;
position: absolute;
}
.score {
width: 500px;
height: 60px;
background-color: white;
border: 2px solid red;
margin: 50px auto 0 auto;
padding: 10px;
position: relative;
font-style: sans-serif;
font-size: 100%;
}
#player_1_score {
width: 100px;
height: 30px;
background-color: blue;
border: 1px solid red;
padding: 10px;
margin-left: 25%;
float: left;
/* position: absolute;*/
font-style: arial black;
font-size: 2em;
text-align: center;
}
#player_2_score {
width: 100px;
height: 30px;
background-color: yellow;
border: 1px solid red;
padding: 10px;
margin-right: 25%;
float: right;
/* position: absolute;*/
font-style: arial black;
font-size: 2em;
text-align: center;
}
#paddle_2 {
background-color: yellow;
border: 1px solid red;
}
#paddle_1 {
background-color: blue;
border: 2px solid red;
}
</style>
</head>
<body>
<div id="screen">
<div class="paddle" id="paddle_1"></div>
<div class="paddle" id="paddle_2"></div>
<div class="ball" id="ball"></div>
<div class="score">
<div class="score1" id="player_1_score"></div>
<div class="score2" id="player_2_score"></div>
</div>
</div>
<!-- <div id="player_1_score"></div> -->
<script>
/*
TODO
1. Ball collision off of the ceiling
2. Control (player / computer -- choose one) of paddle two
3. Display and track scores (ball should reset after score)
4. Ball-paddle collisions (tricky!)
*/
var paddle_1_element = document.getElementById("paddle_1");
var paddle_1_model = {
x: 20,
y: 200
};
var paddle_2_element = document.getElementById("paddle_2");
var paddle_2_model = {
x: 760,
y: 200,
vy: 6
};
var ball_element = document.getElementById("ball");
var ball_model = {
x: 300,
y: 300,
vx: 6,
vy: 7
}
var player_1_score_element = document.getElementById("player_1_score");
var player_1_score = 0;
var player_2_score_element = document.getElementById("player_2_score");
var player_2_score = 0;
function onMouseMove(evt) {
paddle_1_model.y = evt.pageY - evt.target.offsetTop - 50;
}
//update
function update() {
//update the ball model position
ball_model.x = ball_model.x + ball_model.vx;
ball_model.y = ball_model.y + ball_model.vy;
//check if the ball is colliding with the bottom edge
if (ball_model.y > 600 - 20 && ball_model.vy > 0) {
//flip the y direction
ball_model.vy = ball_model.vy * -1;
}
//check if the ball is colliding with the top edge
if (ball_model.y < 0 && ball_model.vy < 0) {
//flip the y direction
ball_model.vy = ball_model.vy * -1;
}
// //check if the ball is colliding with the left edge
// if (ball_model.x < 0 && ball_model.vx < 0) {
// //flip the y direction
// ball_model.vx = ball_model.vx * -1;
// }
// //check if the ball is colliding with the right edge
// if (ball_model.x > 800 - 20 && ball_model.vx > 0) {
// //flip the y direction
// ball_model.vx = ball_model.vx * -1;
// }
//check if ball is colliding with paddle_1
if (ball_model.x < paddle_1_model.x + 20 && ball_model.y + 20 > paddle_1_model.y && ball_model.y < paddle_1_model.y + 100 && ball_model.x + 20 > paddle_1_model.x) {
ball_model.vy = ball_model.vy * -1;
ball_model.vx = ball_model.vx * -1;
}
//check if ball is colliding with paddle_2
if (ball_model.x < paddle_2_model.x + 20 && ball_model.y + 20 > paddle_2_model.y && ball_model.y < paddle_2_model.y + 100 && ball_model.x + 20 > paddle_2_model.x) {
ball_model.vx = ball_model.vx * -1;
ball_model.vy = ball_model.vy * -1;
}
//check if ball is exiting right edge and resets to center
if (ball_model.x > 800 - 20 && ball_model.vx > 0) {
player_1_score += 1;
ball_model.x = 400;
ball_model.y = 300;
}
//check if ball is exiting left edge and resets to center
if (ball_model.x < 0 && ball_model.vx < 0) {
player_2_score += 1;
ball_model.x = 400;
ball_model.y = 300;
}
//check if the ball is colliding with the right edge
// if (ball_model.x > 800 - 20 && ball_model.vx > 0) {
// //flip the y direction
// ball_model.vx = ball_model.vx * -1;
// }
if (ball_model.x) {
// //check if the ball is colliding with the ceiling:
// if the ball is moving towards the ceiling ( along y axis where y is getting closer to 0) and surpasses the height of the screen (600px) then flip the y direction so that the ball moves in the opposite direction from where it was coming from
// } else if (ball_model.y > 600 - 20 && ball_model.vy > 0) {
// ball_model.vy = ball_model.vy * -1;
// }
//update the paddle_2_model position - computer generated
// paddle_2 will follow the x
paddle_2_model.y = paddle_2_model.y + paddle_2_model.vy;
}
//check if paddle_2 is colliding with the edge
if (paddle_2_model.y > 600 - 100 && paddle_2_model.vy > 0) {
//bottom edge
//flip the y direction
paddle_2_model.vy = paddle_2_model.vy * -1;
}
if (paddle_2_model.y < 0 && paddle_2_model.vy < 0) {
//top edge
//flip the y direction
paddle_2_model.vy = paddle_2_model.vy * -1;
}
paddle_2_model.y = ball_model.y - 50;
}
//draw
function draw() {
//draw paddle 1
paddle_1_element.style.top = paddle_1_model.y + "px";
paddle_1_element.style.left = paddle_1_model.x + "px";
//draw paddle 2
paddle_2_element.style.top = paddle_2_model.y + "px";
paddle_2_element.style.left = paddle_2_model.x + "px";
//draw the ball
ball_element.style.top = ball_model.y + "px";
ball_element.style.left = ball_model.x + "px";
//draw the players' scores
player_1_score_element.innerHTML = player_1_score;
player_2_score_element.innerHTML = player_2_score;
}
function loop() {
update();
draw();
}
document.getElementById("screen").onmousemove = onMouseMove;
setInterval(loop, 1000/60);
</script>
</body>
</html>