-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathball.js
45 lines (39 loc) · 1.19 KB
/
ball.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
var ballRadius = 10
function Ball(){
this.x = canvas.width /2
this.y = canvas.height -40
this.velocity = {
x: 6,
y: -6
}
this.draw = function(){
ctx.beginPath()
ctx.arc(this.x,this.y, ballRadius, 0, Math.PI * 2, false)
ctx.fillStyle = 'white'
ctx.fill()
ctx.closePath()
}
this.update = function(){
//if ball goes out of window width right or left
if(this.x + ballRadius > innerWidth || this.x - ballRadius < 0){
this.velocity.x = -this.velocity.x
}
//if ball passes top of the window height
if(this.y - ballRadius < 0){
this.velocity.y = -this.velocity.y
}
//if ball goes below the paddle GAME OVER
else if(this.y + this.velocity.y > paddle.y - ballRadius){
if(ball.x > paddle.x && ball.x < paddle.x + paddleWidth){
ball.velocity.y = -ball.velocity.y
console.log('Bop')
}else{
console.log('Game Over')
}
}
// window.location.reload()
this.draw()
this.x += this.velocity.x
this.y += this.velocity.y
}
}