-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.js
203 lines (175 loc) · 4.62 KB
/
app.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
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
var canv = document.getElementById("paper");
var ctx = canv.getContext('2d');
var plBatX = 20, // Player bat X coord
plBatY = canv.height/2 - 50, // Player bat Y coord
cpBatX = canv.width - 40, // Computer bat X coord
cpBatY = canv.height/2 - 50, // Computer bat Y coord
cpBatVY = 3, // Computer bat velocity
cBallX = 320,
cBallY = 240,
cBallVX = 6,
cBallVY = 4,
cBallVelY = 4,
plName = "",
activeSchema = "",
plScore = 0,
cpScore = 0,
ballColor = '#000',
textColor = '#000',
playerColor = '#000',
computerColor = '#000',
backgroundColor = '#CCC',
batSchema = ['#800000', '#ffffff', '#224912'],
backgroundSchema = ['#112233', '#334455', '#662255'],
textSchema = ['#987631', '#123994', '#61e213'],
ballSchema = ['#987123', '#987123', '#FFFFFF'];
var batSchema = ['#800000', '#ffffff', '#224912'];
var tableSchema = ['#112233', '#334455', '#662255'];
var textSchema = ['#987631', '#123994', '#61e213'];
var ballSchema = ['#987123', '#987123', '#FFFFFF'];
var colorBat = "";
var colorBall = "";
var colorText = "";
var colorTable = "";
window.onload = function() {
plName = prompt("Enter Your Name : ");
if ( plName == "" || plName == null ) {
plName = "Player";
}
activeSchema = prompt("Choose color scheme (1,2, or 3)");
setInterval(update, 1000/60);
setActiveColorSchema(activeSchema);
canv.addEventListener("mousemove", function(event) {
plBatY = event.clientY - 50;
});
document.querySelector("#colorPicker").addEventListener("change", function(){
var color = document.querySelector('#colorPicker').value;
var elem = document.querySelector('#elementSelector').value;
switch (elem) {
case "ballColor":
ballColor = color;
break;
case "playerColor":
playerColor = color;
break;
case "computerColor":
computerColor = color;
break;
case "backgroundColor":
backgroundColor = color;
break;
case "textColor":
textColor = color;
break;
}
}, false)
};
var update = function() {
// Draw the canvas
ctx.fillStyle = backgroundColor;
ctx.fillRect(0, 0, canv.width, canv.height);
ctx.fillStyle = playerColor;
ctx.fillRect(plBatX, plBatY, 20, 100); // Bat width = 20px, height = 100px
ctx.fillStyle = computerColor;
ctx.fillRect(cpBatX, cpBatY, 20, 100);
ctx.beginPath();
ctx.arc(cBallX, cBallY, 15, 0, 2*Math.PI);
ctx.fillStyle = ballColor;
ctx.fill();
ctx.font = "15px Source Code Pro Semibold";
ctx.fillStyle = textColor;
ctx.fillText(plName, 80, 25);
ctx.fillText(plScore, 80, 40);
ctx.fillText("Computer", 500, 25);
ctx.fillText(cpScore, 500, 40);
cBallX += cBallVX;
cBallY += cBallVY;
cpBatY += cpBatVY;
if ( cBallX > canv.width ) {
plScore++;
cBallX = canv.width/2;
}
if ( cBallX < 0 ) {
cpScore++;
cBallX = canv.width/2;
}
// Reverse vertical velocity of ball when it hits canvas edge
if ( cBallY > canv.height-16 || cBallY < 16 ) {
cBallVY = -cBallVY;
}
// Computer bat collision
if ( cBallX > cpBatX - 15 && ( cBallY > cpBatY - 15 && cBallY < cpBatY + 115 ) ) {
cBallVX = -cBallVX;
if ( cBallY < cpBatY + 50 ) {
cBallVY = -cBallVelY;
} else {
cBallVY = cBallVelY;
}
}
// Player Bat collision
if ( (cBallX < plBatX + 35 ) && ( cBallY > plBatY - 15 && cBallY < plBatY + 115 ) ) {
cBallVX = -cBallVX;
if ( cBallY < plBatY + 50 ) {
cBallVY = -cBallVelY;
} else {
cBallVY = cBallVelY;
}
}
// Computer Bat 'AI'
if ( cBallVX > 0 ) {
if ( cpBatY > cBallY ) {
cpBatVY = -5;
}
if ( cpBatY < cBallY ) {
cpBatVY = 5;
}
} else {
cpBatVY = 0;
}
// Winner
if ( plScore >= 10 ) {
alert("The winner is " + plName);
reset();
}
if ( cpScore >= 10 ) {
alert("The winner is Computer");
reset();
}
}
var setActiveColorSchema = function (schemaNumber) {
computerColor = batSchema[schemaNumber - 1];
playerColor = batSchema[schemaNumber - 1];
ballColor = ballSchema[schemaNumber - 1];
textColor = textSchema[schemaNumber - 1];
backgroundColor = backgroundSchema[schemaNumber - 1];
if (!backgroundColor || !computerColor || !playerColor || !textColor || !ballColor) {
computerColor = '#000';
playerColor = '#000';
ballColor = '#000';
textColor = '#000';
backgroundColor = '#ccc';
}
}
// Reset the game
var reset = function() {
plScore = 0;
cpScore = 0;
alert("Good Game!");
plName = prompt("Enter your name : ");
if ( plName == "" || plName == null ) {
plName = "Player";
}
activeSchema = prompt("Choose color scheme (1,2, or 3)");
setActiveColorSchema(activeSchema);
}
// Play or pause game
var storeBallVX = 0, storeBallVY = 0;
var pause = function() {
var temp;
temp = cBallVX;
cBallVX = storeBallVX;
storeBallVX = temp;
temp = cBallVY;
cBallVY = storeBallVY;
storeBallVY = temp;
}