forked from objcode/Hack-the-Future
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong final.html
176 lines (161 loc) · 3.44 KB
/
pong final.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
<html>
<head>
<script language="javascript"
src="http://code.jquery.com/jquery-1.6.js">
</script>
<title>Pong</title>
<style type="text/css">
#gamearea {
position: relative;
background-color: black;
overflow: hidden;
width: 500px;
height: 500px;
}
#paddle1 { position: absolute;
position: absolute;
background-color: white;
top: 230;
left: 10;
width : 7px;
height: 40px;
}
#paddle2 {
position: absolute;
background-color: white;
top: 230;
left: 483;
width : 7px;
height: 40px;
}
#ball {
position: absolute;
background-color: white;
top: 247;
left: 247;
width : 10px;
height: 10px;
}
</style>
</head>
<body>
<!-- Do not edit this -->
<div id="gameinfo">
<span id="player1-info">
<span id="player1-name" class="name">
Player 1
</span>
<span id="player1-score">
0
</span>
</span>
<span id="player2-info">
<span id="player2-name" class="name">
Player 2
</span>
<span id="player2-score">
0
</span>
</span>
</div>
<hr>
<div id="gamearea">
<!-- left blank -->
<div id="ball">
</div>
<div id="paddle1">
</div>
<div id="paddle2">
</div>
</div>
<!-- Put your code inside this script tag -->
<script type="text/javascript">
// Demonstrate CLASS and ID lookup
$(".name").after(':')
$("#gameinfo").css('width', '500px');
$("#player2-info").css('float', 'right');
//Show how to do prompt stuff
// this is the FIRST time they'll have seen variables
input = prompt("What is player 1's name?");
$("#player1-name").html(input);
input = prompt("What is player 2's name?");
$("#player2-name").html(input);
//Set up the ball
ball = $("#ball");
ball_x = 247;
ball_y = 247;
ball_dx = 0.8;
ball_dy = 1.1;
p1 = $("#paddle1");
p2 = $("#paddle2");
// we never move paddles
p1_y = 230;
p2_y = 230;
p1_dy = 0;
p2_dy = 0;
p1_score = 0;
p2_score = 0;
gametick = function() {
ball_x += ball_dx;
ball_y += ball_dy;
p1_y = Math.min(Math.max(p1_y + p1_dy, 0), 460);
p2_y = Math.min(Math.max(p2_y + p2_dy, 0), 460);
p1.css('top', p1_y);
p2.css('top', p2_y);
ball.css('top', ball_y);
ball.css('left', ball_x);
if(ball_x > 490) {
p1_score = p1_score += 1;
ball_x = 247;
ball_y = 247;
$("#player1-score").html(p1_score);
}
if(ball_x < 10) {
p2_score = p2_score += 1;
ball_x = 247;
ball_y = 247;
$("#player2-score").html(p2_score);
}
if (ball_y > 489 || ball_y < 1) {
ball_dy = ball_dy * -1;
}
if (ball_x >= 473 && ball_y > p2_y && ball_y < p2_y + 40) {
ball_dx = -1.1
}
if (ball_x <= 17 && ball_y > p1_y && ball_y < p1_y + 40) {
ball_dx = 1.1;
}
setTimeout('gametick()', 5);
}
gametick();
$(document).keydown(function(e) {
if(e.keyCode == 59) {
p1_dy = 1.5;
}
if(e.keyCode == 65) {
p1_dy = -1.5;
}
if(e.keyCode == 83) {
p2_dy = 1.5;
}
if(e.keyCode == 76) {
p2_dy = -1.5;
}
});
$(document).keyup(function(e) {
if(e.keyCode == 59) {
p1_dy = 0;
}
if(e.keyCode == 65) {
p1_dy = 0;
}
if(e.keyCode == 83) {
p2_dy = 0;
}
if(e.keyCode == 76) {
p2_dy = 0;
}
});
</script>
</body>
</html>