-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
174 lines (154 loc) · 5.33 KB
/
script.js
File metadata and controls
174 lines (154 loc) · 5.33 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
//idea : reverse pong option where you have to dodge the ball 'dodge pong'
//add sound
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
canvas.width = window.innerWidth-100;
canvas.height = window.innerHeight-220;
var speedComp = Math.random() * (200-50)-50;
var playerScore = 0;
var computerScore = 0;
var scorePlayer = document.getElementById('pScore');
var scoreComputer = document.getElementById('cScore');
var message = document.getElementById('message');
var Paddle = function Paddle(xpos, ypos, width, height, speed) {
this.xpos = xpos;
this.ypos = ypos;
this.height = height;
this.width = width;
this.speed = speed;
};
var Ball = function Ball(centerX, centerY, radius) {
this.centerX= centerX;
this.centerY= centerY;
this.radius= radius || 20;
this.speedX = Math.random() * (7-3) + 3;
this.speedY = Math.random() * (7-3) + 3;
};
// ****************************************************** Render
Paddle.prototype.render = function() {
context.beginPath();
context.rect(this.xpos, this.ypos, this.width, this.height);
context.fillStyle = 'black';
context.fill();
};
Ball.prototype.render = function() {
context.beginPath();
context.arc(this.centerX, this.centerY, this.radius, 0, 2 * Math.PI, false);
context.fillStyle = 'darkBlue';
context.fill();
};
// ****************************************************** Move
Paddle.prototype.move = function(event) {
if (event.keyCode === 38 && this.ypos>0) {
this.ypos -= this.speed;
} else if (event.keyCode === 40 && this.ypos<canvas.height-100) {
this.ypos += this.speed;
}
};
Ball.prototype.move = function() {
this.centerX += this.speedX;
this.centerY += this.speedY;
//bouncing off top and bottom
if(this.centerY + this.radius > canvas.height || this.centerY - this.radius < 0) {
this.speedY = this.speedY * -1;
}
//going thru the sides
if(this.centerX + this.radius > canvas.width-5) {
this.speedX = this.speedX * -1;
computerScore +=1;
document.getElementById('bump').play()
console.log('o no ' + computerScore + " points for the computer" );
}
if(this.centerX - this.radius < 0) {
this.speedX = this.speedX * -1;
playerScore += 1;
document.getElementById('bump').play()
console.log('yay ' + playerScore + ' points for you');
}
};
// // ------------collision-----------------
var collisionCheck = function(paddle, axis) {
var widthheight = axis == "x" ? "width" : "height";
var centerRect = paddle[axis + "pos"] + paddle[widthheight] / 2;
return Math.abs(ball["center"+axis.toUpperCase()] - centerRect);
};
var collision = function() {
var distXpaddle = collisionCheck(paddleR, "x");
var distYpaddle = collisionCheck(paddleR, "y");
var distXcomputer = collisionCheck(computer, "x");
var distYcomputer = collisionCheck(computer, "y");
if (distXpaddle < (paddleR.width/2 + ball.radius) && distYpaddle < (paddleR.height/2 + ball.radius)) {
ball.speedX = ball.speedX * -1;
document.getElementById('blop').play()
console.log("boing" );
};
if (distXcomputer < (computer.width/2 + ball.radius) && distYcomputer < (computer.height/2 + ball.radius)) {
console.log("boop" );
document.getElementById('blop').play();
ball.speedX = ball.speedX * -1;
};
};
//
// ****************************************************** Instances
var paddleR = new Paddle(canvas.width-20, canvas.height/2-50, 20, 100, 50);
var ball = new Ball(canvas.width/2, canvas.height/2);
var computer = new Paddle(0, canvas.height/2-50, 20, 100);
var gamePlaying = false;
var updateComputer = function() {
computer.ypos = ball.centerY-speedComp;
// paddleR.ypos = ball.centerY -50;
};
var animate = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback) { window.setTimeout(callback, 1000/60) };
var drawLine = function() {
context.setLineDash([20, 20]);
context.beginPath();
context.moveTo(canvas.width/2, 0);
context.lineWidth = 5;
context.lineTo(canvas.width/2, canvas.height);
context.strokeStyle = 'gray';
context.stroke();
};
var updateScore = function() {
scorePlayer.innerHTML = playerScore;
scoreComputer.innerHTML = computerScore;
if(playerScore >=11 || computerScore >=11) {
var winner = playerScore > computerScore ? message.innerHTML="you win!" : message.innerHTML="you loose";
message.style.visibility = 'visible';
gamePlaying = false;
document.getElementById('game-over').play()
playerScore = 0;
computerScore = 0;
};
};
// callback for animate function
function step() {
context.clearRect(0,0,canvas.width, canvas.height);
drawLine();
ball.move();
collision();
updateComputer();
ball.render();
computer.render();
paddleR.render();
updateScore();
if (gamePlaying){
animate(step);
}
}
function keyListener(event) {
console.log(event.keyCode);
if (event.keyCode == 38 || event.keyCode == 40) {
paddleR.move(event);
} else if(event.keyCode === 32 && gamePlaying === false) {
message.style.visibility = 'hidden';
gamePlaying = true;
animate(step);
}
}
window.addEventListener('keydown', keyListener);
window.onload = animate(step);